如何为跨类 Volley 方法调用创建合适的 Volley Listener [英] How to create a proper Volley Listener for cross class Volley method calling
问题描述
我的目标是从另一个类中调用 Volley,一种非常简洁、模块化的方式,即:
I aim to call Volley from another class in, a very succinct, modular way ie:
VolleyListener newListener = new VolleyListener();
VolleySingleton.getsInstance().somePostRequestReturningString(getApplicationContext(), newListener);
JSONObject data = newListener.getResponse();
但是在让侦听器部分工作以便能够从诸如
But am having allot of trouble getting the listener portion to work so as to be able to access the resulting data from a method such as
newListener.getResponse();
这个站点上有几个问题大致概述了如何从另一个类设置截击呼叫,例如:Android Volley - 如何隔离另一个类中的请求.我已经成功地使方法调用起作用,但现在将该数据放入当前类以供使用却造成了麻烦.
There are a few questions on this site that generally outline how to set up a volley call from another class, such as: Android Volley - How to isolate requests in another class. I have had success getting the method call to work, but to now get that data into the present class for usage has caused trouble.
我的 VolleySingleton
类中的操作为:
I have the action within my VolleySingleton
class as:
public void somePostRequestReturningString(final Context context,final VolleyListener<String> listener) {
final String URL = "http://httpbin.org/ip";
JsonObjectRequest set = new JsonObjectRequest(Request.Method.GET, URL, ((String) null),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
listener.outPut = response.toString();
//Toast.makeText(context, response.toString(), Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
}
}
);
mRequestQueue.add(set);
}
在监听器类中:
public class VolleyListener {
public static String outPut;
private static Response.Listener<String> createSuccessListener() {
return new Response.Listener<String>() {
@Override
public void onResponse(String response) {
outPut = response;
}
};
}
}
如何配置它以使其工作并允许从另一个类中进行 Volley 调用和数据检索,尤其是如何正确构建回调?
How can I configure this to work and allow Volley calls and data retrieval from another class, particularly how to build callbacks correctly?
推荐答案
根据你的需求,建议你参考我下面的解决方案,希望对你有帮助:
For your requirement, I suggest you refer to my following solution, hope it's clear and helpful:
首先是界面:
public interface VolleyResponseListener {
void onError(String message);
void onResponse(Object response);
}
然后在您的助手类中(我将其命名为 VolleyUtils
类):
Then inside your helper class (I name it VolleyUtils
class):
public static void makeJsonObjectRequest(Context context, String url, final VolleyResponseListener listener) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
listener.onResponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.onError(error.toString());
}
}) {
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
};
// Access the RequestQueue through singleton class.
VolleySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest);
}
然后,在您的 Activity 类中,您可以像下面这样调用:
Then, inside your Activity classes, you can call like the following:
VolleyUtils.makeJsonObjectRequest(mContext, url, new VolleyResponseListener() {
@Override
public void onError(String message) {
}
@Override
public void onResponse(Object response) {
}
});
您可以参考以下问题了解更多信息(正如我昨天告诉您的):
You can refer to the following questions for more information (as I told you yesterday):
Android:如何从方法返回异步 JSONObject使用 Volley?
POST 请求 Json 文件传递字符串并等待响应 Volley
这篇关于如何为跨类 Volley 方法调用创建合适的 Volley Listener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!