开发者

Android: WebView loadDataWithBaseURL does not display captcha image

开发者 https://www.devze.com 2023-04-01 02:53 出处:网络
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 capt

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 rar开发者_开发知识库e 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();  
            }


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消