Issue
I'm a beginner in Android web view development. What should I do to keep the cookies? I would like to implement the php homepage using Android WebView. Most of the features have been implemented, but I don't know what to do with cookies that store id and pw How should I implement it in what areas? Please teach me.
in this my mainactivity.java code
package com.example.webviewexample;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private String url = "http://www.test.com/user/login.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClientClass());
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setSaveFormData(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
@Override
public void onBackPressed() {
if (webView.getUrl().equalsIgnoreCase("http://www.test.com/index.php") || webView.getUrl().equalsIgnoreCase("http://www.test.com/user/login.php")) {
new AlertDialog.Builder(this)
.setTitle("exit")
.setMessage("exit?")
.setPositiveButton("finish", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
moveTaskToBack(true);
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
})
.setNegativeButton("no",null)
.show();
}else if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
private class WebViewClientClass extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Solution
To keep cookies, you can use below:
CookieManager.getInstance().setAcceptCookie(true);
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
You can extract all cookies from current url by this way from Webview as string:
@Override
public void onPageFinished(WebView view, String url){
String cookies = CookieManager.getInstance().getCookie(url);
Log.d(TAG, "All the cookies in a string:" + cookies);
}
Answered By - TheND
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.