2014-03-02 254 views
2

我正在開發一個從互聯網獲取數據的android應用程序。當互聯網連接仍然在,然後該應用程序正常工作。但是,當互聯網連接連接保持關閉,然後崩潰。我在谷歌搜索,但我不能解決我的問題。我還添加了在Android的Menifest這些權限文件當互聯網連接關閉時,Android應用程序崩潰

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

我的整個代碼如下所示,

public class NoticeBoard extends Activity { 


ArrayList<String> title_array = new ArrayList<String>(); 
ArrayList<String> notice_array = new ArrayList<String>(); 

ListView list; 
base_adapter3 adapter; 
TextView f; 
TextView msg; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.list_notice); 
    list = (ListView) findViewById(R.id.list_notice2); 
    msg=(TextView)findViewById(R.id.error_msg); 

    new test_ays().execute(); 

} 




class test_ays extends AsyncTask<Void, Void, String> { 

    @Override 
    protected String doInBackground(Void... params) { 
     String str = null ; 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      //HttpPost httppost = new HttpPost("http://10.0.2.2/BSDI/show.php"); 
      HttpPost httppost = new HttpPost("http://logger.net46.net/android_bsdi_tuhin/show.php");   
      HttpResponse response = httpclient.execute(httppost); 
      str = EntityUtils.toString(response.getEntity()); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 



     } 
     return str; 


    } 



@Override 
protected void onPostExecute(String result) { 

    super.onPostExecute(result); 

    String response = result.toString(); 
    msg.setText(response); 

    try { 

     JSONArray new_array = new JSONArray(response); 

     for (int i = 0, count = new_array.length(); i < count; i++) { 
      try { 
       JSONObject jsonObject = new_array.getJSONObject(i); 
       title_array.add(jsonObject.getString("title").toString()); 
       notice_array.add(jsonObject.getString("notice").toString()); 

      } catch (JSONException e) { 
       e.printStackTrace(); 

      } 
     } 

     adapter = new base_adapter3(NoticeBoard.this, title_array, notice_array); 
     list.setAdapter(adapter); 
     // f=(TextView) findViewById(R.id.textTuh); 
     // f.setText(title_array); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 

    } 

} 
} 

} 

我logcat的錯誤是下面,

03-02 06:55:28.570: E/AndroidRuntime(788): FATAL EXCEPTION: main 
03-02 06:55:28.570: E/AndroidRuntime(788): Process: com.example.bsdiassistant, PID: 788 
03-02 06:55:28.570: E/AndroidRuntime(788): java.lang.NullPointerException 
03-02 06:55:28.570: E/AndroidRuntime(788): at  com.example.bsdiassistant.NoticeBoard$test_ays.onPostExecute(NoticeBoard.java:80) 
03-02 06:55:28.570: E/AndroidRuntime(788): at com.example.bsdiassistant.NoticeBoard$test_ays.onPostExecute(NoticeBoard.java:1) 
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.AsyncTask.finish(AsyncTask.java:632) 
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.AsyncTask.access$600(AsyncTask.java:177) 
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.Handler.dispatchMessage(Handler.java:102) 
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.Looper.loop(Looper.java:137) 
03-02 06:55:28.570: E/AndroidRuntime(788): at android.app.ActivityThread.main(ActivityThread.java:4998) 
03-02 06:55:28.570: E/AndroidRuntime(788): at java.lang.reflect.Method.invokeNative(Native Method) 
03-02 06:55:28.570: E/AndroidRuntime(788): at java.lang.reflect.Method.invoke(Method.java:515) 
03-02 06:55:28.570: E/AndroidRuntime(788): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
03-02 06:55:28.570: E/AndroidRuntime(788): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
03-02 06:55:28.570: E/AndroidRuntime(788): at dalvik.system.NativeStart.main(Native Method) 
+3

檢查'onPOstExecute' – Raghunandan

+0

請註明線'80' @'NoticeBoard'做Raghunandan說的 –

+0

什麼是'告示欄線80。 java'? – Raghunandan

回答

2

試試這個代碼:

@Override 
protected void onPostExecute(String result) { 

    super.onPostExecute(result); 

if(result!=null) // add this 
{ 
    String response = result.toString(); 
    msg.setText(response); 

    try { 

     JSONArray new_array = new JSONArray(response); 

     for (int i = 0, count = new_array.length(); i < count; i++) { 
      try { 
       JSONObject jsonObject = new_array.getJSONObject(i); 
       title_array.add(jsonObject.getString("title").toString()); 
       notice_array.add(jsonObject.getString("notice").toString()); 

      } catch (JSONException e) { 
       e.printStackTrace(); 

      } 
     } 

     adapter = new base_adapter3(NoticeBoard.this, title_array, notice_array); 
     list.setAdapter(adapter); 
     // f=(TextView) findViewById(R.id.textTuh); 
     // f.setText(title_array); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 

    } 
} 
} 
0

因爲你開始了AsyncTaskonCreate,所以一旦您的應用程序開始,AsyncTask將啓動,這需要互聯網連接,你應該檢查處理之前響應的狀態代碼,使用

response.getStatusLine().getStatusCode() 
1

當沒有互聯網連接有沒有響應返回。也許你可以在本地緩存響應,如果你想。緩存響應後,您無需發出網絡請求。您可以解析json並顯示數據。

檢查結果爲空,或者如果響應是空不

@Override 
protected void onPostExecute(String result) { 
    super.onPostExecute(result); 
    if(result!=null) 
    { 
      // do something 
    } 
相關問題