Android Volley多个请求

时间:2015-06-17 13:57:03

标签: android json android-volley

我尝试在当前的截击请求中执行新的截击请求,但是当调用新请求时,它不会进入onrespond方法。

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

如何成功执行新请求?

MakeGenericMethod

3 个答案:

答案 0 :(得分:2)

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

}

调用片段

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

答案 1 :(得分:0)

所有这些关于

请求优先级

网络呼叫是实时操作,因此,考虑到我们有多个请求,就像您的情况一样,Volley以先进先出的顺序处理从较高优先级到较低优先级的请求。

所以你需要改变优先级(设置Priority.HIGH)来请求你首先想要进程。

这是一段代码

public class CustomPriorityRequest extends JsonObjectRequest {

// default value
    Priority mPriority = Priority.HIGH;

    public CustomPriorityRequest(int method, String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        super(method, url, jsonRequest, listener, errorListener);
    }

    public CustomPriorityRequest(String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        super(url, jsonRequest, listener, errorListener);
    }

    @Override
    public Priority getPriority() {
        return mPriority;
    }

    public void setPriority(Priority p){
        mPriority = p;
    }
}

答案 2 :(得分:0)

正如其他人所说,一种方法是对请求给予高度重视。

另一个选项,因为看起来你有第一个请求取决于try-catch块中包含的内部请求,在我看来你想要为这个特定情况实现同步/阻塞行为。然后你可以使用RequestFuture

df.dropna(inplace=True)