Marlonez
Marlonez

Reputation: 45

Volley works without ProgressDialog, but seems to take forever with it

There's a point in my app where I need to get some status data from the web server and then populate that data into items of a GridView. I can't populate the GridView items until I get the data from the web server. I'm using Volley to make the request to the server. That code is working fine by itself. I also did some testing of my GridView code to make sure the individual items are populated correctly when expected data is supplied. I did this by hardcoding machines_info with a String.

From what I've read, I should show the user an indeterminate progress dialog while waiting for the Volley response, but when I add in the ProgressDialog code the app just stays in the "while (machines_info == null)" loop. The ProgressDialog doesn't show on the screen and Volley's onResponse() doesn't get called.

What am I missing here ?

public class StatusFragment extends 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;
}

}

Upvotes: 1

Views: 4312

Answers (3)

Ruban
Ruban

Reputation: 1534

Why Thread.sleep. instead call progressdialog once you request the server and dismiss it in onResponse callback method.Remove thread and do like i said it will work.dont use thread for volley or any time consuming task because you don't know when response will come.

Upvotes: 0

mmlooloo
mmlooloo

Reputation: 18977

try this solution:

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

Upvotes: 1

nobalG
nobalG

Reputation: 4620

This piece of code,

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

what if machines_info is null,then what?This will keep on calling the same loop again and again because there is no way coming out of this loop as you are not changing machine_info.


Avoid working with long running tasks on the main UI thread,Use AsyncTask instead

Upvotes: 0

Related Questions