Java 使用volley进行网络操作时如何显示ProgressDialog

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23962339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:26:48  来源:igfitidea点击:

How to display the ProgressDialog when do network operations using volley

javaandroidandroid-volley

提问by N Sharma

I am Woking on an android application where I am very interested to use volleylibrary to perform the network http calls.

我正在使用一个 android 应用程序,我对使用volley库来执行网络 http 调用非常感兴趣。

But my question I found that this library do operations in different background thread then How I can showProgressDialogwhen http request start to execute then later dismiss it once it has executed.

但是我的问题是,我发现这个库在不同的后台线程中执行操作,然后如何显示ProgressDialoghttp 请求何时开始执行,然后在执行后将其关闭。

RequestQueue rq = Volley.newRequestQueue(this);
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
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        System.out.println("Error ["+error+"]");

    }
});

Thanks in advance.

提前致谢。

采纳答案by Hungry Coder

It's pretty straight forward. Start the progress dialog once you add the request object in the queue.

这很直接。在队列中添加请求对象后启动进度对话框。

//add the request to the queue
rq.add(request);

//initialize the progress dialog and show it
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Fetching The File....");
progressDialog.show();

Then dismiss the dialog once you have received the response from the server.

然后在收到服务器的响应后关闭对话框。

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();
    }
});