问题描述
我正在尝试通过以下方式取回 JSON 对象:
I'm trying to get back JSON object in the following way:
JSONObject jsonObject = http.makeRequest("GET", "https://api.twitter.com/1.1/search/tweets.json", null);
处理所有HTTP请求的一般方法如下
General method for processing all HTTP requests is following
public void makeRequest(String method, String url, Array params) {
// Request a string response from the provided URL.
JsonObjectRequest jsonObjReq = new JsonObjectRequest(getRequestMethod(method),
url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
processResponse(Constants.Global.SUCCESS, null, response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try {
mStatusCode = error.networkResponse.statusCode;
VolleyLog.d(Constants.Global.ERROR, "Error: " + error.getMessage());
Logger.e(error.getMessage());
} catch (Exception e) {
Logger.e(e.getMessage());
mStatusCode = 0;
}
Logger.e(mStatusCode.toString());
processResponse(Constants.Global.ERROR, mStatusCode, null);
}
});
// Add tag to request for bulk cancelling
//jsonObjReq.setTag()
queue.add(jsonObjReq);
}
处理JSON结果的方法如下:
And method for processing JSON result is following:
private JSONObject processResponse(String resultState, Integer httpStatusCode, JSONObject responseData) {
try {
// First check that result state is error or the success
if (resultState.equals(Constants.Global.SUCCESS)) {
Logger.i("Response is success");
Logger.i(responseData.toString());
//TODO: ADD SUCCESS OBJECT CREATION
}
if (resultState.equals(Constants.Global.ERROR)) {
Logger.e("Response is error");
//TODO: ADD ERROR HANDLING AND ERROR OBJECT CREATION
}
} catch(Exception e) {
e.printStackTrace();
Logger.e(e.getMessage());
}
return responseData;
}
我想问一下如何以异步方式取回 JSONObject(第一个代码片段).
I would like to ask how can i to get back JSONObject (first code snippet) in the async way.
所有请求都使用 Volley 库处理.
All requests are processed using the Volley library.
非常感谢您的建议.
推荐答案
供您评论
我认为异步是由 Volley 自动提供的.所以我需要知道如何将 JSON 数据放入第一个片段
IMO,您可以尝试以下方式,而不是您的第一个代码段(当然,您可以将 JSONArray
请求替换为 JSONObject
请求):
IMO, instead of your first snippet, you can try the following way (of course, you can replace JSONArray
request by JSONObject
request):
VolleyResponseListener listener = new VolleyResponseListener() {
@Override
public void onError(String message) {
// do something...
}
@Override
public void onResponse(Object response) {
// do something...
}
};
makeJsonArrayRequest(context, Request.Method.POST, url, requestBody, listener);
makeJsonArrayRequest
的主体可以如下:
public void makeJsonArrayRequest(Context context, int method, String url, String requestBody, final VolleyResponseListener listener) {
JSONObject jsonRequest = null;
try {
...
if (requestBody != null) {
jsonRequest = new JSONObject(requestBody);
}
...
} catch (JSONException e) {
e.printStackTrace();
}
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(method, url, jsonRequest, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray jsonArray) {
listener.onResponse(jsonArray);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.onError(error.toString());
}
});
// Access the RequestQueue through singleton class.
MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
}
VolleyResponseListener
接口如下:
public interface VolleyResponseListener {
void onError(String message);
void onResponse(Object response);
}
这篇关于Android:如何使用 Volley 从方法中返回异步 JSONObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!