1

I am making a simple application where i scan the barcode of a book and fetch its title and author from Google APIs,

Now, this is the url for json(for a particular book i am scanning)

https://www.googleapis.com/books/v1/volumes?q=isbn:9788120305960

using this code to get json in a string

HttpURLConnection urlConnection  = (HttpURLConnection)url.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
        String line = "";

        while ((line=bufferedReader.readLine())!=null)
        {
            response+=line;
        }

        bufferedReader.close();
        inputStream.close();
        urlConnection.disconnect();
        Log.d("Info",response);
        return response;

I store the result in a string and use this code to parse through (json_response is a string)

            JSONObject rootObject = new JSONObject(json_response);
            JSONArray items = rootObject.getJSONArray("items");
            JSONObject items_object = items.getJSONObject(0);
            JSONObject volume_info = items_object.getJSONObject("volumeInfo");
            book.setTitle(volume_info.getString("title"));
            JSONArray authors = volume_info.getJSONArray("authors");
            Log.d("Info","authors array length: "+authors.length());
            String author="";
            for (int i =0;i<authors.length();i++)
            {
                author+=authors.getString(i)+", ";
            }

            book.setAuthor(author);

The exception is:

Value null of type org.json.JSONObject$1 cannot be converted to JSONObject

also I used logcat to see what is contained in json_response it looks something like this

null{ "kind": "books#volumes", "totalItems": 1, "items":...

The null here is probably causing the problem, so... any insights how to deal with this???

PS: I am a student , dealing first time with json and android, code is unprofessional, please pardon :)

4
  • You should initialise the variable response to empty string. Does it help? Commented Jun 26, 2016 at 17:13
  • You can use the Volley to get a response from the google. After that use the GSON that will ease your work while parsing the JSON. Commented Jun 26, 2016 at 17:16
  • Yep it worked by setting response to empty string!! Commented Jun 26, 2016 at 17:59
  • ok I will add it as answer. Please accept it if you found useful Commented Jun 26, 2016 at 18:33

1 Answer 1

1

Having

null{ "kind": "books#volumes", "totalItems": 1, "items":...

means that the response value has not been initialised.

You should therefore initialise it to empty string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.