6
votes

Hi I am currently developing an Android application that uses Retrofit for making Network calls.Here is a basic over overview on my requirement.

     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.

Current problem:
I am able to keep the session cookies alive unless the user is using the app(across different activities). But once the user exits the app the Cookies are deleted. I have declared these cookies inside a class that exyends Application class. I need to keep these sessions cookies alive even after the app exits. so that I can use them for further processing when the user again opens my application.

Here are some code snippets that I have implemented in my application.

AppController.java (subclass of Application class)


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

       .....

Here is a snippet of how i call the service.


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

Thanks in advance.

1

1 Answers

2
votes

Problem solved.... Thanks to this link https://gist.github.com/jacobtabak/78e226673d5a6a4c4367

A small changes will be needed in the above program to make it work. In the above program it will throw you an error saying that it cant cast entry to string. You can solve by addingenrty.toString().