2013-06-26 25 views
5

如何使用這樣的請求使用排球庫的會話cookie?使用會話cookie與JsonObjectRequest的android排球庫

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); 

感謝

回答

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

我用亂射的Android庫,因爲它像AFNetworking iOS中。以前更容易和更快。 關於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"/> 

希望它可以幫助你!謝謝。

1

這是我沒有保存使用凌空餅乾JsonObjectRequest

的想法是,以捕獲與我的JSON請求回來的Set-Cookie頭,然後將其保存在首

請求的方式

  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); 
      } 
     }; 

public static void saveCookie(Context context, String cookie) { 
     if (cookie == null) { 
      return; 
     } 

     // Save in the preferences 
     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); 
     if (null == sharedPreferences) { 
      return; 
     } 
     SharedPreferences.Editor editor = sharedPreferences.edit(); 
     editor.putString("cookie", cookie); 
     editor.commit(); 
    } 

    public static String getCookie(Context context) 
    { 
     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); 
     String cookie = sharedPreferences.getString("cookie", ""); 
     if (cookie.contains("expires")) { 
      removeCookie(context); 
      return ""; 
     } 
     return cookie; 
    } 
+0

你是我的英雄! – aletede91