如何使用谷歌排球在Android电子/ Java的HTTP GET请求响应报头字段返回主方法? [英] How to return response header field to main method using Google Volley for HTTP GET request in Android / Java?
问题描述
我使用谷歌凌空于Android的联网。我会让一个HTTP GET请求,需要返回的响应标头值。我发现堆栈溢出一些答案来访问头字段,但不知道如何将其恢复到我的呼唤现场。
请看看我的code,我把四个数字在它来解释我的问题。
在(1)我可以打印出我需要的价值。比我试着将它保存在一个类的属性(2)并没有在IDE中没有错误。如果我想从那里(3)返回它,我得到了在(4)一个NullPointerException异常。也许它写问题之前阅读那里。
所以,我怎么可以从(1)至(4)的价值?非常感谢!
公共类登录{字符串PHPSESSID = NULL;市民登录(){}公共字符串getSessionId(上下文的背景下){
请求队列队列= Volley.newRequestQueue(背景);
StringRequest SR =新StringRequest(Request.Method.GET,任何URL
新Response.Listener<串GT;(){
@覆盖
公共无效onResponse(字符串响应){
的System.out.println(响应);
}
},新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
}
}){
@覆盖
受保护的响应<串GT; parseNetworkResponse(NetworkResponse响应){
的System.out.println(response.headers.get(设置Cookie)); (1)
PHPSESSID = response.headers.get(设置Cookie); (2)
返回super.parseNetworkResponse(响应);
}
};
queue.add(SR);
返回PHPSESSID; (3)
}
}
在主:
登录登录=新的登录();
字符串结果= login.getSessionId(this.getContext);
的System.out.println(结果); (4)
当您添加请求队列,该请求被执行的异步的。这意味着,你正在阅读它,它是不以同样的顺序执行 - 它发生在另一个线程,而当它这样做最终会回归
保护的响应和LT;弦乐> parseNetworkResponse(NetworkResponse响应){
的System.out.println(response.headers.get(设置Cookie)); (1)
PHPSESSID = response.headers.get(设置Cookie); (2)
返回super.parseNetworkResponse(响应);
}
这返回的体内的回应 - 从什么我读您的code,它看起来像你想返回设置Cookie头的值。你可以这样做是这样的:
保护的响应和LT;弦乐> parseNetworkResponse(NetworkResponse networkResponse){
字符串的sessionId = response.headers.get(设置Cookie);
com.android.volley.Response<串GT;结果= com.android.volley.Response.success(的sessionId,
HttpHeaderParser.parseCacheHeaders(networkResponse));
返回结果;
}
这将返回设置Cookie头到你的 onResponse
方法的值:
新Response.Listener<串GT;(){
@覆盖
公共无效onResponse(字符串响应){
的System.out.println(响应);
}
}
有一个更好的想法可能是通过使你的 getSessionId
电话时在成功/失败监听器。这样,你就可以轻松搞定访问结果调用的类:
公共无效getSessionId(上下文的背景下,最终Response.Listener<串GT; successListener,Response.ErrorListener FailureListener的){
请求队列队列= Volley.newRequestQueue(背景); //这应该是一个单身!
StringRequest SR =新StringRequest(Request.Method.GET,任何URL
successListener,
FailureListener的){
@覆盖
受保护的响应<串GT; parseNetworkResponse(NetworkResponse networkResponse){
字符串的sessionId = response.headers.get(设置Cookie);
com.android.volley.Response<串GT;结果= com.android.volley.Response.success(的sessionId,
HttpHeaderParser.parseCacheHeaders(networkResponse));
返回结果;
}
};
queue.add(SR);
}
编辑:
现在,你可以调用这个如下:
登录登录=新的登录();
login.getSessionId(这一点,新Response.Listener<串GT;(){
@覆盖
公共无效onResponse(字符串响应){
//你可以从这里访问成员变量。
//网络活动发生后,这只会被调用!
PHPSESSID =响应;
//你需要你的会话ID已更新后,做所有的工作,你可以放到一个方法,从这里调用它
//在你原来的例子,这将是(4)
onSessionIdUpdated();
}
},新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
//我们只忽略这些现在 - 你应该尽管处理错误!
}
});
//你把这里任何会立即上述getSessionId调用后发生之前,并显着* onResponse或onErrorResponse被称为(这里忽略了潜在的竞争条件,现在)。
I'm using google volley for networking in android. I will make a http GET request and need to return a response header value. I found some answers on stack overflow to access the header fields, but don't know how to return it to my calling spot. Please have a look at my code, I put four numbers in it to explain my problem.
At (1) I can print out the value which I need. Than I've tried to save it in a class attribute (2) and there is no error in the IDE. If I want to return it from there (3), I got a NullPointerException at (4). Maybe its a read before write problem there. So how can I get the value from (1) to (4)? Thanks a lot!
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)
}
}
in main:
Login login = new Login();
String result = login.getSessionId(this.getContext);
System.out.println(result); (4)
When you add a request to the queue, that request is executed asynchronously. This means it is not executed in the same order as you're reading it - it happens on another thread, and will return eventually when it's done.
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'm reading in your code, it looks like you want to return the value of the "Set-Cookie" header. You can do this like this:
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 may be to pass a success/failure listener in when making your getSessionId
call. In this way, you can then easily get access to 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 this as follows:
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).
这篇关于如何使用谷歌排球在Android电子/ Java的HTTP GET请求响应报头字段返回主方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!