android - 我正在尝试从 json api 获取 pdf 链接并将其显示在 android studio 但由于响应是字符串我收到错误
问题描述
以下是 api 的 json 响应,其中包含 pdf 文件的链接。
{
"success": true,
"data": {
"_id": "1230",
"name": "multithreading",
"pdf": "https://fileupload99.s3.ap-south-1.amazonaws.com/33d969c4-34cc-4fc0-8e55-ad2094e16db7.pdf",
"image": "https://fileupload99.s3.ap-south-1.amazonaws.com/4e3ab24b-648c-4781-a225-3f2ad2e03f6d.png",
"__v": 0
}
}
下面是显示pdf文件的代码
class DescActivity : AppCompatActivity() {
lateinit var fileImage: ImageView
lateinit var progressBar: ProgressBar
lateinit var progressLayout: RelativeLayout
lateinit var pdfView: PDFView
var id: String? = "100"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_desc)
fileImage = findViewById(R.id.imgff)
pdfView=findViewById(R.id.PdfView)
progressBar = findViewById(R.id.progressbar)
progressBar.visibility = View.VISIBLE
progressLayout = findViewById(R.id.progresslayout)
progressLayout.visibility = View.VISIBLE
if (intent != null) {
id = intent.getStringExtra("_id")
} else {
Toast.makeText(this, "some error!", Toast.LENGTH_SHORT).show()
}
if (id == "100") {
finish()
Toast.makeText(this, "some unknown error!", Toast.LENGTH_SHORT).show()
}
val queue = Volley.newRequestQueue(this@DescActivity)
val url = "https://guru-raghavendra-api.herokuapp.com/files/id"
val jsonParams = JSONObject()
jsonParams.put("id", id)
val jsonRequest =
object : JsonObjectRequest(Request.Method.POST, url, jsonParams, Response.Listener {
println("response is $it")
val success = it.getBoolean("success")
if (success) {
val bookJsonObject = it.getJSONObject("data")
Picasso.get().load(bookJsonObject.getString("image"))
.error(R.drawable.ic_launcher_background)
.into(fileImage)
val pdfUrl=bookJsonObject.getString("pdf")
val ss= Uri.parse(pdfUrl)
pdfView.fromUri(ss).load()
progressLayout.visibility = View.GONE
}
}, Response.ErrorListener {
Toast.makeText(this, "volley error!", Toast.LENGTH_SHORT).show()
}) {
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
headers["Content-type"] = "application/json"
return headers
}
}
queue.add(jsonRequest)
}
}
代码运行没有错误,但是当我在 logcat 中打开应用程序时,它显示无法加载 pdf。
请帮我解决这个问题。实际上,由于 pdf 文件链接是字符串类型,因此我将其解析为Uri
然后尝试加载 pdf
解决方案
推荐阅读
- laravel - 如何在 Laravel 5.6 中禁用注册新用户
- sql - 如何获得一行中的最大值以及所有列的名称以获得相等的值
- php - 使用 PHP 正则表达式从字符串中提取年份
- javascript - 如何覆盖 Node.js 模块中的函数?
- batch-file - RegEx 适用于在线模拟器,但不适用于带有 findstr 的 BatchFile
- ios - AvPlayer 和 AirPlay 不能很好地工作
- android - 与 QUIC 一起使用的 Android WebView
- google-maps - React-native-Maps 附近的地点
- python - 如何启动运行具有多个参数的函数的单独进程?
- python - 在pytesseract python中保留多个空间