0

My users are reporting that when they download a JSON file using WiFi, the app works like charm, but when they attempt to download the same file using their data connection: GPRS, 3G, 3.5G etc... the app force closes. It doesn't happen with all types of smartphones, e.g, with my LG Optimus Black it doesn't happen.

To retrieve this file I use an AsyncTask.

private class GetData extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... args) {

            DefaultHttpClient httpclient = new DefaultHttpClient();
            try {

                HttpGet httpget = new HttpGet("http://url.com);
                HttpResponse response = httpclient.execute(httpget);
                strPlayers = inputStreamToString(response.getEntity().getContent()).toString();
            }
            catch (IOException e) { e.printStackTrace(); } 
            finally { httpclient.getConnectionManager().shutdown(); }

           return null;
        }

        protected void onPostExecute(Void result) {

            Players players = new Gson().fromJson(strPlayers, Players.class);

            quantity_darkmatter.setText(String.valueOf(players.userName));

        }
    }

Here's the StackTrace()

java.lang.RuntimeException: Unable to start activity ComponentInfo{carl.fri.fer.omegan/carl.fri.fer.omegan.Research}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at carl.fri.fer.omegan.Research.onStart(Research.java:613)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3791)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1632)
... 11 more

What can be the problem?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
anonymous
  • 1,320
  • 5
  • 21
  • 37

1 Answers1

0

For non-WIFI the connection might take considerably more time, so it might be an issue of connection timeout, when that happens strPlayers will be blank and as there is no errorhandling in onPostExecute this might give Gson.fromJson hickups and make it throw an exception... try testing that strPlayers!=null before trying to parse it or putting it in a try-catch block to see if it throws an exception of some sort.

Is there any output of the e.printStackTrace() in the doInBackground function?

Tor P
  • 1,351
  • 11
  • 9
  • hmm... DefaultHttpClient by default has an infinite timeout, so that might be why your app is hanging, you might want to try to set a timeout to see if the connection times out (you'll get an error message which will be printStackTrace()'ed... why the httpclient won't connect to the server is a different question... maybe it has to do with apache's defaulthttpclient not being made for use on cellphones with high-latency connections? – Tor P Apr 23 '12 at 10:55
  • you might want to look at this: http://stackoverflow.com/questions/6705170/httpclient-execute-keeps-giving-connecttimeoutexception – Tor P Apr 23 '12 at 11:03
  • I've edited my post with the stackTrace. I'm going to take a look to the link! Thank you! – anonymous Apr 23 '12 at 11:05
  • I think that there'll be a problem if I add a timeout, because with WiFi, the file is downloaded almost immediately, but with GPRS, the connection sometimes take more than 30 seconds because it has to download some images too... – anonymous Apr 23 '12 at 11:09
  • just set the tcp timeout, not the socket timeout (or at least set the socket timeout to a large value, like 60s) – Tor P Apr 23 '12 at 11:13