嗨,我目前正在开发一个使用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.
Current problem:
除非用户正在使用该应用程序(跨越不同的活动),否则我能够保持会话cookie活着 . 但是一旦用户退出应用程序,Cookie就会被删除 . 我已将这些cookie声明为一个类似于 Application 类的类 . 即使在应用程序退出后,我仍需要保持这些会话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);
});
......
提前致谢 .
1 回答
问题解决了......感谢这个链接https://gist.github.com/jacobtabak/78e226673d5a6a4c4367
上述计划需要进行一些小改动才能使其发挥作用 . 在上面的程序中,它会抛出一个错误,说它无法将条目转换为字符串 . 您可以通过添加
enrty.toString()
来解决 .