【问题标题】:Not able to download a file inside android application using webview无法使用 webview 在 android 应用程序中下载文件
【发布时间】:2014-02-20 18:09:04
【问题描述】:

。我正在尝试从 URL 下载文件 .当我从普通浏览器打开 URL 时,它会将我重定向到登录页面,一旦我登录,下载就会开始 .我需要在我的应用程序中具有相同的功能 .我需要通过 webview 登录。登录后,应触发下载。

当我使用以下逻辑时,即使在我登录 webview 后,浏览器也会重定向到登录页面。请帮忙。

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;
    }

【问题讨论】:

标签: android login webview download


【解决方案1】:

当您使用 WebView 登录时,您的应用程序中的 WebView 已登录,而不是浏览器。两者之间没有共享状态。

您需要自己在 DownloadListener(例如 this)和 pass the cookies needed for authentication 中处理下载。

【讨论】:

  • 还是不能下载,求解决办法
猜你喜欢
  • 2014-08-13
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 2014-06-24
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 2013-06-17
相关资源
最近更新 更多