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);
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);
}
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;
}
I would like to ask how can i to get back JSONObject (first code snippet) in the async way.
All requests are processed using the Volley library.
Many thanks for any advice.
with data fetched asynchronously from the network.
) – Dogberry