2

I'm new to this Android platform, I'm going to write code for reading the JSON formatted pdf path and I need to open that pdf file in my android application.How do I code for the below problem?

The Url is like this: url://xyx.com/talentsday_toppers/Writing JSON format is like this:

{"details"[
{"id":"3",
"talent_category":"Writing",
"date":"13\/06\/2017",
"pdf_path":".\/image\/talentsday_toppers\/Indian-States-and-Capitals-GK-in-PDF1.pdf"}]}
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
Anusha B
  • 91
  • 1
  • 10
  • Try with **iTextPdf** library and try this one: https://stackoverflow.com/a/42887336/7806873 – Nitin Patel Jun 29 '17 at 11:56
  • That will get to result in String and then use JSON parsing to get data. – Nitin Patel Jun 29 '17 at 11:57
  • @Nithi Patel In XML file I need to add anything??? – Anusha B Jun 29 '17 at 12:08
  • Sorry you have url to read write, then I have to check this library will work or not. – Nitin Patel Jun 29 '17 at 12:11
  • 1
    @Nithi Patel, I'm reading the JSON like this: JSONObject jsonOject = new JSONObject(response); JSONArray jsonArray = jsonOject.getJSONArray("details"); JSONObject jsonOject1 = jsonArray.getJSONObject(0); PdfModel item = new PdfModel(jsonOject1.getString("pdf_path")); listItem.add(item); adapter = new PdfAdapter(listItem,getApplicationContext()); recyclerView.setAdapter(adapter); – Anusha B Jun 29 '17 at 12:12
  • 1
    @Nithi Patel, any other solution?? in the place of ImageView we can use Webview to open the pdf file – Anusha B Jun 29 '17 at 12:14
  • Your code is correct. It will get all in list. – Nitin Patel Jun 29 '17 at 12:14
  • @Nithi Patel, Then in Adapter class how we use imageView know like that we can use WebView,,, It will be work or?? – Anusha B Jun 29 '17 at 12:16
  • Like this way https://stackoverflow.com/a/5296125/7806873 , you can view pdf in WebView. – Nitin Patel Jun 29 '17 at 12:18
  • @Nithin Patel, Then how do I use getpdfUrl() in Adapter class?? – Anusha B Jun 29 '17 at 12:25
  • @Nithin Patel, Like to getUrl of the Image we use Picasso.with(context) .load(imageModel.getImageUrl()) .into(holder.imageView); Like how I can use?? – Anusha B Jun 29 '17 at 12:26
  • If you have both pdfPath and ImagePath in data. Then you can set imagePath using Picasso and OnClick of that item move that item's pdfPath to new Activity with WebView. – Nitin Patel Jun 29 '17 at 12:36
  • @Nithi Patel, No acutely only I have pdf path. So in Adapter class, i need to implement OnclickListener button to get that URL from Model class?? – Anusha B Jun 29 '17 at 12:39
  • Ok. then make RecyclerView item click and on that get URL as per position from Model list. – Nitin Patel Jun 29 '17 at 12:41
  • @Nithin Patel, In MainActivity I'm using "RecyclerView" and getting the "Json object" adding it into in "adapter". And in Model Class have one constructor() and getter(), And in adapter class, i used one layout(XML) file in that file have one LineraLayout and WebView. so in onBindViewHolder() method what i need to code?? – Anusha B Jun 29 '17 at 12:46
  • Then make click event of RecyclerView by this link using: http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/ Or you can make click inside Adapter class. – Nitin Patel Jun 29 '17 at 12:52
  • @Nithi Patel, If I make click function to recyclerVew how can I get JSON formatted PdfPath?? – Anusha B Jun 29 '17 at 12:55
  • @Nithin Patel, I need simple requirement like this: when I click the button it should open a pdf file, But the problem is I'm getting the path in JSON formatted.So for that, i need to code that I don't Know how do I? – Anusha B Jun 29 '17 at 12:58
  • You already have extracted and Put that in Models list right?. Now, get that like list.get(position) in OnClickListner of recycler. – Nitin Patel Jun 29 '17 at 12:59
  • @Nithin Patel, Ok I did that public String getPdfUrl() { return pdfUrl; } How can i use this function in Adapter class?? – Anusha B Jun 29 '17 at 13:02
  • @Nithin Patel, list.get(position) means how do I? Can you clearly tell me where to I add that code or how do I code for that JSON format? – Anusha B Jun 29 '17 at 13:14
  • You already get padf path and store in Model, So just get that from Model. – Nitin Patel Jun 29 '17 at 13:17
  • @Nithin Patel, How do I get and where to place?? to show that file which View I have to those things I don't know?? – Anusha B Jun 29 '17 at 13:22
  • Sorry Now find the RecyclerView list item click and how to get data in that click. Now office close time. – Nitin Patel Jun 29 '17 at 13:30
  • @Nithin Patel, Ok Thank You. – Anusha B Jun 29 '17 at 13:36
  • 1
    @Nithin Patel, I get the URL final PdfModel pdfModel = listIteml.get(position); String url = pdfModel.getPdfUrl(); But the proble is i'm not abel to see in my activity. webView.getSettings().setJavaScriptEnabled(true); webView.setVisibility(WebView.VISIBLE); webView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url="+url); I wrote that in my Adapter class. – Anusha B Jun 30 '17 at 04:59
  • if you don't mind can you move next comments solutions to chat. – Nitin Patel Jun 30 '17 at 05:12
  • @Nithin Patel, Ok I will. How to I?? – Anusha B Jun 30 '17 at 05:17
  • @Nithin Patel, I have only 19 reputations. How can I? – Anusha B Jun 30 '17 at 05:23
  • right leave it. – Nitin Patel Jun 30 '17 at 05:24
  • 1
    You don't have to write that in Adapter class. Write that coding in new class – Nitin Patel Jun 30 '17 at 05:25
  • 1
    Means in new Activity and from adapter class start that activity and pass that URL like putting in Intent. – Nitin Patel Jun 30 '17 at 05:26
  • And in new Activity get that value (getIntent(0.getStringExtra()...) then try this link coding...https://stackoverflow.com/a/40626648/7806873 – Nitin Patel Jun 30 '17 at 05:37
  • @Nithin Patel means in another activity need to add Webview there I can show my pdf file right?? I don't Know how to Interact with one activity to another through putExtra() and getExtara() method. – Anusha B Jun 30 '17 at 05:43
  • This way you can https://stackoverflow.com/a/15859509/7806873 – Nitin Patel Jun 30 '17 at 05:47
  • @Nithin Patel, While calling new Activity from the Adapter class getting an error. – Anusha B Jun 30 '17 at 06:02
  • From where are you calling? – Nitin Patel Jun 30 '17 at 06:07
  • @Nithin Patel, After the onBindViewHolder() {........ } Intent i = new Intent(PdfAdapter.this,WebViewActivity.class); i.putExtra("Key",url); startActivity(i); – Anusha B Jun 30 '17 at 06:10
  • holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(context,WebViewActivity.class); } }); – Nitin Patel Jun 30 '17 at 06:15
  • You have to pass context in constructor of adapter class and then pass that context to `Intent(context,WebViewActivity.class); – Nitin Patel Jun 30 '17 at 06:16
  • @Nithin Patel, Ok I will check that and if I get any issue I will reach you, Thank You. – Anusha B Jun 30 '17 at 06:21
  • @Nithin Patel, That's not working. startActivity(i); getting error. – Anusha B Jun 30 '17 at 06:30
  • what is error check in Android Monitor'--> Error spinner? – Nitin Patel Jun 30 '17 at 06:34
  • @Nithin Patel, It's not at all accepting startActibity() in adapter clas. – Anusha B Jun 30 '17 at 06:40
  • Paste code here..of startActivity So I will help you. – Nitin Patel Jun 30 '17 at 06:42
  • @Nithin Patel, public void onBindViewHolder(final ViewHolder holder, int position) { final PdfModel pdfModel = listIteml.get(position); url = pdfModel.getPdfUrl(); System.out.println("Url of the:"+url); holder.itemView.setOnClickListener(new View.OnClickListener() { Override public void onClick(View view) { Intent i = new Intent(context,WebViewActivity.class); i.putExtra("web",url); satrtActivity(i); } }); – Anusha B Jun 30 '17 at 07:36
  • ok now what is error? – Nitin Patel Jun 30 '17 at 07:40
  • @Nithin Patel, I totally change the code, Now I'm not using Adapter class and Model Class, Using 2 Activities, MainActivity and SubActivity. In MainActivity used the only button when I click that it's going to another activity. – Anusha B Jun 30 '17 at 08:17
  • good do it and solve that. – Nitin Patel Jun 30 '17 at 08:56
  • @Nithin Patel, But the issue is the pdf file is not opening. Requesting URL:url://xyz.com/talentsday_toppers/Writing But path getting the URL is like this: url://xyz.com/images/talentsday_toppers/ – Anusha B Jun 30 '17 at 09:03
  • I have send that how to view pdf again try it https://stackoverflow.com/questions/2655972/how-can-i-display-a-pdf-document-into-a-webview/40626648#40626648 – Nitin Patel Jun 30 '17 at 09:07
  • @Nithin Patel, Still pdf file is not opening. Requesting URL is different and response URL is different . – Anusha B Jun 30 '17 at 09:23
  • Why both are different? You have to just set pdf=pdfUrl. – Nitin Patel Jun 30 '17 at 09:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148033/discussion-between-nitin-patel-and-anusha-b). – Nitin Patel Jun 30 '17 at 09:34

0 Answers0