【问题标题】:AsyncTask - onPostExecute invoked earlyAsyncTask - onPostExecute 提前调用
【发布时间】:2014-08-05 17:51:39
【问题描述】:

这是我第一次使用 AsyncTask,所以如果错误非常愚蠢,我会道歉......

这是我的课:

class HttpAsyncTask extends AsyncTask<String, Void, String> 
{
    @Override
    protected String doInBackground(String... urls) 
    {
        return null;
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) 
    {       
         Log.i("ASYNC", "size: "+todo.size());
         displayListView();
    }

    public String POST(String url)
    {
        InputStream inputStream = null;
        String result = "";
        try 
        {
            HttpClient httpclient = new DefaultHttpClient();
            //make POST request to the given URL
            HttpPost httpPost = new HttpPost(url);
           // pass parameters in this way 

            for(int i=0;i<preguntas.length;i++)
            {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("id",String.valueOf(preguntas[i])));
                nameValuePairs.add(new BasicNameValuePair("te",tablas[i]));

                //add data
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // 8. Execute POST request to the given URL
                HttpResponse httpResponse = httpclient.execute(httpPost);

                // 9. receive response as inputStream
                inputStream = httpResponse.getEntity().getContent();

                // 10. convert inputstream to string
                if(inputStream != null)
                {
                    result = convertInputStreamToString(inputStream);
                    NumPregTem todoAux = new NumPregTem();
                    todoAux.setBBDD(preguntas[i]) ;
                    todoAux.setTema(tablas[i]);

                    String[] aux = result.split(";");   
                    todoAux.setPreg(aux[0]);
                    todoAux.setRespA(aux[1]);
                    todoAux.setRespB(aux[2]);
                    todoAux.setRespC(aux[3]);
                    todoAux.setRespD(aux[4]);
                    todoAux.setRespV(aux[5]);   
                    todo.add(todoAux);
                }
                else
                {
                    result = "Did not work!";
                }

            }
           Log.i("ASYNC", "i've finished to query");
        } 

        catch (Exception e) 
        {
                Log.d("InputStream", e.getLocalizedMessage());
        }
        return result;
    }
}

我的问题是在调试器中我看到消息 Log.i("ASYNC", "size: "+todo.size());在消息 Log.i("ASYNC", "i've finished to query"); 之前的 onPostExecute() 中在 POST 中,当我在主类(调用 displayListView() )中使用 todo.get(i) 时,此对象为空。

谢谢!

我想在异步任务完成时调用 displayListView()

PD:我在这个函数中调用 POST(在我的主类中)

HttpAsyncTask httpAsyncTask = new HttpAsyncTask();
httpAsyncTask.execute("http://appdomain.hol.es/webService.php");

【问题讨论】:

  • 我没有看到你的 POST 方法在任何地方被调用
  • 我已经编辑了 posthttpAsyncTask.execute,在我的主类中你调用了调用的函数,当异步任务完成后你想调用 displayLstView()
  • 您正在做的是将参数传递给您的任务。您的 POST() 方法仍未被调用。查看答案。你的任务实际上并没有做任何事情
  • 谢谢,@SimonSays 的回答是对的
  • @Droidman 很抱歉提出这个问题作为评论,您是否可以使用一些链接,例如在 AsyncTask 运行时放置一个显示下载状态的栏?该栏进入 POST 方法?

标签: android android-asynctask


【解决方案1】:

您可能想像这样从doInBackground() 调用POST() 方法

@Override
protected String doInBackground(String... urls) 
{
    return POST(myUrl);
}

【讨论】:

  • 方法post在其他函数中被调用,我希望异步任务完成它调用disPlayListView()
  • 你是正确的@SimonSays,谢谢,我写了这个:覆盖受保护的字符串doInBackground(字符串... urls){返回POST(“appdomain.hol.es/webService.php");}覆盖受保护的无效onPostExecute(字符串结果) { displayListView(); }
  • 很抱歉作为评论提出这个问题,您是否可以使用一些链接,例如在 AsyncTask 运行时放置一个显示下载状态的栏?该栏位于 POST 方法中?
  • 这里有很多关于这个主题的问题。搜索“异步任务进度”。这是一个例子:stackoverflow.com/questions/18069678/…
【解决方案2】:

好吧,粗略观察... doInBackground() 方法是异步调用的代码。当该方法完成时,它会调用 onPostExecute() 方法。现在,doInBackground() 只是返回 null,它会立即调用 onPostExecute()。

我不确定您何时调用 POST 方法,但如果您希望它在 displayListView() 之前执行,则将其放入 doInBackground() 方法中。

【讨论】:

  • 我希望方法 displayListView() 在异步任务完成时运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-07
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
相关资源
最近更新 更多