在Android Volley中处理多个请求 [英] Handle Multiple request in Android volley

查看:206
本文介绍了在Android Volley中处理多个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Volley来处理多个请求,并且我正在获得所有请求的响应.我的问题是如何识别响应属于哪个API.

I am trying to hit multiple request using Volley and i am getting response for all the request. my problem is how to identify the response is belong to which API.

mQueue = CustomVolleyRequest.getInstance(this.getApplicationContext())
            .getRequestQueue();

    final CustomJSONObjectrequest jsonRequest = new CustomJSONObjectrequest(Request.Method
            .GET, url,
            new JSONObject(), this, this); // 
    jsonRequest.setTag(REQUEST_TAG);

    final CustomJSONObjectrequest jsonRequest2 = new CustomJSONObjectrequest(Request.Method
            .GET, url2,
            new JSONObject(), this, this);
    jsonRequest2.setTag(REQUEST_TAG);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mQueue.add(jsonRequest);
            mQueue.add(jsonRequest2); // Both the request will have different API request
        }
    });
}
@Override
public void onErrorResponse(VolleyError error) {
    mTextView.setText(error.getMessage());
}

@Override
public void onResponse(Object response) {

    // How to identify, which response is belong to which api request
    mTextView.setText("Response is: " + response);

}

推荐答案

您可以对单个请求执行类似的操作.相同的内容可以应用于第二个请求.这样您就知道哪个请求正在给您答复.

You can do something like this for a single request. Same can be applied to the second request. This way you know which request is giving you the response.

final CustomJSONObjectrequest jsonRequest = new CustomJSONObjectrequest(Request.Method
        .GET, url,
        new JSONObject(), this,  new Response.Listener<Object>() {
                @Override
            public void onResponse(Object response) {

// How to identify, which response is belong to which api request
mTextView.setText("Response is: " + response);

});

您可以从创建像这样的界面开始:

You can start with making an interface like :

public interface VolleyResponse {

void onResponse(JSONObject object, String tag);

void onError(VolleyError error, String tag);
}

然后,您可以为截击请求创建自定义处理程序,例如:

Then you can make a custom handler for volley request like:

public class CustomJSONObjectRequest implements Response.Listener<JSONObject>, Response.ErrorListener {

private VolleyResponse volleyResponse;
private String tag;
private JsonObjectRequest jsonObjectRequest;


public CustomJSONObjectRequest(int method, String url, JSONObject jsonObject, String tag, VolleyResponse volleyResponse) {
    this.volleyResponse = volleyResponse;
    this.tag= tag;
    jsonObjectRequest = new JsonObjectRequest(method, url, jsonObject, this, this);
}

@Override
public void onResponse(JSONObject response) {
    volleyResponse.onResponse(response, tag);
}

@Override
public void onErrorResponse(VolleyError error) {
    volleyResponse.onError(error, tag);
}

public JsonObjectRequest getJsonObjectRequest() {
    return jsonObjectRequest;
}
}

并在您的班级中调用它,就像这样:

And to call it in your class use it like:

 CustomJSONObjectRequest request1 = new CustomJSONObjectRequest(Request.Method.GET, url,
            new JSONObject(), "YOUR REQUEST TAG", this);

确保让您的类实现VolleyResponse接口,该接口将为您提供响应和标签.

Make sure to let your class implement the VolleyResponse interface that will get you the response and your tag.

@Override
public void onResponse(JSONObject object, String tag) {
    Log.i("Response :", object.toString() + "   " + tag);
}

@Override
public void onError(VolleyError error, String tag) {

}

要将请求添加到排球队列,可以使用:

To add the request to the volley queue you can use:

mQueue.add(request1.getJsonObjectRequest());

PS:此代码未经测试,但可以正常工作.

PS : this code is not tested but it should work.

这篇关于在Android Volley中处理多个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆