Android:WebView loadDataWithBaseURL 不显示验证码图像

发布于 2024-12-02 06:58:19 字数 1438 浏览 5 评论 0 原文

我试图在应用程序的 WebView 中显示验证码 jpeg 图像(由 Java servlet 动态生成),但是,它有时会显示蓝色问号而不是验证码图像。下面是我在扩展 WebViewClientMyWebViewClient 类的 shouldOverrideUrlLoading(WebView view, String url) 方法中的内容

我在我的 WebViewClient 中启用了互联网访问明显,并且该问题出现的时间约为 80%。即有时图像确实显示正确,但非常罕见。

这里还有另一个类似的问题 Image with Dynamic src Loads in Android browser, but not in Webview,但确实似乎没有明确的答案。

提前致谢

try
            {
                CookieSyncManager.getInstance().sync();
                Log.v("Hello","CookieStore: " + httpClient.getCookieStore().toString());
                HttpResponse response = httpClient.execute(httppost);
                data = new BasicResponseHandler().handleResponse(response);
                view.loadDataWithBaseURL(URL.toString(), data, "text/html", "UTF-8" , null);
                //Log.v("Hello","Data: " + data.toString());
            }
            catch (ClientProtocolException e)
            {  
                Log.v("Hello","ClientProtocolException:Overriding");
                Log.v("Hello",e.toString());
                e.printStackTrace();    
            } 
            catch (IOException e) 
            {   
                Log.v("Hello","IOException:Overriding");
                Log.v("Hello",e.toString());
                e.printStackTrace();  
            }

I am trying to display a captcha jpeg image (which is generated dynamically by a Java servlet) in my application's WebView, however, it will sometimes display a blue question mark instead of the captcha image. Below is what I have in my shouldOverrideUrlLoading(WebView view, String url) method of MyWebViewClient class that extends WebViewClient

I have internet access enabled in my manifest and the problem occurs for about 80% of the time. i.e. Sometimes the image does show up correctly but very rare though.

There was another similar question here Image with dynamic src loads in Android browser, but not in Webview, but does not seem to have a definitive answer.

Thanks in advance

try
            {
                CookieSyncManager.getInstance().sync();
                Log.v("Hello","CookieStore: " + httpClient.getCookieStore().toString());
                HttpResponse response = httpClient.execute(httppost);
                data = new BasicResponseHandler().handleResponse(response);
                view.loadDataWithBaseURL(URL.toString(), data, "text/html", "UTF-8" , null);
                //Log.v("Hello","Data: " + data.toString());
            }
            catch (ClientProtocolException e)
            {  
                Log.v("Hello","ClientProtocolException:Overriding");
                Log.v("Hello",e.toString());
                e.printStackTrace();    
            } 
            catch (IOException e) 
            {   
                Log.v("Hello","IOException:Overriding");
                Log.v("Hello",e.toString());
                e.printStackTrace();  
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不弃不离 2024-12-09 06:58:19

我刚刚遇到了同样的问题。我发现了问题并解决了它(就我而言)。您可以注意到,在 HTML 代码中,img 标记的 src 指向站点的相对路径。类似于 。您所要做的就是注入完整路径(主机+相对路径)以获得类似 .

  1. 逐行阅读 HTML 代码
  2. 将序列 " (可能在您的网站中有些不同)与每一行匹配
  3. 当您找到该行时,您将拆分它,直到找到src="" 内的链接
  4. 使用主机名重新构建标记:

然后图像就会出现;)

I just got the same problem. I identified the issue and solved it (in my case). You can notice that in HTML code, the img tag has the src pointing to the relative path of the site. Something like <img src="/relativepath/captcha.jpg?12345678"/>. All you have to do is to inject the full path (host + relative path) to obtain something like <img src="http://yourhost.com/relativepath/captcha.jpg?12345678">.

  1. Read the HTML code, line by line
  2. Match the sequence "<img src" (maybe something a little bit differente in your site) with each line
  3. When you found the line you will split it until you find the link inside the src=""
  4. Reconstruct your tag with the host name on it: <img src="http://host.com" + link >

Then the image will appear ;)

梦幻之岛 2024-12-09 06:58:19

尝试使用 view.loadUrl(URL.toString()); 而不是 loadDataWithBaseURL

Try using view.loadUrl(URL.toString()); instead of loadDataWithBaseURL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文