2

. I am trying to download a file from URL . When I open the URL from normal browser, it redirects me to a login page and once I login, download starts . I need to have the same functionality in my application . I need to login via a webview. Once I login, download should be triggered.

When I use the below logic, even after I login in webview, browser redirects to login page. Please help.

private void loadPage(final String iUrl) {
        // TODO Auto-generated method stub
        WebView webView = (WebView) findViewById(R.id.webView1);
        WebSettings webSettings = webView.getSettings(); 
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setBuiltInZoomControls(true);
        int default_zoom_level=100;
        webView.setInitialScale(default_zoom_level);
        final Activity activity = this;
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                activity.setTitle("I-Aurora: Loading...");
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(getApplicationContext(), description, Toast.LENGTH_SHORT).show();
            }
            public void onPageFinished (WebView view, String url) {
                activity.setTitle("I-Aurora: Login");
            }
        });
        webView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,String contentDisposition,String mimetype,long contentLength) {
                   Intent int3 = new Intent(Intent.ACTION_VIEW);
                   int3.setClassName("com.android.chrome", "com.google.android.apps.chrome.Main");
                   int3.setData(Uri.parse(url));
                   startActivity(int3);
            }
        });
        webView.loadUrl(iUrl);
        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "Button Click" , Toast.LENGTH_SHORT).show();
            }
        }); 
        return;
    }
nandudroid
  • 21
  • 2
  • 1
    You can add the cookie on your request header. http://stackoverflow.com/questions/22935214/file-download-not-working-in-android-using-cordova-2-9-0/24646292#24646292 – h3n Jul 09 '14 at 06:31

1 Answers1

0

When you log in with the WebView then the WebView in your app is logged in, not the browser. There is no shared state between the two.

What you need to do is handle the download yourself in the DownloadListener (like this, for example) and pass the cookies needed for authentication.

Community
  • 1
  • 1
marcin.kosiba
  • 3,221
  • 14
  • 19