Web view problems

时间:2016-04-04 17:09:36

标签: android android-webview

I have some problem in my web view.
I want to load a string from a WordPress blog, witch have html tags such as <a> and image tag and ... .
So my problems are:

  1. As I mention above, I want to load a local string and I want to handle user click on the links, so I load data like this into the web view:

    WebView webview = (WebView) this.findViewById(R.id.mainWV);
    webview.setWebViewClient(new MyWebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);
    
    String s="<p>It will<a href=\"http://www.bbc.com/\"> enable</a> Seattle-based Alaska to expand into lucrative hubs such</p>\n<p><img class=\"aligncenter size-full wp-image-1035\" src=\"http://ichef.bbci.co.uk/news/660/cpsprodpb/D09F/production/_89070435_89069565.jpg\" alt=\"\" width=\"300\" height=\"120\" /></p>\n<p>as San Francisco and Los Angeles.</p>\n"; 
    
    webview.loadDataWithBaseURL("", s, "text/html", "utf-8", "");
    

    and another way I tried was:

     String head1 = "<head></head>";
        String text = "<html>" + head1
                + "<body  dir=\"rtl\" >" + s
                + "</body></html>";
    

    webview.loadData(text, "text/html", "utf-8");

    and my client is :

     class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d("USER_CLICKED", url + "USER_CLICKED");
            return true;
        }
    }
    

    Ok, now when I run the app, and when I click on <a> I never see 'USER_CLICKED', but the webview content change and it seems web view is empty, I mean is white as snow.
    notice 1: when I try this:

    webview.loadUrl("https://android-arsenal.com/");
    

    and run the app, when I click on the links in the loaded web view, every thing is OK, and i see this Log: 'USER_CLICKED' and the related URL. notice 2: yes i try a lots of different URL, but loading from string, nothings change in click handling.
    notice 3: I test in android 5.1 and 4.1 in 4.1 clicked recognized and is see 'User.. but in the 5.1 the white page story happens.(Edit: android 6 also do not show 'USER... ')

  2. my number 2 problem is, when I call this:

     webview.loadDataWithBaseURL("", s, "text/html", "utf-8", "");
    

    the image tag does not load! I mean it is just ignore to load the images, and I do not know why.
    notice 3: when I copy text from inside the web view there is some rectangle in the text.

2 个答案:

答案 0 :(得分:1)

确定每一个,经过很长一段时间,我发现问题,所以正如我上面提到的,我从WordPress rest API(json)中取出字符串,所以在我看来,字符串应该没问题,但是我发现了,字符串中有一些额外的'\',字符串如下:

<p>It will<a href=\\\"example URL\\\"> enable</a> Seattle....

,简单就是我只是使用:   S = s.replaceAll( “\\”, “”); 所以,感谢任何看过这篇文章的人。

答案 1 :(得分:0)

网址是一个网址。第一项是HTML,而不是URL。如果要加载URL,则必须传递有效的URL(而不是HTML),并且不要期望从正在加载的HTML字符串中神奇地解析URL。事实上,第一项甚至不是有效的HTML,它是HTML的一部分,可能是一个片段,但它甚至没有包含在我希望Web视图需要的HTML标记中。