在我的应用程序中,我需要按顺序发送多个截击请求。 我已经创建了一个用于处理凌空响应的公共监听器。
public interface RequestCallBack {
void onSuccess(JSONObject jsonObject, String tag)
void OnError(String message);
}
使用方法注册此回调:
public void setOnResponseListener (RequestCallBack onResponseListener) {
this.onResponseListener = onResponseListener;
}
我已经创建了一个常用方法,可以处理凌空请求。
public void getResponse(String tag, String url) {
JsonObjectRequest jsonObjectRequest;
try {
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
url,null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
mStatusCode = response.optInt("status_code");
mBody = response.optJSONObject("body");
if (mStatusCode != 0 && mStatusCode == 201) {
onResponseListener.onSuccess(mBody, (String) jsonObjectRequest.getTag());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
onResponseListener.OnError(displayVolleyError(error));
}
}) {
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
volleyError = new VolleyError(new String(volleyError.networkResponse.data));
}
return volleyError;
}
};
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
REQUEST_TIMEOUT_MS,
NO_OF_RETRY_ATTEMPTS,
BACK_OF_MULTIPLIER));
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjectRequest, tag);
} catch (Exception e) {
e.printStackTrace();
}
}
我从类中调用了这个方法:
classObject.getResponse("request1", firstUrl);
classObject.getResponse("request2", secondUrl);
我已经覆盖了RequestCallBack接口方法。 但是每次返回第二个请求的标记时,在 onSuccess 方法内。
@Override
public void onSuccess(JSONObject jsonObject, String tag) {
Log.d("Class", "tag: "+tag); // Always returns the "request2"
// Will check service response according to tag
// but didn't get the right tag.
}
@Override
public void OnError(String message) {
}
有人可以在这里建议我如何解决这个问题。
提前致谢。
答案 0 :(得分:4)
申请类
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
/**
* Created by Preetika on 7/4/2016.
*/
public class App extends Application {
private static App mInstance;
public static final String TAG = App.class
.getSimpleName();
private RequestQueue mRequestQueue;
public App() {
}
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized App getmInstance(){return mInstance;}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(com.android.volley.Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
}
创建一个枚举因此,您可以在下达任何请求时设置任何值。
public static enum SERVICE_TYPE{
//set enums here for example you are hitting request for login
LOGIN
}
我创建了一个在服务器上发送请求的公共类。
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import com.acadgild.android.volley.App;
import com.acadgild.android.volley.utils.CommonUtilities;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.StringRequest;
import java.util.Map;
/**
* Created by Preetika on 6/17/2016.
*/
public class CallAddrVolley extends AsyncTask<Void, Void, Void> {
private static String TAG= "CallAddr";
Context context;
Map<String, String> paramss;
OnWebServiceResult resultListener;
CommonUtilities.SERVICE_TYPE Servicetype;
String url;
int method;
private String tag_json_obj = "jobj_req";
public CallAddrVolley(Context context, Map<String, String> params, int method, String url, CommonUtilities.SERVICE_TYPE Servicetype, OnWebServiceResult resultListener){
this.context= context;
this.paramss = params;
this.url= url;
this.resultListener= resultListener;
this.Servicetype= Servicetype;
this.method= method;
Log.e("size", "size= "+ paramss.size());
}
@Override
protected Void doInBackground(Void... params) {
/* JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
resultListener.getWebResponse(response.toString(), Servicetype);
}catch (Exception e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
}) {
*//**
* Passing some request headers
* *//*
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
@Override
protected Map<String, String> getParams() {
Log.e("params", "params= "+ paramss.size());
Log.e("params", "params= "+ paramss.get(Constants.USER_ID));
return paramss;
}
};*/
StringRequest myReq = new StringRequest(method,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e(TAG, response.toString());
try {
resultListener.getWebResponse(response.toString(), Servicetype);
}catch (Exception e){
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
}) {
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
Log.e("params", "params= "+ paramss.size());
Log.e(TAG, "Url= "+ url+ paramss.toString());
return paramss;
};
};
// Adding request to request queue
App.getmInstance().addToRequestQueue(myReq,
tag_json_obj);
return null;
}
}
创建一个接口,你必须在那些你想要命中的类中包含这些接口,以便你可以得到结果,并在CommonUtilities.SERVICE_TYPE
的帮助下,你会发现结果来自服务
import com.acadgild.android.volley.utils.CommonUtilities;
/**
* @author Preetika
*
*/
public interface OnWebServiceResult {
public void getWebResponse(String result, CommonUtilities.SERVICE_TYPE type);
}
尝试一下我在我的项目中使用这种方法,它对我很有用......如果需要任何帮助,请告诉我....