AAlferez
AAlferez

Reputation: 1502

WebView cookies not working

Before saying this is a duplicate, none of the solutions around stackoverflow worked for me. I have an app which has a button. Once the button is clicked, a webviewActivity with a webview is opened. It has a login, and when I type my login and try to access the rest of the website (with my credentials already on) I cant due to not been logged in.

Here is my WebViewActivity

public class WebViewActivity extends Activity {

    private WebView webView;
    private static byte[] finaldata;
    FileInputStream finall = null;
    private static boolean loopSleep = false;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webView1);    

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

        webView.setWebViewClient(new MyWebViewClient());
        WebSettings ws = webView.getSettings();
        ws.setSaveFormData(false);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDatabaseEnabled(true);
        String dbPath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        webView.getSettings().setDatabasePath(dbPath);
        webView.getSettings().setDomStorageEnabled(true);
        setContentView(webView);
        webView.loadUrl("http://192.168.1.33:8080/formsvi/login.html");

    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
            if (url.contains("TAKEPICWITHCAMERA")) 
            {
                //OPEN CAMERA AND TAKE PICTURE              
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(takePictureIntent, 1);
                }
                while (!loopSleep)
                {
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }        
                }  
                loopSleep = false;
                InputStream is = new ByteArrayInputStream(finaldata);
                return new WebResourceResponse("text/html", "UTF-8", is);    
             }          
            else
            {
                return null;
            }               
        }
    }

    }

Hope the camera code or the shouldInterceptRequest I have has nothing to do with this problem.

Upvotes: 0

Views: 1834

Answers (1)

AAlferez
AAlferez

Reputation: 1502

Here was my problem:

    String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
    webSettings.setAllowFileAccess(true);
    webSettings.setAppCachePath(appCachePath);

I was forgetting to set the application cache path!

Upvotes: 1

Related Questions