Volley在没有ProgressDialog的情况下工作,但似乎永远都在使用它

时间:2014-09-25 04:04:35

标签: android progressdialog android-volley

我的应用程序中有一点我需要从Web服务器获取一些状态数据 然后将该数据填充到GridView的项目中。我无法填充GridView 项目,直到我从Web服务器获取数据。我用Volley制作 对服务器的请求。该代码本身工作正常。 我还对我的GridView代码进行了一些测试,以确保单个项目 在提供预期数据时正确填充。我通过硬编码做到了这一点 machines_info with String。

根据我的阅读,我应该向用户展示 在等待Volley响应时不确定进度对话,但是当我 添加ProgressDialog代码,应用程序只停留在" while(machines_info == null)" 环。 ProgressDialog没有显示在屏幕上和Volley的onResponse() 没有被召唤。

我在这里缺少什么?

公共类StatusFragment扩展Fragment {

GridView myGrid;
String machines_info = null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_status, container, false);

    // Instantiate the RequestQuee
    RequestQueue queue = Volley.newRequestQueue(this.getActivity());
    String url ="http://www.myserver.net/get_machines.php";  // NOT the REAL URL

    // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {      
        @Override
        public void onResponse(String response) {
            // Log.i("ONRESPONSE", "Got" + response);
            machines_info = response;
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("onRESPONE", "That didn't work!" + error);
        }
    });
    // Add the request to the RequestQueue.
    queue.add(stringRequest);

            // Show User a progress dialog while waiting for Volley response
    ProgressDialog pd = new ProgressDialog(this.getActivity(), ProgressDialog.STYLE_SPINNER);
    pd.setIndeterminate(true);
    pd.show(this.getActivity(), "PROG_DIALOG", "Getting machine status...");

    while (machines_info == null) {
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
        }
        Log.i("LOOP", "SLEEP");
    }
    pd.dismiss();

            // This works fine if I hardcode machines_info so I think my
            // GridView code is fine. But it never gets executed once I add in
            // the ProgressDialog code because it never comes out of the while loop.
    myGrid = (GridView) rootView.findViewById(R.id.gridView1);
    myGrid.setAdapter(new machineAdapter(machines_info));

    return rootView;
}

}

3 个答案:

答案 0 :(得分:1)

试试这个解决方案:

final GridView myGrid;
String machines_info = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final ProgressDialog pd = new ProgressDialog(this.getActivity(), ProgressDialog.STYLE_SPINNER);
    pd.setIndeterminate(true);
    pd.show(this.getActivity(), "PROG_DIALOG", "Getting machine status...");


    View rootView = inflater.inflate(R.layout.fragment_status, container, false);
    myGrid = (GridView) rootView.findViewById(R.id.gridView1);
    myGrid.setAdapter(new machineAdapter(null));

    // Instantiate the RequestQuee
    RequestQueue queue = Volley.newRequestQueue(this.getActivity());
    String url ="http://www.myserver.net/get_machines.php";  // NOT the REAL URL

    // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {      
        @Override
        public void onResponse(String response) {
            // Log.i("ONRESPONSE", "Got" + response);
            pd.dismiss();
            machines_info = response;
            myGrid.setAdapter(new machineAdapter(machines_info));
            myGrid.invalidate();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("onRESPONE", "That didn't work!" + error);
        }
    });
    // Add the request to the RequestQueue.
    queue.add(stringRequest);
    return rootView;
}

答案 1 :(得分:0)

这段代码,

while (machines_info == null) {
            try {
                Thread.sleep(3000);
            } catch (Exception e) {
            }
            Log.i("LOOP", "SLEEP");
        }

如果machines_info为空,那又怎么样?这将继续一次又一次地调用同一个循环,因为你没有改变machine_info,因为没有办法退出这个循环。


  

避免在主UI线程上使用长时间运行的任务,而是使用 AsyncTask

答案 2 :(得分:0)

为什么 Thread.sleep 。一旦你请求服务器并在 onResponse 回调方法中解除它,请调用 progressdialog 。移动线程并像我说的那样工作。不要使用线程进行抽射或任何耗时任务,因为你不知道什么时候会有回应。