WebView cookie无法正常工作

时间:2014-02-20 18:01:53

标签: java android cookies webview

在说这是重复之前,stackoverflow的解决方案都没有为我工作。 我有一个有按钮的应用程序。 单击按钮后,将打开带有webview的webviewActivity。 它有一个登录,当我输入我的登录并尝试访问网站的其余部分(我的凭据已经打开)时,我无法登录。

这是我的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;
            }               
        }
    }

    }

希望相机代码或我shouldInterceptRequest与此问题无关。

1 个答案:

答案 0 :(得分:1)

这是我的问题:

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

我忘了设置应用程序缓存路径了!