如何使Volley类成为公共方法 [英] How to make a Volley class a public method
本文介绍了如何使Volley类成为公共方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须在使用 Volley 的多个班级中调用网络服务.那么我应该如何在 onresponse 方法中触发另一个响应类? 我想将syncData作为所有类的通用函数.
I have to call web service in multiple classes I am using Volley. so what should I do in onresponse method to trigger the other class for the response? I want to make syncData as a generic function for my all classes.
public String syncData(Context mContext, String url) {
try {
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
jsonResponse=response.toString();//what I should do here to trigger another class that responds achieved
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try {
Log.e("Response", "error");
// updateForecastUI(isCentigradeType);
} catch (Exception e) {
e.printStackTrace();
}
}
});
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
Constants.MY_SOCKET_TIMEOUT_MS,
Constants.MAX_RETRY,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
jsonObjectRequest.setShouldCache(false);
queue.add(jsonObjectRequest);
} catch (Exception e) {
e.printStackTrace();
}
return jsonResponse;
}
推荐答案
创建接口VolleyResultCallBack
public interface VolleyResultCallBack {
void onVolleyResultListener(String response, String requestUrl);
void onVolleyErrorListener(VolleyError error);
}
使活动实现此接口
您的方法将类似于
public static void syncData(Context mContext, String url,VolleyResultCallBack resultCallBack) {
try {
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
jsonResponse=response.toString();//what I should do here to trigger another class that responds achieved
resultCallBack.onVolleyResultListener(jsonResponse,url);
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try {
Log.e("Response", "error");
resultCallBack.onVolleyErrorListener(error);
// updateForecastUI(isCentigradeType);
} catch (Exception e) {
e.printStackTrace();
}
}
});
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
Constants.MY_SOCKET_TIMEOUT_MS,
Constants.MAX_RETRY,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
jsonObjectRequest.setShouldCache(false);
queue.add(jsonObjectRequest);
} catch (Exception e) {
e.printStackTrace();
}
return jsonResponse;
}
.
在您的活动中发出这样的请求
In your activity make request like this
YourClassName.syncData(this,url,this);
您将在onVolleyResultListener方法中获得响应;
you will get responce in onVolleyResultListener method;
@Override
public void onVolleyResultListener(String response, String requestUrl) {
if(requestUrl.contains(url){ // required to check because there may be multiple requests in same activity
//do something with responce
}
}
onVolleyErrorListener中的处理错误
handle error in onVolleyErrorListener
@Override
public void onVolleyErrorListener(VolleyError error) {
//do something with error ,may be show a toast
}
这篇关于如何使Volley类成为公共方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文