我有2个页面:第一个是登录页面,第二个是类别页面。输入凭据后,在登录API中,我从响应标头获取响应作为会话ID。
会话ID将被保存,并将用于进一步的API调用。我正在尝试调用第二个API(类别页面)。在此页面中,作为输入的内容是在请求标头中传递已保存的会话ID。获得响应为“会话已过期”。还尝试在请求标头中传递Set-Cookie: PHPSESSID=d9f9sdkfjs9
。但这没用。
注意:
public void fnCallLoginAPI() {
try {
//DEMO URL
//final String URL="http://demo.io/api/api.php?m=login";
//LIVE URL
final String URL = "https://www.live.com/shop/api/api.php?m=login";
final String requestBody = "email=abc.b@xyz.com" + "&password=43443==" + "&strPlatform=i" + "&strDeviceToken=null";
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String strResponse = response;
System.out.println("THE RESPONSE IS in PROFILE IS" + response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Cookie", "PHPSESSID=" + sessionID);
return headers;
}
@Override
public byte[] getBody() throws AuthFailureError {
byte[] body = new byte[0];
try {
System.out.println("THE REQIEST BODY IS" + requestBody);
body = requestBody.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
Log.e("TAG", "Unable to gets bytes from JSON", e.fillInStackTrace());
}
return body;
}
};
AppApplication.getInstance().addToRequestQueue(stringRequest, "assignment");
} catch (Exception e) {
}
}
public void fnCallCateGoryAPI(){
try { final String URL ="https://www.live.com/shop/api/api.php?m=getcategories";
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String strResponse = response;
System.out.println("THE RESPONSE IS in PROFILE IS" + response);
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(strResponse);
sessionID = jsonObj.optString("session_id");
System.out.print("sessionID" + sessionID);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
};
AppApplication.getInstance().addToRequestQueue(stringRequest, "assignment");
} catch (Exception e) {}
}}
答案 0 :(得分:0)
@fazil从后端增加令牌过期时间后尝试
答案 1 :(得分:0)
@fazil:我在项目中也遇到了类似的事情,我理解的原因实际上是由于在同一键“ Set-Cookie”下设置了多个标头值。
请在您的日志中进行检查。
此外,请确保已在请求中正确设置标题(检查服务器的请求和响应日志)。
如果实现的所有内容都是正确的,并且问题是由于同一标头中存在多个值,则需要检查以下凌空的实现:https://github.com/georgiecasey/android-volley-duplicateheadersfix