如何使用具有此类请求的排球库来使用会话cookie?
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//Response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Error
}
});
queue.add(jsObjRequest);
感谢
答案 0 :(得分:1)
BasicHttpParams mHttpParams = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 15000;
HttpConnectionParams.setConnectionTimeout(mHttpParams, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 20000;
HttpConnectionParams.setSoTimeout(mHttpParams, timeoutSocket);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
registry.register(new Scheme("https", sslSocketFactory, 443));
/*ClientConnectionManager cm = new ThreadSafeClientConnManager(mHttpParams, registry);*/
DefaultHttpClient defaultHttpClient = new DefaultHttpClient(/*cm,*/ mHttpParams);
RequestQueue requestQueue = Volley.newRequestQueue(mContext.getApplicationContext(),new HttpClientStack(defaultHttpClient));
答案 1 :(得分:1)
我使用Volley Android Library,因为它喜欢iOS中的AFNetworking。之前更轻松,更快捷。 关于Cookies会话登录成功时。 我配置了:
CookieManager cookieManage = new CookieManager();
CookieHandler.setDefault(cookieManage);
它成功了。但是,CookieManage只是执行更大的8版本SDK。
赞:在AndroidManifest.xml中
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
希望它可以帮到你!感谢。
答案 2 :(得分:1)
这是我使用Volley with JsonObjectRequest
保存cookie的方法我们的想法是捕获随Json请求返回的Set-Cookie标头,然后将其保存在首选项中
请求强>
10*10 + 1 = 101
偏好强>
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,
url,
(String)param,
requestFuture, requestFuture){
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Show.m("getHeaders");
Map<String,String> headers = new HashMap<String, String>();
headers.put("Accept","application/json");
if(!MyApplication.getCookie(context).equals("")){
String cookie = MyApplication.getCookie(context);
Show.m("Cookie to load from preferences: " + cookie);
headers.put("Cookie", cookie);
}
return headers;
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
Map headers = response.headers;
String cookie = (String)headers.get("Set-Cookie");
MyApplication.saveCookie(context, cookie);
Show.m("Cookie to save to preferences: " + cookie);
return super.parseNetworkResponse(response);
}
};