What I’m trying to do is get some json results back from a URL using a Bufferedreader which uses a inputstreamreader. I read each line and for each JSONObject i get properties and do awesome things with them later.
Anyway, on Android 2.2 the code works fine but when i test on a 2.3 device (emulated, stock, or custom rom alike) i get this exception:
06-22 15:33:13.958: ERROR/App(416): IOException
06-22 15:33:13.958: ERROR/App(416): java.io.IOException: CRC mismatch
06-22 15:33:13.958: ERROR/App(416): at java.util.zip.GZIPInputStream.verifyCrc(GZIPInputStream.java:201)
06-22 15:33:13.958: ERROR/App(416): at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:184)
06-22 15:33:13.958: ERROR/App(416): at java.io.InputStreamReader.read(InputStreamReader.java:255)
06-22 15:33:13.958: ERROR/App(416): at java.io.BufferedReader.fillBuf(BufferedReader.java:128)
06-22 15:33:13.958: ERROR/App(416): at java.io.BufferedReader.readLine(BufferedReader.java:357)
06-22 15:33:13.958: ERROR/App(416): at ca.bnotions.App.Places.fetchData(Places.java:57)
06-22 15:33:13.958: ERROR/App(416): at ca.bnotions.App.Places.onCreate(Places.java:40)
06-22 15:33:13.958: ERROR/App(416): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-22 15:33:13.958: ERROR/App(416): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-22 15:33:13.958: ERROR/App(416): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-22 15:33:13.958: ERROR/App(416): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-22 15:33:13.958: ERROR/App(416): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-22 15:33:13.958: ERROR/App(416): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 15:33:13.958: ERROR/App(416): at android.os.Looper.loop(Looper.java:123)
06-22 15:33:13.958: ERROR/App(416): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-22 15:33:13.958: ERROR/App(416): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 15:33:13.958: ERROR/App(416): at java.lang.reflect.Method.invoke(Method.java:507)
06-22 15:33:13.958: ERROR/App(416): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-22 15:33:13.958: ERROR/App(416): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-22 15:33:13.958: ERROR/App(416): at dalvik.system.NativeStart.main(Native Method)
Here is the code I’m using:
public ArrayList<String> fetchData()
{
list_items = new ArrayList<String>();
try {
URL places_data = new URL("http://myurl.com");
URLConnection tc = places_data.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONArray ja = new JSONArray(line);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
list_items.add(jo.getString("name"));
}
}
} catch (MalformedURLException e) {
Log.e("MyApp","MalformedURLException",e);
} catch (IOException e) {
Log.e("MyApp","IOException",e);
} catch (JSONException e) {
Log.e("MyApp","JSONException",e);
}
return list_items;
}
Any insight would be greatly appreciated.
Thanks,
Marcus
I don’t have time to research this to understand why it doesn’t work and why you are getting the error. But I do have time to show you how I do it which works great.
Download the Apache Mime and Commons HttpClient Jar libraries and add them to your project’s Java Build Path.
Now that you have parsed the response, just turn it in a JSONObject.
I hope that works for you as well as it works for me.