首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android WebView无法在UIDAI/Aadhaar网站的DownloadStart上下载文件

Android WebView无法在UIDAI/Aadhaar网站的DownloadStart上下载文件
EN

Stack Overflow用户
提问于 2019-09-26 15:48:49
回答 1查看 406关注 0票数 5

我正在使用Webview从UIDAI网站下载我的应用程序中的.zip文件。但是当调用DownloadListener时,它会返回正确的数据,但是会下载.zip文件中的网页,而不是下载实际的文件。而当我尝试通过chrome下载相同的zip文件时。它正在下载正确的zip文件。请帮我解决这个问题。为什么在这个特定网站(https://resident.uidai.gov.in/offline-kyc)的webview中会发生这种情况,而使用相同的下载侦听程序,我可以在其他网站上下载文件?

我的代码:

代码语言:javascript
复制
public class wvDownloadFile extends AppCompatActivity {

private static final int MY_PERMISSION_REQUEST_CODE = 123;
String QT_TAG = "download test";

String fileName = "test";

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

    WebView wv = findViewById(R.id.web_view);

    checkPermission();
    String url = "https://resident.uidai.gov.in/offline-kyc";
    wv.loadUrl(url);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setSupportMultipleWindows(true);
    wv.getSettings().setAllowContentAccess(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        wv.getSettings().setAllowUniversalAccessFromFileURLs(true);
    }

    wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    wv.getSettings().setBuiltInZoomControls(false);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    wv.getSettings().setAllowFileAccess(true);
    wv.getSettings().setAllowContentAccess(true);
    wv.getSettings().setAllowFileAccess(true);
    wv.getSettings().setAllowFileAccessFromFileURLs(true);
    wv.getSettings().setAllowUniversalAccessFromFileURLs(true);
    wv.getSettings().setDomStorageEnabled(true);

    // userAgent:  downloadlistener: Mozilla/5.0 (Linux; Android 7.1.2; Redmi Y1 Lite Build/N2G47H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/76.0.3809.132 Mobile Safari/537.36
    // mimeType: application/zip
    // contentLength: 5445
    // contentDispostition: attachment; filename=offlineaadhaar20190925055612130.zip
    // url: https://resident.uidai.gov.in/offline-kyc

    wv.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {


            fileName = URLUtil.guessFileName(url, contentDisposition, mimeType);

            Log.i(QT_TAG, "downloadlistener0: " + userAgent + " " + mimeType + " " + contentLength + " " + contentDisposition + " " + url);
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

            request.setMimeType(mimeType);
            //------------------------COOKIE!!------------------------
            String cookies = CookieManager.getInstance().getCookie(url);
            request.addRequestHeader("cookie", cookies);
            //------------------------COOKIE!!------------------------
            request.addRequestHeader("User-Agent", userAgent);
            request.setDescription("Downloading file...");
            request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
            Toast.makeText(getApplicationContext(), "Downloading File0 ", Toast.LENGTH_LONG).show();
        }
    });

    wv.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i(QT_TAG, "listener  overide url ");
            view.loadUrl(url);
            return true;
        }
    });
}

protected void checkPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                // show an alert dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(wvDownloadFile.this);
                builder.setMessage("Write external storage permission is required.");
                builder.setTitle("Please grant permission");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ActivityCompat.requestPermissions(
                                wvDownloadFile.this,
                                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                MY_PERMISSION_REQUEST_CODE
                        );
                    }
                });
                builder.setNeutralButton("Cancel", null);
                AlertDialog dialog = builder.create();
                dialog.show();
            } else {
                // Request permission
                ActivityCompat.requestPermissions(
                        wvDownloadFile.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSION_REQUEST_CODE
                );
            }
        } else {
            // Permission already granted
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSION_REQUEST_CODE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission granted
            } else {
                // Permission denied
            }
        }
    }
}

}

EN

回答 1

Stack Overflow用户

发布于 2020-11-30 03:50:28

问题是我们无法从downloadListener获取web视图到downloadManager的链接

代码语言:javascript
复制
wv.setDownloadListener(new DownloadListener() {

    @Override
    public void onDownloadStart(String url, String userAgent, 
                       String contentDisposition, String mimeType, long contentLength) {

             Log.d(TAG, "onDownloadStart: " + url); // taking debug log

    }

结果将类似于,url = https://resident.uidai.gov.in/offline-kyc。这不是文件的下载链接,因为cookie会话(出于来自UIDAI的安全原因)。故障不是来自downloadManager,故障是OnDownloadListener没有使用cookie会话获取链接。

我不得不这么做,所以这是另一种方法,

Link

  • After用户使用folder

  • Getting
  1. 下载压缩文件,我告诉他们共享代码。
  2. 之后使用包含“folder
  3. Getting”和“zip”
  4. 的string.compareTo方法找到最新(按字母顺序排列)下载的文件。然后使用共享代码解压它,并创建与该文件夹中XML文件的路径相同的名称
  5. 解析XML,然后将其用于您的任务

<代码>G218

如果有人使用webView找到解决方案,请分享

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58111966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档