2014-12-24 40 views
5

您好我目前正在開發一個Android應用程序,使用Retrofit進行網絡調用。這是一個基本的概述我的要求。
會話Cookie在改造Android不穩定

  1.Get Facebook access token and send it to the server. 
    2.The server sets a Session cookie in the response. 
    3.In all the upcoming requests send the session cookies back. 

目前存在的問題:
我能夠在用戶不使用應用程序(在不同的活動),以保持會話cookie活着。但是一旦用戶退出應用程序,Cookie將被刪除。我已經宣佈這些餅乾在類別應用程序類中。即使在應用程序退出後,我也需要保持這些會話Cookie的存在。這樣當用戶再次打開我的應用程序時,我可以將它們用於進一步處理。

以下是我在應用程序中實現的一些代碼片段。

AppController.java(Application類的子類)

 

    public class AppController extends Application { 
     private static AppController mInstance; 
     private static OkHttpClient client; 
     private static CookieManager cookieManager; 
     private static Context context; 
     private static AviyalApiService api; // custom interface having all the GET and POST 
     private static OkClient okClient; 
     private static RestAdapter adapter; 

     @Override 
     public void onCreate() { 
      super.onCreate(); 

      client = new OkHttpClient(); 

      cookieManager = AppController.getCookieManager(); 
      client.setCookieHandler(cookieManager); 
      client.setFollowRedirects(true); 
      client.setFollowRedirects(true); 
      okClient = new OkClient(client); 

      CookieManager cookieManager = new CookieManager(); 
      cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); 
      CookieHandler.setDefault(cookieManager); 

      adapter = new RestAdapter.Builder() 
        .setEndpoint(context.getResources().getString(R.string.base_url)) 
        .setClient(okClient).build(); 
     } 

     ..... 

這裏是我如何調用該服務的一個片段。

 

    class LoginActivity extends Activity{ 
     .... 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     .... 
     ApiService api = AppController.getApi(); 
     api.fbLogin(tok, new Callback() { 
      @Override 
      public void success(FbLogin fbLogin, Response response) { 
      //success 
      } 
      @Override 
      public void failure(RetrofitError error) { 
       MyFunctions.showAlertDialogue(LoginActivity.this, "Login Failed", error.getMessage(), "OK", null); 
      }); 
     ...... 

在此先感謝。將需要

回答

2

問題解決了....謝謝這個鏈接 https://gist.github.com/jacobtabak/78e226673d5a6a4c4367

一個小的變化在上面的程序,使其工作。在上面的程序中它會給你一個錯誤,說它不能將條目轉換爲字符串。您可以通過添加enrty.toString()來解決。

+0

感謝您的支持。 –