gpt4 book ai didi

android - 在 Android volley 中处理多个请求

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:34 25 4
gpt4 key购买 nike

我正在尝试使用 Volley 发送多个请求,并且我收到了所有请求的响应。我的问题是如何识别响应属于哪个 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);

}

最佳答案

创建一个Generic Volley 类和一个Interface,使用该接口(interface)获取成功和失败的响应。

  • 第 1 步创建一个单独的 Volley 类
  • 第 2 步创建用于访问 volley 类响应的接口(interface)
  • 第 3 步为创建新对象类并发送所需的参数

new PostVolleyJsonRequest(TestVolley.this, TestVolley.this(interfcae), "Submit", url, params);

  1. 类(class)背景
  2. 发送成功和失败响应的接口(interface)
  3. 成功时识别的请求类型
  4. 网址(必填)
  5. 参数(可选)GET 不需要

通用截击类

public class PostVolleyJsonRequest {
private String type;
private Activity act;
private VolleyJsonRespondsListener volleyJsonRespondsListener;
private String networkurl;
private JSONObject jsonObject = null;
private JSONObject params;


public PostVolleyJsonRequest(Activity act, VolleyJsonRespondsListener volleyJsonRespondsListener, String type, String netnetworkUrl,JSONObject params) {
this.act = act;
this.volleyJsonRespondsListener = volleyJsonRespondsListener;
this.type = type;
this.networkurl = netnetworkUrl;
this.params = params;
sendRequest();
}

private void sendRequest() {

Log.d("url", "url" + networkurl);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,networkurl,params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("response", "response " + response);
volleyJsonRespondsListener.onSuccessJson(response, type);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try {
NetworkResponse response = error.networkResponse;
Log.e("response", "response " + response);
if (response != null) {
int code = response.statusCode;

String errorMsg = new String(response.data);
Log.e("response", "response" + errorMsg);
try {
jsonObject = new JSONObject(errorMsg);
} catch (JSONException e) {
e.printStackTrace();
}
String msg = jsonObject.optString("message");
volleyJsonRespondsListener.onFailureJson(code, msg);
} else {
String errorMsg = error.getMessage();
volleyJsonRespondsListener.onFailureJson(0, errorMsg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});

jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(
600000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

RequestQueue requestqueue = Volley.newRequestQueue(act);
requestqueue.add(jsObjRequest);

}

使用接口(interface)获取响应消息

public interface VolleyJsonRespondsListener {

public void onSuccessJson(JSONObject result, String type);
public void onFailureJson(int responseCode, String responseMessage);

在你想要包含多个请求的类中

public class TestVolley extends AppCompatActivity implements VolleyJsonRespondsListener{

//Your class code goes here


//network request

try {
//parameters
//Context,Interface,Type(to indentify your responds),URL,parameter for your request

//request 1
new PostVolleyJsonRequest(TestVolley.this, TestVolley.this, "Submit", url, params);

//request 2
new PostVolleyJsonRequest(TestVolley.this, TestVolley.this, "AccessData", url_2, params_2);




} catch (Exception e) {

e.printStackTrace()
}

//Methods from Interface

@Override
public void onSuccessJson(JSONObject result, String type) {

//Based on the Type you send get the responds and parse it
switch (type) {
case "Submit":
try {
parseSubmit(result);
} catch (Exception e) {
e.printStackTrace();
}
break;

case "AccessData":
try {
parseAccessData(result);
} catch (Exception e) {
e.printStackTrace();
}
break;
}

关于android - 在 Android volley 中处理多个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45212853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com