0

I have a android application which loads a website using webview. In my website landing page i have a form where user input a code number, according to that code number i am redirecting to a specific url of my site. Which works perfectly well in my website. What is did is stored the code in session with that url and when the same user load my site again it automatically redirect it to that page. But when i try this scenario using my android application, seems like session data not working. Can anyone help me out?

Here is my code:

package com.myPackageName;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Message;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
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 {

    WebView myweb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myweb = findViewById(R.id.myWebView);

        WebSettings myWebSettings = myweb.getSettings();
        myWebSettings.setJavaScriptEnabled(true);
        myWebSettings.setSupportMultipleWindows(true);
        myWebSettings.setBuiltInZoomControls(true);
        myweb.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        myweb.getSettings().setAppCacheEnabled(true);

        myWebSettings.setDomStorageEnabled(true);
        myWebSettings.setSavePassword(true);
        myWebSettings.setSaveFormData(true);

        myweb.setWebViewClient(new MyWebViewClient());
        myweb.setWebChromeClient(new MyWebChromeClient());

        CookieSyncManager.createInstance(this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);

        myweb.loadUrl("https://www.mywebsite.com");

    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    private class MyWebChromeClient extends WebChromeClient{
        @Override
        public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
            WebView newWebView = new WebView(MainActivity.this);
            view.addView(newWebView);
            WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
            transport.setWebView(newWebView);
            //transport.getWebView();
            resultMsg.sendToTarget();
            //Toast.makeText(MainActivity.this, WebView.HitTestResult , Toast.LENGTH_LONG).show();
            return true;
        }
    }

    @Override
    public void onBackPressed () {
        if (myweb != null && myweb.canGoBack())
            myweb.goBack();// if there is previous page open it
        else
            super.onBackPressed();//if there is no previous page, close app
    }
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Alauddin Ahmed
  • 1,128
  • 2
  • 14
  • 34

1 Answers1

1

I think, that your problem lies in the fact, that each time you start your activity, the webview has no clue of the previous set cookies.

To overcome this problem, you might be able, to store your set cookies, re-inject them, when starting a new instance of your webview.

A reference on how to get the cookies can be found her: Reading cookies

sebi0920
  • 300
  • 4
  • 12
  • Sorry for long wait. As i found out. That those code above is sufficient enough for storing and getting cookies. But the problem is, i have to wait 15 seconds to store cookies. Can you help me out with that? – Alauddin Ahmed May 05 '20 at 21:57
  • Of course, when you want to define a waiting interval in Android you have to do that in a background thread. There are many possibilities to obtain another thread. One of the most common one is a runnable: https://developer.android.com/reference/java/lang/Runnable Inside the runnable you can easily wait for 15 seconds, and then save the cookies. There are many ways (far beyond the reach of my comment) to ensure that the cookies get written. I would recommend you to have a look at: https://developer.android.com/guide/components/processes-and-threads#Threads – sebi0920 May 06 '20 at 13:07
  • I don't want to wait 15 seconds. I want to set the cookie instantly. As i told you, right now it is taking 15-20 seconds. – Alauddin Ahmed May 06 '20 at 14:08