2014-12-24 42 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

感谢您的支持。 –