我有一个从互联网上获取XML数据的AsyncTask。我想将它转换为JSON并使用java-json库。但该应用程序崩溃了我。这是AsyncTask
private class GetBlogPostsTask extends AsyncTask<Object, Void, JSONObject> {
@Override
protected JSONObject doInBackground(Object... arg0) {
int responseCode = -1;
JSONObject jsonResponse = null;
try {
URL blogFeedUrl = new URL("some url");
HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
connection.connect();
responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int count; (count = inputStream.read(buffer)) != -1; ) {
out.write(buffer, 0, count);
}
byte[] response = out.toByteArray();
String responseData = new String(response, "UTF-8");
Log.d("TAG", responseData);
try {
jsonResponse = XML.toJSONObject(responseData);
} catch (JSONException e) {
Log.e("JSON exception", e.getMessage());
e.printStackTrace();
}
} else {
Log.i(TAG, "Unsuccessful HTTP Response Code: "
+ responseCode);
}
} catch (MalformedURLException e) {
logException(e);
} catch (IOException e) {
logException(e);
} catch (Exception e) {
logException(e);
}
return jsonResponse;
}
我正在成功检索XML数据,因为我可以在Logcat中看到它。我认为我的问题是我没有正确使用该库。当应用程序崩溃时,Logcat会显示以下错误消息
09-08 04:58:42.262: E/AndroidRuntime(1112): FATAL EXCEPTION: AsyncTask #1
09-08 04:58:42.262: E/AndroidRuntime(1112): Process: com.example.xmltojsontest, PID: 1112
09-08 04:58:42.262: E/AndroidRuntime(1112): java.lang.RuntimeException: An error occured while executing doInBackground()
09-08 04:58:42.262: E/AndroidRuntime(1112): at android.os.AsyncTask$3.done(AsyncTask.java:300)
09-08 04:58:42.262: E/AndroidRuntime(1112): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
09-08 04:58:42.262: E/AndroidRuntime(1112): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
09-08 04:58:42.262: E/AndroidRuntime(1112): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
09-08 04:58:42.262: E/AndroidRuntime(1112): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-08 04:58:42.262: E/AndroidRuntime(1112): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-08 04:58:42.262: E/AndroidRuntime(1112): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-08 04:58:42.262: E/AndroidRuntime(1112): at java.lang.Thread.run(Thread.java:841)
09-08 04:58:42.262: E/AndroidRuntime(1112): Caused by: java.lang.NoSuchMethodError: org.json.XMLTokener.end
09-08 04:58:42.262: E/AndroidRuntime(1112): at org.json.XMLTokener.nextCDATA(XMLTokener.java:69)
09-08 04:58:42.262: E/AndroidRuntime(1112): at org.json.XML.parse(XML.java:165)
09-08 04:58:42.262: E/AndroidRuntime(1112): at org.json.XML.parse(XML.java:272)
09-08 04:58:42.262: E/AndroidRuntime(1112): at org.json.XML.parse(XML.java:272)
09-08 04:58:42.262: E/AndroidRuntime(1112): at org.json.XML.parse(XML.java:272)
09-08 04:58:42.262: E/AndroidRuntime(1112): at org.json.XML.toJSONObject(XML.java:369)
09-08 04:58:42.262: E/AndroidRuntime(1112): at com.example.xmltojsontest.MainActivity$GetBlogPostsTask.doInBackground(MainActivity.java:168)
09-08 04:58:42.262: E/AndroidRuntime(1112): at com.example.xmltojsontest.MainActivity$GetBlogPostsTask.doInBackground(MainActivity.java:1)
09-08 04:58:42.262: E/AndroidRuntime(1112): at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-08 04:58:42.262: E/AndroidRuntime(1112): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-08 04:58:42.262: E/AndroidRuntime(1112): ... 4 more
提前感谢您的帮助。