android - session Cookie 在 Retrofit Android 中不持久

标签 android facebook retrofit okhttp android-cookiemanager

您好,我目前正在开发一个使用 Retrofit 进行网络调用的 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.

当前问题:
除非用户正在使用该应用程序(跨不同的 Activity ),否则我能够使 session cookie 保持 Activity 状态。但是一旦用户退出应用程序,Cookie 就会被删除。我已经在 Application 类之外的类中声明了这些 cookie。即使在应用程序退出后,我也需要让这些 session cookie 保持 Activity 状态。以便当用户再次打开我的应用程序时,我可以使用它们进行进一步处理。

以下是我在我的应用程序中实现的一些代码 fragment 。

AppController.java(应用类的子类)


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

       .....

这是我如何调用该服务的 fragment 。


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

提前致谢。

最佳答案

问题解决了....感谢这个链接 https://gist.github.com/jacobtabak/78e226673d5a6a4c4367

上面的程序需要做一些小的改动才能运行。在上面的程序中,它会抛出一个错误,提示它无法将条目转换为字符串。可以通过添加enrty.toString()来解决。

关于android - session Cookie 在 Retrofit Android 中不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27632729/

相关文章:

Android facebook SDK - 无法为 facebook sharebutton 设置 onclicklistener

java - 将 Gson 与 JSend Standard 结合使用时遇到的问题

android - 使用 phonegap 和 jquery mobile 在 android 上崩溃

android - 移动 SDK 中的 oauth 2 实现

facebook - 我可以修复错误: "JSON.parse: unexpected character"吗

javascript - 带有页面访问 token 的 Facebook Graph API private_replies 抛出错误

java - 为什么不调用改造 validator ?

android - 改造 - 插入查询后将文本附加到终点的末尾

android - 如何从首选项中获取摘要

安卓开发框架