Skip to content

Instantly share code, notes, and snippets.

@jesty
Last active July 6, 2023 06:36
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesty/e934314cae3d2014c6fa46ab64f3904e to your computer and use it in GitHub Desktop.
Save jesty/e934314cae3d2014c6fa46ab64f3904e to your computer and use it in GitHub Desktop.
Share cookies between WebView and OkHttpClient 3 / Retrofit 2. In the example I did the login on a web page in a WebView component and then I used the cookie to invoke a service from an activity
//Setup the client
OkHttpClient client = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
String cookie = getSharedPreferences(context).getString(Constants.COOKIE, "");
return !cookie.isEmpty() ? Arrays.asList(Cookie.parse(url, cookie)) : Collections.EMPTY_LIST;
}
})
.build();
//use the client with Retrofit
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.baseUrl(Constants.SERVER_URL)
.build();
package com.nutcore.mementoparking;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Cookie;
import static com.nutcore.mementoparking.Constants.USERINFO;
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_social_side);
webView = (WebView) findViewById(R.id.webView1);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(false);
settings.setGeolocationEnabled(true);
settings.setAppCacheEnabled(false);
settings.setLoadsImagesAutomatically(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(Constants.SERVER_URL);
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
//I save the cookies only when the user goes on login page
if(url.equals(Constants.LOGIN_URL)){
CookieSyncManager syncManager = CookieSyncManager.createInstance(webView.getContext());
CookieManager cookieManager = CookieManager.getInstance();
String cookie = cookieManager.getCookie(url);
SharedPreferences.Editor editor = getEditor(SocialSideActivity.this);
editor.putString(Constants.COOKIE, cookie);
editor.commit();
syncManager.sync();
}
}
});
}
private SharedPreferences.Editor getEditor(Context context) {
SharedPreferences settings = getSharedPreferences(context);
return settings.edit();
}
private SharedPreferences getSharedPreferences(Context context) {
return context.getSharedPreferences(USERINFO, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment