2012-12-17 126 views
1

我是android開發的初學者,在下面的代碼中出錯。我打電話ASYN用於HTTP request.Its給我了java.lang.RuntimeException錯誤的方法在執行doInBackground()執行doInBackground()時發生java.lang.RuntimeException錯誤

private class PostAlert extends AsyncTask<String, Integer, JSONArray>{ 

    private ProgressDialog progressDialog; 

    @Override 
    protected JSONArray doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     JSONArray menuitemArr = null; 
     String url=params[0]; 

     System.out.println("fsfsddddf"+params[0]); 
     ControlDashboard obj = new ControlDashboard(); 
     try{ 
     JSONObject aobj = obj.getJSONFromUrl(url); 

      JSONObject array = aobj.getJSONObject("alert_list");  
      menuitemArr = array.getJSONArray("array"); 

     } 
     catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }  

     return menuitemArr; 
    } 

    @Override 
    protected void onPostExecute(JSONArray menuitemArr) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(menuitemArr); 
     if (menuitemArr.length() == 0) { 
      startActivity(new Intent("com.example.mysampleapp.ABOUT")); 
      //Toast.makeText(Alert.this, "No Alerts", Toast.LENGTH_LONG).show(); 
     }else{ 
     name = new String[menuitemArr.length()]; 
     alert = new String[menuitemArr.length()]; 
     date = new String[menuitemArr.length()]; 
     for (int i = 0; i < menuitemArr.length(); i++) { 
      // printing the values to the logcat 
      try { 
       name[i] = menuitemArr.getJSONObject(i).getString("name").toString(); 
       alert[i] = menuitemArr.getJSONObject(i).getString("message").toString(); 
       date[i] = menuitemArr.getJSONObject(i).getString("date").toString(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
     ListView list = (ListView) findViewById(R.id.listView3); 
     ArrayList<HashMap<String, String>> mylistData = new ArrayList<HashMap<String, String>>(); 
     String[] columnTags = new String[] {"col1", "col2", "col3"}; 
     int[] columnIds = new int[] {R.id.alert1, R.id.alert2, R.id.alert3}; 
     for(int i=0; i<name.length; i++) 
     { 
     HashMap<String,String> map = new HashMap<String, String>(); 
      map.put("col1", name[i]); 
      map.put("col2", alert[i]); 
      map.put("col3", date[i]); 
     mylistData.add(map); 
     } 
     SimpleAdapter arrayAdapter = new SimpleAdapter(Alert.this, mylistData, R.layout.alert_view,columnTags,columnIds); 
     list.setAdapter(arrayAdapter);  
     } 
    } 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     progressDialog = new ProgressDialog(Alert.this); 
     progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     progressDialog.setMessage("please wait..."); 
     progressDialog.setCancelable(false); 
     progressDialog.setIndeterminate(false); 
     progressDialog.setMax(100); 
     progressDialog.setProgress(0); 
     progressDialog.show(); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     // TODO Auto-generated method stub 
     super.onProgressUpdate(values); 
     //progressDialog.dismiss(); 
    } 



} 

錯誤,當我運行我的Android應用程序出錯。

12-17 13:22:48.035: W/dalvikvm(1160): threadid=8: thread exiting with uncaught exception (group=0x4001d800) 
12-17 13:22:48.045: E/AndroidRuntime(1160): FATAL EXCEPTION: AsyncTask #2 
12-17 13:22:48.045: E/AndroidRuntime(1160): java.lang.RuntimeException: An error occured while executing doInBackground() 
12-17 13:22:48.045: E/AndroidRuntime(1160):  at android.os.AsyncTask$3.done(AsyncTask.java:200) 
12-17 13:22:48.045: E/AndroidRuntime(1160):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
12-17 13:22:48.045: E/AndroidRuntime(1160):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
12-17 13:22:48.045: E/AndroidRuntime(1160):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
12-17 13:22:48.045: E/AndroidRuntime(1160):  at java.util.concurrent.FutureTask.run(FutureTask.java:137) 

JSON請求類:

public class ControlDashboard extends Activity { 
    public static DefaultHttpClient httpClient; 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    public JSONObject getJSONFromUrl(String url) { 

     // Making HTTP request 
     try { 
      // defaultHttpClient 
      httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent();   

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      jObj = null; 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 



} 
+0

看到我的編輯答案,只是複製/過去PostAlert類,如果仍面臨着同樣的問題 –

+0

@ρяσѕρєяK不適合我:( –

+0

@ρяσѕρєяK我已更新的問題看看 –

回答

1

您設定背景ListAdapter哪些不應該做的,因爲它是UI線程任務。將doInBackground()中的整個代碼更改爲onPostExecute(),但getJsonFromUrl(url)除外。您可以返回doInBackground()中的json對象,您可以在中將其作爲結果參數。不過你得到運行時錯誤在onPostExecute()過,那麼你正好這條線listview.setAdapter()

runOnUIThread(new Runnable(){ 
public void run() 
{ 

} 
}); 

我希望它清楚,它會爲你工作。

+0

的實例看看更新的問題 –

+0

@ user1841413在嘗試編寫完整的doInBackground()代碼並抓住例外。打印堆棧跟蹤e並找出你正在得到什麼樣的異常。並檢查一次您在清單文件中提供Internet權限,這可能是原因。 – TNR

3
startActivity(new Intent("com.example.mysampleapp.ABOUT")); 

ListView list = (ListView) findViewById(R.id.listView3); 

這裏您試圖訪問的UI元素從後臺線程的AsyncTask doInBackground這是不可能的

溶液中運動相關的代碼,所有的UI在onPostExecute爲後更新的用戶界面意味着doInBackground complete

你可以在這裏看到我們如何從更新UI當doInBackground執行完整

編輯後:或使用此PostAlert的AsyncTask類:

private class PostAlert extends AsyncTask<String, Integer, JSONArray>{ 

     private ProgressDialog progressDialog; 

     @Override 
     protected JSONArray doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      JSONArray menuitemArr=null; 
      String url=params[0]; 
      System.out.println("fsfsddddf"+url); 

     // System.out.println("fsfsddddfobj"+obj); 
      try{ 

      JSONObject aobj = obj.getJSONFromUrl(url); 
      System.out.println("fsfsf"+aobj); 

       JSONObject array = aobj.getJSONObject("alert_list");  
       menuitemArr = array.getJSONArray("array"); 

      catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }  
      return menuitemArr; 
     } 

     @Override 
     protected void onPostExecute(JSONArray menuitemArr) { 
      // TODO Auto-generated method stub 

       if (menuitemArr.length() == 0) { 
        startActivity(new Intent("com.example.mysampleapp.ABOUT")); 
        //Toast.makeText(Alert.this, "No Alerts", Toast.LENGTH_LONG).show(); 
       }else{ 
       name = new String[menuitemArr.length()]; 
       System.out.println("name"+name); 
       alert = new String[menuitemArr.length()]; 
       date = new String[menuitemArr.length()]; 
       for (int i = 0; i < menuitemArr.length(); i++) { 
        // printing the values to the logcat 
        name[i] = menuitemArr.getJSONObject(i).getString("name").toString(); 
        alert[i] = menuitemArr.getJSONObject(i).getString("message").toString(); 
        date[i] = menuitemArr.getJSONObject(i).getString("date").toString(); 
       } 
       ListView list = (ListView) findViewById(R.id.listView3); 
       ArrayList<HashMap<String, String>> mylistData = new ArrayList<HashMap<String, String>>(); 
       String[] columnTags = new String[] {"col1", "col2", "col3"}; 
       int[] columnIds = new int[] {R.id.alert1, R.id.alert2, R.id.alert3}; 
       for(int i=0; i<name.length; i++) 
       { 
       HashMap<String,String> map = new HashMap<String, String>(); 
        map.put("col1", name[i]); 
        map.put("col2", alert[i]); 
        map.put("col3", date[i]); 
       mylistData.add(map); 
       } 
       SimpleAdapter arrayAdapter = new SimpleAdapter(Alert.this, mylistData, R.layout.alert_view,columnTags,columnIds); 
       list.setAdapter(arrayAdapter);  
       } 
      } 

      super.onPostExecute(result); 
     } 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      progressDialog = new ProgressDialog(Alert.this); 
      progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
      progressDialog.setMessage("please wait..."); 
      progressDialog.setCancelable(false); 
      progressDialog.setIndeterminate(false); 
      progressDialog.setMax(100); 
      progressDialog.setProgress(0); 
      progressDialog.show(); 
      System.out.println("fsgsgsg"); 
     } 
     @Override 
     protected void onProgressUpdate(Integer... values) { 
      // TODO Auto-generated method stub 
      progressDialog.setProgress(values[0]); 
     } 



    } 
} 

http://developer.android.com/reference/android/os/AsyncTask.html

相關問題