排球JsonObjectRequest响应按顺序进行

时间:2018-11-25 16:31:43

标签: android android-studio android-volley

我正在同时发送几个请求,并且按照请求顺序收到了响应。

我如何更改它以在收到他后立即收到回复?

发送(1,2,3,4) 收到(1,2,3,4)

示例: 同时发送所有请求(可以在日志中查看)

仅在收到第一个请求响应后,我才会获得第二个响应;

这是我的服务器请求类:

公共类ServerRequest {

public static ServerRequest instance;
public RequestQueue requestQueue;
private JsonObjectRequest jsonObjectRequest;

private ServerRequest(Context context) {
 requestQueue = Volley.newRequestQueue(context.getApplicationContext());
}


public static synchronized  ServerRequest getInstance(Context context) {
    if (instance == null) {
        instance = new ServerRequest(context);
    }
    return instance;
}

/**
 * this function  create server response
 *
 * @param method          the server request method (Request.Method.POST)
 * @param timeOutInterval the time for time out from the volley request
 * @param baseUrl         the url for the call
 * @param params          the Json object for the call
 * @param listener        success response listner
 * @param errListener     error response listner
 */
public void jsonRequest(int method, Integer timeOutInterval, String baseUrl, JSONObject params, Response.Listener<JSONObject> listener, Response.ErrorListener errListener) {
    jsonObjectRequest = new JsonObjectRequest(method, baseUrl, params, listener, errListener);
    RetryPolicy policy = new DefaultRetryPolicy(timeOutInterval, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    jsonObjectRequest.setRetryPolicy(policy);
    requestQueue.add(jsonObjectRequest);

}

/**
 * this function  create server response (with tag)
 *
 * @param TAG             tag for the server call
 * @param method          the server request method (Request.Method.POST)
 * @param timeOutInterval the time for time out from the volley request
 * @param baseUrl         the url for the call
 * @param params          the Json object for the call
 * @param listener        success response listner
 * @param errListener     error response listner
 */
public void jsonRequest(String TAG, int method, Integer timeOutInterval, String baseUrl, JSONObject params, Response.Listener<JSONObject> listener, Response.ErrorListener errListener) {
    jsonObjectRequest = new JsonObjectRequest(method, baseUrl, params, listener, errListener);
    RetryPolicy policy = new DefaultRetryPolicy(timeOutInterval, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    jsonObjectRequest.setRetryPolicy(policy);
    jsonObjectRequest.setTag(TAG);
    requestQueue.add(jsonObjectRequest);
}

/**
 * this function  create server response (witout params)
 *
 * @param method          the server request method (Request.Method.POST)
 * @param timeOutInterval the time for time out from the volley request
 * @param baseUrl         the url for the call
 * @param listener        success response listner
 * @param errListener     error response listner
 */
public void stringRequest(int method, Integer timeOutInterval, String baseUrl, Response.Listener<String> listener, Response.ErrorListener errListener) {
    StringRequest stringRequest = new StringRequest(method, baseUrl, listener, errListener);
    RetryPolicy policy = new DefaultRetryPolicy(timeOutInterval, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    stringRequest.setRetryPolicy(policy);
    requestQueue.add(stringRequest);
}

}

0 个答案:

没有答案