是setDownloadListener onDownloadStart之后调用web视图已获取的文件吗?视图、文件、setDownloadListener、onDownloadStart

2023-09-06 05:11:49 作者:无情斩情丝

这是真的,web视图将执行一个HTTP GET和下载完整的文件,那么它调用调用我onDownloadStart()方法和我的code再次下载该文件?

在我们需要处理下载一个PDF文件Android应用程序使用的WebView。我看到的行为,我想是有道理的,但它似乎很奇怪,所以我希望有人可以验证我。

在web视图设置,我们称之为setDownloadListener(),并创建一个新的DownloadListener来处理onDownloadStart()方法调用。在onDownloadStart()方法中,我们使用一个HttpURLConnection类,以获得我们的Web服务器的资源。

在网络跟踪我看到两个HTTP GET对同一资源进行的请求。我想这是因为web视图首先进行获取的资源,那么web视图执行其自己的处理,并确定它无法呈现的资源。 web视图然后调用onDownloadStart()方法中,我们获取资源的第二次。

该文档的 SetDownloadListener 说:

  

注册时的界面内容不能被处理,以用于   呈现引擎,并应被下载代替。这会   取代目前的处理程序。

web视图不知道它是否可以使资源,直到它得到来自服务器的响应,并可以读取内容类型返回。因此,它必须首先做一个GET或HEAD读取响应头。因此,双下载行为似乎是有道理的。

Free Dailymotion Download中文版下载 Free Dailymotion Download破解版 v4.0.0.208 附注册机 9553下载

和一些跟进的问题:

这是一个常见的​​情况?做到这一点从web视图中的下载文件,大多数应用程序真正下载的文件的两倍? (这似乎昂贵,但我认为它可能会发生) 有没有办法重用从第一个请求已经下载的内容,而不是再次请求呢? 为什么没有web视图使用HTTP HEAD方法的第一个请求,而不是得到什么呢? (我想这将尽一切超链接的两个步骤,这将是过于昂贵) 有没有一种方法可以让我prevent额外的下载?也许使用shouldOverrideUrlLoading()来拦截请求? 解决方案

它的更好的时候的答案一个初学者到您的 3。问题:

我想的WebView使用GET-方法对所有Ressources。它gehts的这一请求的WebView检查的第一个HTTP标头后,才是否有告诉做一个下载标题

(如头文件,如内容处置:附件;文件名= example.html

如果没有标头存在至极点,下载,web视图显示负载,并在其查看内容。

onDownload 叫,如果有一个下载头(即使其值被设置为内联)。

回答问题2

我觉得在这种情况下,web视图不加载内容的内容。 目前,我不`吨知道的方式来重用现有的请求。

答第4题

如果您在此改变 shouldInterceptRequest

就像这个例子: http://stackoverflow.com/a/29811280/2377961 你可以改变这种behavoir。

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:

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) Is there a way to reuse the already downloaded content from the first request rather than requesting it again? 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) 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 gehts 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 wich 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: http://stackoverflow.com/a/29811280/2377961 You could change this behavoir.