Android: json response, google api and getting image from url

sowailam

I'm using google books api to get some data as json response. in the response, there are JSONArrays, each element is a book and it contains data about it. the problem is the elements should be symmetric when it comes to the data they contain. in other words, each element should contain a title, authors, book cover url, rating and so on. and when a book doesn't contain ,for example, a rating, this value should exist and be empty but it doesn't exist at all for that element so in the for loop it throws an exception

the exception message

so what should i do to avoid such a problem and i do need that data even if it's empty.


the other part is there is a url for the cover of the book provided in the response. i want to get that image. i tried both top answers in this link and non of them seemed to work.

Muthukrishnan Rajendran

Try to use optJSONObject instead of getJSONObject to get JSONObject.
Note: getJSONObject will through error if the object don't have the key, but optJSONObject will just return null if the object don't have value

JSONArray array = null;// your array
            for (int i = 0; i < array.length(); i++) {
                JSONObject jsonObject = array.optJSONObject(i);

                if(jsonObject == null) {
                    continue;
                }

    //            JSONObject ratingObject = jsonObject.getJSONObject("rating"); // Dont use this
                JSONObject ratingObject = jsonObject.optJSONObject("rating");

                if(ratingObject != null) {
                    // Do someting here
                }
            }

For Image loading you can use Glide lib it may help you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get / Make thumbnail image from video url getting in API response

From Dev

Getting response from GOOGLE MAPS GEOLOCATION API in PHP, the output is in JSON

From Dev

Getting image url from json object not working

From Dev

Can Google's Vision API accept a URL from any external server and return a response on that image?

From Dev

Retrieve JSON data returned from GOOGLE FINANCE API URL in android

From Dev

Windows Silverlight phone app getting a json response from url

From Dev

Getting Json Response from an API using javascript/jquery

From Dev

Matching the Json schema with the response getting from the API in rest-assured

From Dev

Android Google Maps Overlay image from URL

From Dev

Getting specific data from returned response of Facebook API for android

From Dev

Show image from Google API Place Photo response

From Dev

Comparing json response from android to external json url in php server

From Dev

How to get a JSON response from a Google Image Search?

From Dev

Download Image from google drive Android API?

From Dev

Why is my Image not getting downloaded from the URL in android?

From Dev

Retrieving bitmap image from a URL ,getting nullpointerexception randomly in android

From Dev

Rails 4 API, how to create a URL from a JSON response?

From Dev

Image Search API bing Retrofit not getting response

From Dev

how to get json response from google custom search Api

From Dev

android parse JSON from url.. is not working?there is no response data

From Dev

Error getting image from url

From Dev

Getting the URL from a submitted image

From Dev

Get back json response from android management API call

From Dev

Getting the right accentuation from Google API Geocode - Android

From Dev

Empty response from Google Vision API with an image stored in Google Cloud Storage

From Dev

Unable to download image from Google Cloud Storage url with Android

From Dev

How post Image and Link URL in Google Plus from Android Application?

From Dev

JSON response is getting cut off, Android

From Dev

Getting Image from gallery in android

Related Related

  1. 1

    Get / Make thumbnail image from video url getting in API response

  2. 2

    Getting response from GOOGLE MAPS GEOLOCATION API in PHP, the output is in JSON

  3. 3

    Getting image url from json object not working

  4. 4

    Can Google's Vision API accept a URL from any external server and return a response on that image?

  5. 5

    Retrieve JSON data returned from GOOGLE FINANCE API URL in android

  6. 6

    Windows Silverlight phone app getting a json response from url

  7. 7

    Getting Json Response from an API using javascript/jquery

  8. 8

    Matching the Json schema with the response getting from the API in rest-assured

  9. 9

    Android Google Maps Overlay image from URL

  10. 10

    Getting specific data from returned response of Facebook API for android

  11. 11

    Show image from Google API Place Photo response

  12. 12

    Comparing json response from android to external json url in php server

  13. 13

    How to get a JSON response from a Google Image Search?

  14. 14

    Download Image from google drive Android API?

  15. 15

    Why is my Image not getting downloaded from the URL in android?

  16. 16

    Retrieving bitmap image from a URL ,getting nullpointerexception randomly in android

  17. 17

    Rails 4 API, how to create a URL from a JSON response?

  18. 18

    Image Search API bing Retrofit not getting response

  19. 19

    how to get json response from google custom search Api

  20. 20

    android parse JSON from url.. is not working?there is no response data

  21. 21

    Error getting image from url

  22. 22

    Getting the URL from a submitted image

  23. 23

    Get back json response from android management API call

  24. 24

    Getting the right accentuation from Google API Geocode - Android

  25. 25

    Empty response from Google Vision API with an image stored in Google Cloud Storage

  26. 26

    Unable to download image from Google Cloud Storage url with Android

  27. 27

    How post Image and Link URL in Google Plus from Android Application?

  28. 28

    JSON response is getting cut off, Android

  29. 29

    Getting Image from gallery in android

HotTag

Archive