setDownloadListener onDownloadStart是在webview已经获取了文件之后被调用的吗?[英] Is setDownloadListener onDownloadStart called after the webview already Gets the file?

本文是小编为大家收集整理的关于setDownloadListener onDownloadStart是在webview已经获取了文件之后被调用的吗?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

webview将执行http get并下载完整文件,然后调用我的ondownloadstart()方法,我的代码再次下载文件?

在用于Android应用程序中使用的WebView中,我们需要处理下载PDF文件.我看到了我猜的行为是有道理的,但似乎很奇怪,所以我希望有人能为我验证.

当WebView设置为SetDowloadListener()并创建新的DownloadListener以处理ondownloadStart()方法调用.在indownloadstart()方法中,我们使用HttpCurlConnection从我们的Web服务器获取资源.

在网络迹线中,我会看到对相同资源执行的两个HTTP获取请求.我假设这是因为WebView首先对资源进行了,那么WebView是自己的处理,并确定它无法呈现资源.然后webview然后调用ondownloadstart()方法,然后第二次检索资源.

setdowloadlistener 说:

注册内容无法处理时要使用的接口 渲染引擎,应该是下载的.这会 替换当前处理程序.

WebView将不知道它是否可以呈现资源,直到它从服务器获取响应,并且可以读取返回的内容类型.所以,它必须首先做一个或一个头来阅读响应标题.因此,双下载行为似乎有意义.

,一些跟进问题:

  1. 这是一个常见的情况吗?大多数应用程序在WebView中下载文件真的是两次下载文件吗? (似乎昂贵但我认为它可能正在发生)
  2. 有没有办法从第一请求中重用已经下载的内容而不是再次请求它?
  3. 为什么WebView不在第一个请求中使用HTTP MEAD方法而不是获取? (我猜这将使每个超链接到两个步进过程,这也是昂贵的)
  4. 有没有办法可以防止额外下载?也许使用了应该使用的rideurlloading()来拦截请求?

推荐答案

当一个beginn答案到你的 3的答案时更好.问题:

我认为webview使用所有Ressources的Get-方法.只有在它获得此请求的第一个HTTP标题之后,WebView检查是否有标题告诉"DO下载"

(例如Content-Disposition: Attachment; filename=example.html等标题)

如果没有标头,该标题会指向下载,WebView将在其视图中显示加载和内容.

如果存在下载标题(即使其值设置为"内联"),则调用

onDownload.

问题2 :

我认为在这种情况下,WebView不会加载内容的内容. 目前我不知道一种重用现有请求的方法.

问题4

如果覆盖shouldInterceptRequest

在这个例子中: https://stackoverflow.com/a/29811280/2377961 你可以改变这个行为.

本文地址:https://www.itbaoku.cn/post/102303.html

问题描述

Is it true that the WebView will perform an Http Get and download the complete file then it calls calls my onDownloadStart() method and my code downloads the file again?

In a WebView used in an Android application we need to handle downloading a PDF file. I'm seeing behavior which I guess makes sense, but it seems odd so I'm hoping someone can verify for me.

When the WebView is set up we call setDownloadListener() and create a new DownloadListener to handle the onDownloadStart() method call. In the onDownloadStart() method we use an HttpURLConnection to get the resource from our web server.

In network traces I see two Http Get requests performed for the same resource. I assume this is because the webview first does a Get on the resource, then the webview does its own processing and determines that it cannot render the resource. The webview then calls the onDownloadStart() method and we retrieve the resource a second time.

The docs for SetDownloadListener say:

Register the interface to be used when content can not be handled by the rendering engine, and should be downloaded instead. This will replace the current handler.

The webview would not know if it can render the resource until it gets a response from the server and can read the content-type returned. So, it must first do a GET or a HEAD to read the response headers. So, the double download behavior seems to make sense.

And, some follow up questions:

  1. Is this a common situation? Do most apps that download files from within a webview really download the file twice? (that seems expensive but I think it may be happening)
  2. Is there a way to reuse the already downloaded content from the first request rather than requesting it again?
  3. Why doesn't the WebView use the Http HEAD method on the first request rather than GET? (I guess this would make every hyperlink a two step process and that would be expensive too)
  4. Is there a way I can prevent the extra download? Perhaps using shouldOverrideUrlLoading() to intercept the request?

推荐答案

Its better when a beginn with the answer to your 3. question:

I think WebView uses GET-method for all Ressources. And only after it gets the first http Headers of this request WebView checks if there are headers that tell "do a download"

(e.g. headers like Content-Disposition: Attachment; filename=example.html)

If no header exists which points to a download, WebView will display load and the content in its view.

onDownload is called if there is a download header (even if its value are set to "inline").

Answer to question 2:

I think in that case the webview does NOT load the content of the content. Currently i don`t know a way to reuse the existing request.

Answer to question 4

If you overide shouldInterceptRequest

like in this example: https://stackoverflow.com/a/29811280/2377961 You could change this behavoir.