2012-04-23 144 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