我试图访问google books api并根据用户输入的关键字检索书籍列表。在尝试解析Json数据时,我收到以下错误:
我用来解析JSON数据的方法:
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
我用于解析JSON数据的方法:
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);
}
任何关于我在解析时可能出错的地方的想法?这是我的第一个解析程序。
答案 0 :(得分:1)
我认为错误在你的for循环中,所以通过下面的代码改变了它,
for (int j = 0; j < authors.length(); j++)
{
String author = authors.getString(j);
books.add(new Book(title,author));
}
答案 1 :(得分:0)
您的代码中存在索引异常。更改为字符串author = authors.getString(i);
到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);
}
答案 2 :(得分:0)
使用您的JSON响应检查jdata可能是您没有在jdata中写入完整的字符串
请尝试从InputStream
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null)
{
total.append(line).append('\n');
}