2013-02-04 79 views
0

它適用於wifi或3G,但強制關閉與兩個斷開連接。 我正在從教程中學習,所以我不確定,但我相信代碼中會考慮互聯網斷開。xml解析應用程序部隊關閉與互聯網斷開連接

完全logcat的http://pastebin.com/aAEGwfFk

代碼:

package org.tatvamoksh.tml; 

import java.util.ArrayList; 
import java.util.HashMap; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 

import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 


public class Updates extends ListActivity { 

    public ArrayList<HashMap<String, String>> mylist; 
    public ListAdapter adapter; 
    public ProgressDialog progressDialog; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.updates_item); 


     mylist = new ArrayList<HashMap<String, String>>(); 

     adapter = new SimpleAdapter(this, mylist , R.layout.updates, 
         new String[] { "event", "notice" }, 
         new int[] { R.id.event_title, R.id.notice }); 


     final ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 
/*  lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
       @SuppressWarnings("unchecked") 
       HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);     
       Toast.makeText(Updates.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_LONG).show(); 

      } 
     }); 
*/   
     progressDialog = ProgressDialog.show(Updates.this, "Getting updates", "Please wait..."); 
     new GetDataTask().execute();   

    } 

    private Boolean isOnline() { 
     ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo ni = cm.getActiveNetworkInfo(); 
     if(ni != null && ni.isConnected()) 
      return true; 

     return false; 
    } 

    private class GetDataTask extends AsyncTask<Void, Void, Integer> { 

     @Override 
     protected Integer doInBackground(Void... params) { 



      // Simulates some delay 
      try { 
       Thread.sleep(2000); 
      } catch (InterruptedException e1) { 
       e1.printStackTrace(); 
      } 

      if(isOnline()){ 
       mylist.clear();     

       String xml = XMLfunctions.getXML(); 
       Document doc = XMLfunctions.XMLfromString(xml); 

       int numResults = XMLfunctions.numResults(doc); 

       if((numResults <= 0)){ 
        Toast.makeText(Updates.this, "Geen resultaten gevonden", Toast.LENGTH_LONG).show(); 
        finish(); 
       } 

       NodeList nodes = doc.getElementsByTagName("update"); 

       for (int i = 0; i < nodes.getLength(); i++) {       
        HashMap<String, String> map = new HashMap<String, String>();  

        Element e = (Element)nodes.item(i); 
        map.put("id", XMLfunctions.getValue(e, "id")); 
        map.put("event", "Event:" + XMLfunctions.getValue(e, "event")); 
        map.put("notice", "Update: " + XMLfunctions.getValue(e, "notice")); 
        mylist.add(map);    
       } 
      } 
      else{ 
       Toast.makeText(Updates.this, "No connection..", Toast.LENGTH_LONG).show();   
      } 

      return 1; 
     } 

     @Override 
     protected void onPostExecute(Integer result) { 

      setListAdapter(adapter); 
      progressDialog.dismiss(); 
      super.onPostExecute(result); 
     } 
    } 
} 

我曾嘗試尋找類似的問題,最引發NullPointerException異常,調試器沒有抓到任何(我認爲) screenshot of debugger perspective

+0

後完成日誌貓.. –

+0

完成logcat添加。 – hypd09

回答

0

刪除

Toast.makeText(Updates.this, "No connection..", Toast.LENGTH_LONG).show();   

現在是空的東西,讓你的AsyncTask返回另一個值(例如2)

else{ 
    return 2; 
} 

如果移動return 1;if(isOnline()裏面,那麼你就可以擺脫別人的和簡單的寫return 2;,但是這僅僅是一個可讀性的問題。

然後在onPostExecute()中,檢查值是否爲2,然後做Toast。

@Override 
    protected void onPostExecute(Integer result) { 
    if (result == 1){ 
     setListAdapter(adapter); 
     progressDialog.dismiss(); 
     super.onPostExecute(result); 
    } 
    else 
     Toast.makeText(Updates.this, "No connection..", Toast.LENGTH_LONG).show();   
    } 
+0

作品,非常感謝:) – hypd09

相關問題