Dan*_*her 7 android http-get http-headers android-networking android-volley
我正在使用谷歌凌空在Android中进行网络连接.我将发出一个http GET请求,并需要返回一个响应头值.我在堆栈溢出时找到了一些访问头字段的答案,但不知道如何将它返回到我的调用点.请看看我的代码,我在其中放了四个数字来解释我的问题.
在(1)我可以打印出我需要的价值.我试图将它保存在类属性(2)中,并且IDE中没有错误.如果我想从那里(3)返回它,我在(4)得到一个NullPointerException.也许它在那之前是一个读写问题.那么我怎样才能得到(1)到(4)的值?非常感谢!
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)
}
Run Code Online (Sandbox Code Playgroud)
}
在主要:
Login login = new Login();
String result = login.getSessionId(this.getContext);
System.out.println(result); (4)
Run Code Online (Sandbox Code Playgroud)
向队列添加请求时,该请求将异步执行.这意味着它的执行顺序与您正在读取的顺序不同 - 它发生在另一个线程上,最终会在完成时返回.
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);
}
Run Code Online (Sandbox Code Playgroud)
这将返回响应的主体 - 从我在您的代码中读取的内容,看起来您想要返回"Set-Cookie"标头的值.你可以这样做:
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;
}
Run Code Online (Sandbox Code Playgroud)
这将返回"Set-Cookie"标头的值到您的onResponse方法:
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
}
}
Run Code Online (Sandbox Code Playgroud)
更好的想法可能是在getSessionId拨打电话时通过成功/失败监听器.通过这种方式,您可以轻松访问调用类中的结果:
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);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
现在,您可以按如下方式调用:
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).
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7878 次 |
| 最近记录: |