Android Volley onResponse not getting Called in Certain Activity - android

I am making Volley GET Requests which will work until I get to a specific activity and then it stops calling the onResponse in the Response.Listener. Here is my Volley Request method:
public static void stringReqGet(String url, final VolleyCallback callback){
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
System.out.println("In VolleyRequest onResponse(): " + response);
callback.onSuccess(response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error" ,error.getMessage());
}
});
System.out.println("Adding request to queue: " + stringRequest.toString());
Utilities.getInstance().addToRequestQueue(stringRequest);
}
Here's how I'm making the request in the activity in question:
CourseUtil.loadCourse(courseID, new VolleyCallback() {
#Override
public void onSuccess(String response) {
System.out.println("JSON Success");
courseJSON = response;
}
});
and in CourseUtil class:
public static void loadCourse(int courseID, VolleyCallback callback){
VolleyRequest.stringReqGet(Const.URL_COURSE + courseID,callback);
}
I'm getting no Volley errors just seemingly no response. Here are my (truncated) logs:
I/System.out: Adding request to queue: [ ] http://server/courses/267267267 0xebb503b0 NORMAL null
I/System.out: In VolleyRequest onResponse(): *Correct Response Object*
.
.
.
I/System.out: 267267267 <-- Same course ID used in first request
I/System.out: Adding request to queue: [ ] http://server/courses/267267267 0xebb503b0 NORMAL null
*onResponse never called*
The first time this request is made you can see that the onResponse() method is called and it returns the correct object. The signature of this 'correct' request is the exact same as the one above. The next time that exact request is made the onResponse is never called and I get no error.
I've looked at other posts on here but none of their solutions fixed the problem I am having and no error makes it hard to know what's going on.
Any help would be greatly appreciated.

Put:
System.out.println("JSON Success");
courseJSON = response;
into a separate method outside the callback and it works now. Not sure why...

Related

How to disable returning volley network error?

Here is my code to ignore errors:
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError ignored) {
//nothing
}
});
But still it returns 403 error
E/Volley: [934] BasicNetwork.performRequest: Unexpected response code 403 for http://myawesome.site/login
E/Volley: [934] BasicNetwork.performRequest: Unexpected response code 403 for http://myawesome.site/login
Another problem is: above error shows twice in one call. It ignores DefaultRetryPolicy set to 0
Expecting your help.
To stop logging network activity, call the DevicePolicyManager method setNetworkLoggingEnabled() and pass false as the enabled argument.
Your DPC can call isNetworkLoggingEnabled() to check if network activity is currently logged.
reference link :https://developer.android.com/work/dpc/logging.html
check this section : Enable network logging.
I have solve problem by changing the method name. previously i am try to calling web service with POST method and I have changed to GET method then my problem solved. Try this solution by changing request method.
StringRequest stringRequest = new StringRequest(Request.Method.GET, "YOUR_WEBSERVICE_URL",
new Response.Listener<String>() {
#Override
public void onResponse(String response){}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {}
}) {
#Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
return params;
}
};
stringRequest.setRetryPolicy(new
DefaultRetryPolicy(60000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);

How to detect 204 No Content from Android Volley Request

When i request to my api api returns 204 - No Content. But volley does not recognise that and give TimeOutError.
How can i handle this ?
When you setup a new volley request :
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// act upon a valid response
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
Notice that you pass a Response.ErrorListener. When error occurs, such as for instance 204, the onErrorResponse(VolleyError) callback is called with the VolleyError instance - error with appropriate information about the error passed to it.
So in this callback you should inspect for the error and take appropriate action.
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if(error instanceof TimeoutError){
// Take action when timeout happens
}
}
}
NOTE : When timeout happens, the VolleyError instance is in fact an instance of TimeoutError a subclass of VolleyError. Hence we check if the error caused is timeout using instanceof
The list of VolleyError sub classes are available here : http://afzaln.com/volley/com/android/volley/VolleyError.html
The example given is for StringRequest type but the technique is the same for other VolleyObjectRequest types.

How to handle the response of Volley in Android

I use volley in the Android Activity, and make a request and got the response, but I want to handle the response maybe in an another method,but it won't work, what should i do ?
public class TestActivity extends Activity {
RequestQueue queue;
private String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "www.google.com/something/I/need";
queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Do something with the response
Log.i("resp", response);
// I want to do sth with the response out of here
// maybe like this, let result = response
// and see the log at the end of the code
// but it failed, what should I do?
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Handle error
Log.e("error", error.toString());
}
});
queue.add(stringRequest);
Log.e("result", result);
}
The Volley requests are asynchronous, so the program after sending the request, continues execution without waiting for the answer. So the code that processes the result is inserted into the OnResponse method. For more precise help explain why you would like to log out of the method OnResponse
Think about what you're doing: You're creating a StringRequest, then you add it to the request queue, but then you immediately try to check the result. Obviously, this won't work because the request hasn't been processed yet.
Your response will arrive in the onResponse method, and only then you'll be able to do something with it. You can set result = response here, but you'll only be able to see the value when the onResponse is called which could take some time.
Hope this clarifies things.

What is the best way to coordinate Volley requests

I'm trying to use Volley to execute multiple HTTP request where each one of them relies on the result of the previous one, what is the best option as a design?
1-Firing the next request in the onResponse callback of the previous request?
2-Writing some coordinator class that have callbacks that get called in the onResponse method of a request and fires the next request
skeleton code for the second option
coodrinator = new Coordinator();
JsonObjectRequest firstRequest = new JSONObjectRequest(Request.Method.GET,firstURL),new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//handle the responsee
coordinator.onFirstRequestRecieved();
}
},
errorListener);
private void doSecondRequest(){
JsonObjectRequest secondRequest = new JSONObjectRequest(Request.Method.GET,secondURL),new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//handle the responsee
coordinator.onSecondRequestRecieved();
}
},
errorListener);
}
private class Coordinator{
public void onFirstReequestRecieved(){
doSecondRequest();
}
public void onSecondRequestRecieved(){
//do Something
}
}
If first request response parameters are needed for making second request and so on then you can go for synchronous way. That can be achieved by making second request in onResponse on First request can there is no good or bad practise for it.
The thing is volley is asynchronous and request what is added in the queue execute without depending on other request and we are going to make it synchronous request and it can be achieved by many ways seeing your requirement.

How to make a synchronous GET request with volley library? And how to parse it afterwards?

I've seen answer to both of these questions, however, when I tried to put them together, I couldn't make it work. The problem itself is pretty simple: I want to get a string from one site and use it in a post request. That means I can only make the post request after I've finished parsing the GET request. The main ideas I'm using are these ones:
How to return response header field to main method using Google Volley for HTTP GET request in Android / Java?
Can I do a synchronous request with volley?
However the synchronous request is blocked and doesn't go on, and the first one is Async.
I believe this to be a simple thing to do, but still, I haven't be able to do it...
Thanks for any help!
Why not do something like this:
// send first request
requestQueue.add(firstRequest, null, new Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// ** code to parse response **
// send second request
requestQueue.add(secondRequest, null, new Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// ** code to parse response **
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// ** code to handle errors **
}
}));
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// ** code to handle errors **
}
}));

Categories

Resources