How to return the response header field to the main method using Google Volley for HTTP GET request in Android / Java?

I use google volley to work in android. I will make a http GET request and should return the response header value. I found answers to the stack overflow in order to access the header fields, but I don’t know how to return it to my place. Please take a look at my code, I put four digits to explain my problem.

In (1), I can print out the value I need. Than I tried to save it in the class attribute (2), and there is no error in the IDE. If I want to return it from there (3), I got a NullPointerException in (4). Maybe this is read before the write problem. So how can I get the value from (1) to (4)? Thank you very much!

public class Login { String phpsessid = null; public Login() {} public String getSessionId(Context context) { RequestQueue queue = Volley.newRequestQueue(context); StringRequest sr = new StringRequest(Request.Method.GET, "any url", new Response.Listener<String>() { @Override public void onResponse(String response) { System.out.println(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) { @Override protected Response<String> parseNetworkResponse(NetworkResponse response) { System.out.println(response.headers.get("Set-Cookie")); (1) phpsessid = response.headers.get("Set-Cookie"); (2) return super.parseNetworkResponse(response); } }; queue.add(sr); return phpsessid; (3) } 

}

mostly:

 Login login = new Login(); String result = login.getSessionId(this.getContext); System.out.println(result); (4) 
+6
source share
1 answer

When you add a request to the queue, this request is executed asynchronously. This means that it is not executed in the order in which you read it, it happens in another thread and will eventually return.

 protected Response<String> parseNetworkResponse(NetworkResponse response) { System.out.println(response.headers.get("Set-Cookie")); (1) phpsessid = response.headers.get("Set-Cookie"); (2) return super.parseNetworkResponse(response); } 

This returns the body of the response - from what I read in your code, it looks like you want to return the value of the "Set-Cookie" header. You can do it as follows:

 protected Response<String> parseNetworkResponse(NetworkResponse networkResponse) { String sessionId = response.headers.get("Set-Cookie"); com.android.volley.Response<String> result = com.android.volley.Response.success(sessionId, HttpHeaderParser.parseCacheHeaders(networkResponse)); return result; } 

This will return the value of the Set-Cookie header to your onResponse method:

 new Response.Listener<String>() { @Override public void onResponse(String response) { System.out.println(response); } } 

A better idea would be to pass the listener with success / failure when calling getSessionId . This way you can easily access the result in the calling class:

 public void getSessionId(Context context, final Response.Listener<String> successListener, Response.ErrorListener failureListener) { RequestQueue queue = Volley.newRequestQueue(context); // This should be a singleton! StringRequest sr = new StringRequest(Request.Method.GET, "any url", successListener, failureListener) { @Override protected Response<String> parseNetworkResponse(NetworkResponse networkResponse) { String sessionId = response.headers.get("Set-Cookie"); com.android.volley.Response<String> result = com.android.volley.Response.success(sessionId, HttpHeaderParser.parseCacheHeaders(networkResponse)); return result; } }; queue.add(sr); } 

Edit:

Now you can call it like this:

 Login login = new Login(); login.getSessionId(this, new Response.Listener<String>() { @Override public void onResponse(String response) { // You can access member variables from here. // This will only get called after the network activity has happened! phpsessid = response; // All the work you need to do after your session ID has updated, you can put into a method and call it from here // In your original example, this would be (4) onSessionIdUpdated(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // We'll just ignore these for now - you should handle errors though! } }); // Anything you put here will happen immediately after the getSessionId call above, and notably *before* onResponse or onErrorResponse is called (ignoring potential race conditions here for now). 
+8
source

All Articles