Android : How to use the java-json library in Android

on Monday, September 8, 2014


I have an AsyncTask that fetches XML data from the internet. I want to convert it into JSON and am using the java-json library. But the app crashes on me. This is the 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;
}


I am successfully retrieving the XML data because I can see it in the Logcat. I think my problem is that I'm not using the library properly. The Logcat shows the following error messages when the app crashes



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


Thank you in advance for any help.


0 comments:

Post a Comment