1

I'm trying to access the google books api and retrieve the list of books according to the keyword entered by the user. While trying to parse the Json Data I receive the following error:

The method I am using to parse JSON data:

W/System.err: org.json.JSONException: Unterminated array at character 11 of [C@425e2fb8

W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)

W/System.err:     at org.json.JSONTokener.readArray(JSONTokener.java:440)

W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:103)

W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:155)

W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:172)

W/System.err:     at com.example.android.booklisting.BookListingActivity.readIt(BookListingActivity.java:167)

W/System.err:     at com.example.android.booklisting.BookListingActivity.downloadUrl(BookListingActivity.java:145)

W/System.err:     at com.example.android.booklisting.BookListingActivity.access$100(BookListingActivity.java:39)

W/System.err:     at com.example.android.booklisting.BookListingActivity$DownloadWebpageTask.doInBackground(BookListingActivity.java:96)

W/System.err:     at com.example.android.booklisting.BookListingActivity$DownloadWebpageTask.doInBackground(BookListingActivity.java:90)

W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)

W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)

W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)

W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

W/System.err:     at java.lang.Thread.run(Thread.java:841)

com.example.android.booklisting I/dalvikvm: Could not find method android.widget.ArrayAdapter.getDropDownViewTheme, referenced from method    com.example.android.booklisting.BooksAdapter.access$super

The Method that I am using to Parse the JSON data:

  public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
    Reader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");
    char[] buffer = new char[len];
    reader.read(buffer);
    String jdata = buffer.toString();

    try {
        JSONObject jsonobject = new JSONObject(jdata);
        JSONArray jarray = (JSONArray) jsonobject.getJSONArray("items");

        for (int i = 0; i < jarray.length(); i++) {
            JSONObject volumeInfo = jarray.getJSONObject(i).getJSONObject("volumeInfo");
            String title = volumeInfo.getString("title");
            JSONArray authors = volumeInfo.getJSONArray("authors");
            for (int j = 0; j < authors.length(); j++) {
                String author = authors.getString(i);
                books.add(new Book(title,author));
            }

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }


    return new String(buffer);

}

Any idea as to where I might be going wrong in parsing ?? This is my first parsing program.

2
  • check my answer and if it not work then put your json response here Commented Jul 2, 2016 at 5:51
  • 1
    json response format is not proper Commented Jul 2, 2016 at 6:17

3 Answers 3

1

I think error is in your for loop of author so changed it by below code,

for (int j = 0; j < authors.length(); j++)
{
    String author = authors.getString(j);
    books.add(new Book(title,author));
}
Sign up to request clarification or add additional context in comments.

Comments

0

There are indexing Exception in you code. change to String author = authors.getString(i); to String author = authors.getString(j);

  public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
    Reader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");
    char[] buffer = new char[len];
    reader.read(buffer);
    String jdata = buffer.toString();

    try { 
        JSONObject jsonobject = new JSONObject(jdata);
        JSONArray jarray = (JSONArray) jsonobject.getJSONArray("items");

        for (int i = 0; i < jarray.length(); i++) {
            JSONObject volumeInfo = jarray.getJSONObject(i).getJSONObject("volumeInfo");
            String title = volumeInfo.getString("title");
            JSONArray authors = volumeInfo.getJSONArray("authors");
            for (int j = 0; j < authors.length(); j++) {
                String author = authors.getString(j);
                books.add(new Book(title,author));
            } 

        } 
    } catch (JSONException e) {
        e.printStackTrace();
    } 


    return new String(buffer);

} 

Comments

0

Check jdata with your JSON response may be you didn't write full string in jdata

please try this to get full string from InputStream

BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;

while ((line = r.readLine()) != null)
{
    total.append(line).append('\n');
}

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.