1

I recently worked on an application which connects and parse data from a webserver into an android device, tested on froyo and gingerbread and works fine but crashes on ICS devices. As for the UI I used gingebread objects so I think it's not the issue.

It works just fine on the first run even after connecting to facebook but errors on the part where it fetch and parse all of the JSON data gathered.

Here's the logcat errors straight from the device:

E/AndroidRuntime(25620): FATAL EXCEPTION: Thread-9660
E/AndroidRuntime(25620): java.lang.UnsupportedOperationException
E/AndroidRuntime(25620):        at java.lang.Thread.stop(Thread.java:1076)
E/AndroidRuntime(25620):        at java.lang.Thread.stop(Thread.java:1063)
E/AndroidRuntime(25620):        at com.android.guestlist.SplashScreen$1.run(Spla
shScreen.java:89)
E/android.os.Debug( 2090): !@Dumpstate > dumpstate -k -t -n -z -d -o /data/log/d
umpstate_app_error

E/Launcher(18546): Error finding setting, default accessibility to not found: ac
cessibility_enabled

E/log_tag (25620): Error in http connection android.os.NetworkOnMainThreadExcept
ion

E/log_tag (25620): Error converting result java.lang.NullPointerException

E/log_tag (25620): Error parsing data org.json.JSONException: End of input at ch
aracter 0 of

E/log_tag (25620): Error in http connection android.os.NetworkOnMainThreadExcept
ion

E/log_tag (25620): Error converting result java.lang.NullPointerException

E/log_tag (25620): Error in http connection android.os.NetworkOnMainThreadExcept
ion

E/log_tag (25620): Error parsing data org.json.JSONException: End of input at ch
aracter 0 of

And here's my JSON parser since I think this is where the problem happens? And oh, the logs I created doesn't even appear of the phone logs Well I can't really say but here is the code:

public void getDataFromWeb(String a, String b){
        String result = "";

        Log.v(TAG, "Name Value Pair a " + a);
        Log.v(TAG, "Name Value Pair b " + b);

        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("a_param",a));
        nameValuePairs.add(new BasicNameValuePair("b_param",b));

             try{
                     HttpClient httpclient = new DefaultHttpClient();
                     HttpPost httppost = new HttpPost("http://[mywebserviceshere].php");
                     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     HttpResponse response = httpclient.execute(httppost);
                     HttpEntity entity = response.getEntity();
                     is = entity.getContent();
                     Log.v(TAG, "connected");
             }catch(Exception e){
                     Log.v(TAG, "run failed");

                     Log.e("log_tag", "Error in http connection "+e.toString());
             }

             try{
                     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                     sb = new StringBuilder();
                     String line = "0";

                     while ((line = reader.readLine()) != null) {
                             sb.append(line + "\n");
                     }
                     is.close();
                     result=sb.toString(); 
                     Log.v(TAG, "buffered read");
             }catch(Exception e){
                 Log.v(TAG, "buffered error");
                     Log.e("log_tag", "Error converting result "+e.toString());
             }

             try{
                 Log.v(TAG, result);

                     JSONArray jArray = new JSONArray(result);
                     JSONObject json_data=null;

                     for(int i=0;i<jArray.length();i++){
                         Log.v(TAG, "loop start");

                             json_data = jArray.getJSONObject(i);
                             w.add(json_data.getString("w_data"));
                             x.add(json_data.getString("x_data"));
                             y.add(json_data.getString("y_data"));
                             z.add(json_data.getString("y_data"));
                             Log.v(TAG, "list added");
                     }

             }catch(JSONException e){
                 w.add("0");
                 x.add("No Data");
                 y.add("No Data");
                 z.add("No Data");
                 Log.v(TAG, "rest failed");
                     Log.e("log_tag", "Error parsing data "+e.toString());
             }


    }

And finally here's exactly what it looks like in AVD:

enter image description here

Is this an issue on ICS or I have to revise some of my codes?

KaHeL
  • 4,301
  • 16
  • 54
  • 78

3 Answers3

4

You've got a NetworkOnMainThreadException and that is generally a sign of poor code, and it'll result in a bad user experience because the application locks up whenever it's running some sort of network activity. Yes, it (unfortunately) works on pre-Honeycomb devices, but it's not what anyone should be going for.

To solve your problem, I'd highly recommend you use a single AsyncTask and do all your HTTP calls in the doInBackground() method. Once they're completed, the onPostExecute() method will be called automatically, so you can update the GUI. Quite simple.

Take a look at the documentation for AsyncTask and understand it before doing anything else. I'm sure this will work perfectly for your application: http://developer.android.com/reference/android/os/AsyncTask.html

So no - it's not a problem with Ice Cream Sandwich. It simply won't allow you to make network requests on the main thread because it can block everything else.

(this is pretty similar to another question I answered a few days ago, so part of the answer is copied from there: https://stackoverflow.com/a/11897381/762442)

Community
  • 1
  • 1
Michell Bak
  • 13,182
  • 11
  • 64
  • 121
1

You are getting this exception because you are performing network operation in main thread

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://[mywebserviceshere].php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

This was allowed before Honeycomb but after honeycomb this has been stopped and if you do so, you will get NetworkOnMainThreadException.

So its better to use AsyncTask. Using this, you can perform network operation in background thread(other than main thread).

To know more how AsyncTask works, see this link

AndroDev
  • 3,236
  • 8
  • 35
  • 49
0

Use the Asynctask in your code your getting NetworkOnMainThreadException , not only with ics but also with honeycomb it gives you the same Exception.

Nikhil Virupakshi
  • 482
  • 2
  • 13
  • 26