你好!
我的应用程序使用简单的HTTP客户端-服务器通信,在客户端使用Volley库建立。首先,我发起一个授权请求:
public class AuthenticationRequest extends StringRequest {
private static final String TAG = AuthenticationRequest.class.getSimpleName();
private String login;
private String password;
public AuthenticationRequest(int method,
String url,
Response.Listener<String> listener,
Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
}
public void setLogin(String login) {
this.login = login;
}
public void setPassword(String password) {
this.password = password;
}
@Override
protected Map<String, String> getParams() {
HashMap<String, String> parameters = new HashMap<>();
parameters.put("login", login);
parameters.put("password", password);
return parameters;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
for (Map.Entry<String, String> entry : response.headers.entrySet()) {
Log.d(TAG, entry.getKey() + " -- " + entry.getValue());
}
String sessionId = response.headers.get("Set-Cookie");
return Response.success(sessionId, HttpHeaderParser.parseCacheHeaders(response));
}
}
紧接着我的请求后,我需要列出与当前用户相关联的设备列表。此时我从响应头中获得了一个cookie。完整的授权请求响应头输出如下:
- Cache-Control -- no-store,no-cache,must-revalidate,post-check=0,pre-check=0
- Connection -- keep-alive
- Content-Length -- 16
- Content-Type -- text/html; charset=utf-8
- Date -- Fri, 18 Sep 2015 06:15:57 GMT
- Expires -- Thu, 19 Nov 1981 08:52:00 GMT
- Pragma -- no-cache
- Server -- nginx
- Set-Cookie -- PHPSESSID=osurmkq1fmnj462c3hk76l1405; path=/
- X-Android-Received-Millis -- 1442553247146
- X-Android-Sent-Millis -- 1442553247096
- X-Powered-By -- PHP/5.3.27
在Auth请求的onResponce回调中,我将Set-Cookie值作为sessionId,并启动DeviceListRequest:
public class DeviceListRequest extends StringRequest {
private static final String TAG = DeviceListRequest.class.getSimpleName();
private String phpSessId;
public DeviceListRequest(int method,
String url,
Response.Listener<String> listener,
Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
}
public void setPhpSessId(String phpSessId) {
this.phpSessId = phpSessId;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
Log.d(TAG, phpSessId);
headers.put("Cookie", phpSessId);
return headers;
}
}
我在设备列表请求的头部中加入了sessionId,但是响应中出现了这个:{"status":false,"error":"No authorization"}
在桌面PC上使用Chrome浏览器可以正常接收设备列表,问题出现在Android上。
请问我的代码有什么问题?也许我在设置头部时出错了吗?
发送请求的代码如下:
public void authenticationRequest(String login, String password) {
final AuthenticationRequest request = new AuthenticationRequest(Request.Method.GET,
AUTHENTICATION_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Response: " + response);
phpSessId = response;
deviceListRequest(phpSessId);
}
},
this);
request.setLogin(login);
request.setPassword(password);
requestQueue.add(request);
}
public void deviceListRequest(String phpSessId) {
DeviceListRequest request = new DeviceListRequest(Request.Method.GET,
DEVICE_LIST_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Response: " + response);
}
},
this);
request.setPhpSessId(phpSessId);
requestQueue.add(request);
}