2012-04-23 96 views
0

我的用户报告说,当他们使用WiFi下载JSON文件时,该应用的工作方式就像魅力,但是当他们尝试使用他们的数据连接下载相同的文件时:GPRS,3G,3.5G等等。应用程序部队关闭。它不会发生在所有类型的智能手机上,例如,我的LG Optimus Black不会发生。互联网数据连接失败

要检索这个文件,我使用了一个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)); 

     } 
    } 

这里的堆栈跟踪()

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 

可以采取什么问题吗?

+0

好吧,在某处有一个空指针,在Research.java:613中会发生什么? – 2012-04-23 11:15:21

回答

0

对于非WIFI连接可能需要相当多的时间,所以它可能是一个连接超时的问题,当发生这种情况时,strPlayers将是空白的,因为onPostExecute中没有错误处理,这可能会给Gson.fromJson hickups并使它会抛出一个异常......在尝试解析它或将它放入try-catch块以查看它是否引发某种异常之前,尝试测试strPlayers!= null。

doInBackground函数中是否有e.printStackTrace()的输出?

+0

hmm ...默认情况下,DefaultHttpClient具有无限超时,所以这可能是您的应用程序挂起的原因,您可能想要设置超时以查看连接是否超时(您将收到一条错误消息, printStackTrace()'ed ...为什么httpclient不会连接到服务器是一个不同的问题...也许这与apache的defaulthttpclient不适合用于具有高延迟连接的手机有关吗? – 2012-04-23 10:55:24

+0

您可能需要看看这个:http://stackoverflow.com/questions/6705170/httpclient-execute-keeps-giving-connecttimeoutexception – 2012-04-23 11:03:10

+0

我编辑我的帖子与stackTrace。我要看看链接!谢谢你! – 2012-04-23 11:05:29