2017-09-24 125 views
0

當我運行我的代碼,它返回一個值,爲「空」`爲什麼我的AsyncTask doInBackground()沒有將值傳遞給onPostExecute()?

private class MessageActivityLoaderTask extends AsyncTask<Void, Void, Contentlist> { 
    private LinkedMultiValueMap<String, String> formData; 
    Activity activity; 

    public MessageActivityLoaderTask(Activity activity) { 
     this.activity = activity; 
    } 

    @Override 
    protected void onPreExecute() { 
     formData = new LinkedMultiValueMap<String, String>(); 
     mProgress.setMessage("Please wait.."); 
     mProgress.show(); 
    } 

    @Override 
    protected Contentlist doInBackground(Void... params) { 
     String url = getString(R.string.base_url) + "/example/example1/1"; 
     Contentlist mess = null; 
     try { 
      mess = RestUtils.exchangeFormData(url, HttpMethod.GET, formData, Contentlist.class); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      mProgress.dismiss(); 
     } 
     return mess; 

    } 

    protected void onPostExecute(Contentlist result) { 

     if (result== null) { 
      Toast message = Toast.makeText(ListobjectsActivity.this, "result is empty", Toast.LENGTH_SHORT); 
      message.show(); 
     } else { 
      ListactivityAdapter adapter = new ListactivityAdapter(this.activity, result.getContents()); 

      ListView list = (ListView) activity.findViewById(R.id.account); 
      list.setAdapter(adapter); 
      mProgress.dismiss(); 
     } 

    }` 
+0

你需要檢查你是否正在調用方法exchangeFromData –

+0

你是否有可能發佈exchangeFromData的代碼(0 –

+0

你必須添加缺少的模板參數'... extends AsyncTask {...'使其工作 – k3b

回答

2

AsyncTask看起來是正確設置,所以onPostExecute()將收到doInBackgroun()返回ContentList。由於onPostExecute()正在看到null,因此doInBackground()正在返回空值。這意味着要麼doInBackground()正在發生異常,並且mess從未通過catchRestUtils.exchangeFormData()返回空值而設置爲非空值。

我建議你調試這個區域的代碼,看看到底發生了什麼。這不可能是AsyncTask問題。

相關問題