Last active
July 6, 2023 06:36
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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