Android Volley 多个请求

标签 android json android-volley

我尝试在当前的 volley 请求中执行一个新的 volley 请求,但是当调用新请求时它没有进入 onrespond 方法。

新请求应该在第一个请求结束之前执行。 (后进先出)

如何成功执行新请求?

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

最佳答案

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

调用 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);
        }
    });

关于Android Volley 多个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30893775/

相关文章:

javascript - 我可以对 PHP 数组进行 json_encode 并将值作为 JS RegExp 对象获取吗?

ios - MultipeerConnectivity MCPeerID 不符合 Codable

java - Android Firebase REST 问题

java - Android,Volley Request,响应阻塞主线程

android - 批量更新会触发多少次 onUpdate 函数?

java - 异步任务流程

javascript - 如何根据边的数组对象内的数据选择边?

php - 在 php 中转换 url 会导致 Volley 错误

javascript - Android主线程阻塞WebView线程

android - 您上传的 APK 或 Android App Bundle 具有带有 Intent 过滤器的 Activity 、 Activity 别名、服务或广播接收器,但没有“an”