Hi I am currently developing an Android application that uses Retrofit to make network calls. Here is a basic overview of 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 issue:
I can save session cookies if the user does not use the application (in different actions). But as soon as the user exits the application, cookies will be deleted. I declared these cookies in the class that exyends Application . I need these session cookies to be saved even after the application exits. so that I can use them for further processing when the user opens my application again.
Here are some code snippets that I 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.
source share