2016-11-21 82 views
-3

我使用的是AsyncTaskVolley调用加载数据后加载。并且还编码以显示ProgressDialog。但问题是onPreExecuteonPostExecute如此之快,ProgressDialog不会出现,目前仍是项目是不可见的几秒钟后回收站项目将onPostExecute

+4

*我正在使用的AsyncTask内凌空通话* ...为什么加载数据里面你的对话? volley已经是一个异步库...请学习多线程中的基本流程...... *但问题是onPreExecute和onPostExecute如此之快,以至于ProgressDialog不会出现* ...我很肯定你在任何这个方法调用......让我猜测:'new Task()。execute(); dialog.dismiss();'或'中doInBackground' – Selvin

+0

异步调用,请提供您的代码? –

+0

不,我解雇了下onPostExecute –

回答

1

原因的执行你的AsyncTask看起来非常快(速度甚至比你得到的结果是你的请求)可能是通过使用凌空内的AsyncTask您使用凌空的线程使网络请求而不是异步任务线程。

这将使它看起来像真快执行的异步任务的时候,其实你只是传递到另一个线程(凌空的线程),这样的AsyncTask已完成工作,你还有什么工作,然后凌空完成它的工作,你会得到结果。

解决方案:

要么使用Android抽射或使用AsyncTask

正如你可以在volley training docs看有没有需要AsyncTask

您还可以看到this SO AsyncTask example,确认自doInBackgound()运行中另一个线程,就没有必要使用抽射或其他异步方法

更新来回答这个问题的意见:

如何使用ProgressDialog与凌空?

其实很容易,因为在this other说得这么张贴例如

你只需要两个步骤:

  1. 您启动ProgressDialog因为你把你的凌空请求队列 ;)

    //请求添加到队列 rq.add(请求);

    //initialize the progress dialog and show it 
    progressDialog = new ProgressDialog(getActivity()); 
    progressDialog.setMessage("Fetching The File...."); 
    progressDialog.show(); 
    
  2. 你放弃你的OnResponse()

    StringRequest postReq = new StringRequest(Request.Method.POST, "http://httpbin.org/post", new Response.Listener<String>() 
    { 
    
        @Override 
        public void onResponse(String response) { 
         tv.setText(response); // We set the response data in the TextView 
         progressDialog.dismiss(); 
        } 
    }, 
    
    new Response.ErrorListener() { 
    
         @Override 
         public void onErrorResponse(VolleyError error) { 
          Log.e(“Volly Error”,”Error: ”+error.getLocalizedMessage()); 
          progressDialog.dismiss(); 
         } 
    }); 
    
+0

感谢您的回答,现在我想使用凌空通话我没有选择。所以,如果我这样做,那么我将如何显示我自己的ProgressDialog –

+0

@AkshayDusane我将更新答案 – HenriqueMS

+0

@AkshayDusane检查更新的答案 – HenriqueMS