2013-01-20 72 views
0

我在AsyncTask类中未达到onPostExecuteAsyncTask onPostExecute不输出

我调用任务这种方式,onClick里面一个对话框:

new HelpfulTask().execute(passing); 

注:当我将鼠标悬停在上面的代码中,我得到一个警告:

的通用阵列ArrayList是为参数varargs 创建的。

我不确定这是什么意思,如果这阻止我的任务甚至运行?

这里是任务代码:

protected class UnHelpfulTask extends 
      AsyncTask<ArrayList<String>, Void, ArrayList<String>> { 

     ArrayList<String> passed; 

     protected ArrayList<String> doInBackground(ArrayList<String>... passing) { 
      passed = passing[0]; 

      String url_select = "http://www.---.com/---/bad.php"; 

      ArrayList<NameValuePair> param = new ArrayList<NameValuePair>(); 
      param.add(new BasicNameValuePair("item", passed.get(0))); 
      param.add(new BasicNameValuePair("text", passed.get(1))); 
      param.add(new BasicNameValuePair("category", passed.get(2))); 

      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url_select); 

      try { 
       httpPost.setEntity(new UrlEncodedFormEntity(param)); 
       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 

       // read content 
       is = httpEntity.getContent(); 

      } catch (Exception e) { 

       Log.e("log_tag", "Error in http connection " + e.toString()); 
      } 

      return null; 
     } 

     InputStream is = null; 
     String result = ""; 

     protected void onPostExecute(Void v) { 


      Toast.makeText(getContext(), "You have voted this down!", 
        Toast.LENGTH_SHORT).show(); 

     } 

    } 

我得到上运行,并在onPostExecute没有错误,敬酒从不显露。 (注意:这是ArrayAdapter.里面的一个内部类)我还加了一杯烤面包,看看我的ArrayList是否正确展开,并将其放在onPostExecute中,但它从未显示过。

如何测试以查看此任务是否正在运行?

+0

你的意思是'UnHelpfulTask​​'? – KhalidTaha

回答

2

变化

protected void onPostExecute(Void v) { 

所以它是

protected void onPostExecute(ArrayList<String> arr) 

或者改变你的AsyncTask声明,以便它

extends AsyncTask<ArrayList<String>, Void, Void> 

哪个是更好的主意,因为你在doInBackground()返回null。你没有得到祝酒的原因是因为你正在制定一个不同于AsyncTask指定的方法。这就是为什么我们通常使用@Override注释 - 它会强制检查实际的覆盖,您需要在像AsyncTasks这样的类中进行检查。

+0

我去了第二个选项,它的工作。这是我第一次将* arg传递给'AsyncTask',我不知道如何设置它。谢谢! – KickingLettuce

+0

@KickingLettuce没问题:) –