如何使用齐射进行网络操作时显示ProgressDialog [英] How to display the ProgressDialog when do network operations using volley

查看:110
本文介绍了如何使用齐射进行网络操作时显示ProgressDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android应用程序进行Woking,我非常有兴趣使用 volley 库来执行网络http调用。

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

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

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

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+"]");

    }
});

提前致谢。

推荐答案

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

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

这篇关于如何使用齐射进行网络操作时显示ProgressDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆