0

The below is the json response of api which contains the link of the pdf file.

{
  "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
  }
}

The below is the code to display the pdf file

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)
    }
}

The code runs without error but when I open the app in logcat its showing that unable to load pdf.

Please help me to fix this. Actually since the pdf file link is of string type I am parsing it into Uri and then I am trying to load the pdf

Bruno
  • 3,204
  • 4
  • 14
  • 30

0 Answers0