Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Volley multiple Requests

I try to execute a new volley request in the current volley request, but when the new request is called it don't step into the onrespond method.

The new request should be executed before the first ends. (Last in, first out)

How can I execute the new request succesfully ?

private void makeJsonObjectRequest() {
    ac = new AppController();


    final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
            url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.d("test", response.toString());

            try {
                // Parsing json object response
                // response will be a json object


                JSONArray name = response.getJSONArray("data");
                for (int i = 0; i < name.length(); i++) {
                    JSONObject post = (JSONObject) name.getJSONObject(i);

                    try {
                        objectid = post.getString("object_id");

                        newRequest(objectid);

                    }
                    catch (Exception e) {

                   }

                }


            } catch (JSONException e) {
                e.printStackTrace();

            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("test", "Error: " + error.getMessage());
        }
    });

    // Adding request to request queue
    ac.getInstance().addToRequestQueue(jsonObjReq);
}
like image 444
eugngt Avatar asked Oct 19 '22 09:10

eugngt


1 Answers

Try it Work 100%

public class Utility {
String result = "";

String tag_string_req = "string_raq";
private Activity activity;
Context context;
private LinearLayout mLinear;
private ProgressDialog pDialog;
public Utility(Context context) {
    this.context = context;
}

public String getString(String url, final VolleyCallback callback) {
    showpDialog();
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            result = response;
            hideDialog();
            callback.onSuccess(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            callback.onRequestError(error);
            hideDialog();
           /*LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            View layout = inflater.inflate(R.layout.custom_toast, null);
            ((Activity) context).setContentView(layout);*/

        }
    });
    VolleySingleton.getInstance().addToRequestQueue(stringRequest, tag_string_req);
    stringRequest.setRetryPolicy(
            new DefaultRetryPolicy(1 * 1000, 1, 1.0f));
    return result;
}

public interface VolleyCallback {
    void onSuccess(String result);
    void onRequestError(VolleyError errorMessage);
    //void onJsonInvoke(String url, final VolleyCallback callback);
}

public boolean isOnline() {
    Runtime runtime = Runtime.getRuntime();
    try {
        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int exitValue = ipProcess.waitFor();
        return (exitValue == 0);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    return false;
}

private void showpDialog() {
    onProgress();
    if (!pDialog.isShowing())
        pDialog.show();
}

private void hideDialog() {
    if (pDialog.isShowing())
        pDialog.dismiss();
}

public void onProgress() {
    pDialog = new ProgressDialog(context);
    pDialog.setMessage("Please wait...");
    pDialog.setCancelable(false);
    pDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
}

}

Call Fragment

Utility utility = new Utility(getContext());
    utility.getString(urls, new Utility.VolleyCallback() {
        @Override
        public void onSuccess(String result) {
            try {
                JSONObject toplinks = new JSONObject(result);
                JSONObject data  = toplinks.getJSONObject("toplinks");
                M.i("============LS", "" + data);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            finally {

            }
        }
        @Override
        public void onRequestError(VolleyError errorMessage) {
            errorJson.setVisibility(View.VISIBLE);
            String msg = VolleyException.getErrorMessageFromVolleyError(errorMessage);
            errorJson.setText(msg);
        }
    });
like image 156
Rahul Vishwakarma Avatar answered Oct 21 '22 22:10

Rahul Vishwakarma