1

I have an app that works fine in android 2.3.4, 2.3.6 and 2.3.7 but it doesn´t in android 4.1.2 or 4.2.2 (I have no more devices)

The app calls to a jar file that returns a result, when I test the app in 2.3.7 I make the call to the jar and takes the result but when I do it with android 4.1.2 it forces the close. It´s quite weird because the only difference is the OS because the app is the same.

The jar file makes a call to internet, I don´t think this could be the mistake

Thanks again everybody!

Ivan
  • 215
  • 3
  • 17

2 Answers2

1

The jar file makes a call to internet, I don´t think this could be the mistake

=> Sorry dude problem is here, as you said your app is working fine in < 2.3 versions but not in >2.3, reasons behind this failure is NetworkOnMainThreadException.

Now, Why this exception is being raised whenever you run app in >2.3 version device?

=> NetworkOnMainThreadException occurs whenever an application attempts to perform a networking operation on its main thread.

Solutions:

Below are the two solutions to resolve this issue:

1) Standard way is to make webservice call by implementing AsyncTask.

2) The lazy way of handling this is to turn the check of:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

From android Versions 4.0 (ICS) onwards networking operation should be run in AsynTask. Run your code in AsyncTask:

private class Task extends AsyncTask<Void, Void, Void> {

    private Exception exception;

    @Override
    protected Void doInBackground(Void... urls) { 
        /// run your network operations here     
        return null;         
    }

    @Override
    protected void onPostExecute(RSSFeed feed) {

   }     
}

 new Task ().execute();
Ahmad
  • 69,608
  • 17
  • 111
  • 137
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166