【问题标题】:Unable to detect http url inside webview in android无法在android的webview中检测到http url
【发布时间】:2013-12-29 11:04:47
【问题描述】:

我通过点击链接在 webview 中检测 web url 它应该在应用程序外部打开 url。

我的代码如下:-

  content=getContentData();
  final String mimeType = "text/html";
  final String encoding = "UTF-8";
  webViewDetails.getSettings().setJavaScriptEnabled(true);
  webViewDetails.loadDataWithBaseURL("", content, mimeType, encoding, ""); 

出现错误:- 网页不可用

提前谢谢....

=== 如果我使用以下代码,则显示如下:-

 SpannableString sp = new SpannableString(content);
 Linkify.addLinks(sp, Linkify.WEB_URLS);
 final String data = Html.toHtml(sp);
 final String mimeType = "text/html";
 final String encoding = "UTF-8";
 webViewDetails.getSettings().setJavaScriptEnabled(true);
 webViewDetails.loadDataWithBaseURL("", data, mimeType, encoding, "");

【问题讨论】:

  • 您是否在AndroidManifest.xml 文件中添加互联网权限??
  • <uses-permission android:name="android.permission.INTERNET" />
  • @TomaszGawel,是的,已添加权限。
  • @TomaszGawel,我更新了我的问题。对此有任何想法。谢谢...
  • 问题出在您的 html 字符串中。我用String content = "This is link http://www.google.com. Just testing if linkify works"; 对此进行了测试,它可以工作。

标签: android url webview hyperlink


【解决方案1】:

尝试在代码中使用单引号,而不是使用双引号,

<a href='http://basecamp.com'>http://basecamp.com</a>

【讨论】:

    【解决方案2】:

    您需要重写WebViewClient 实现(shouldOverrideUrlLoading 方法),如下面的演示所示:

    public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_main);
    
        WebView mWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(false);
        mWebView.setVerticalScrollBarEnabled(true);
        mWebView.setScrollBarStyle(0);
    
        /*
         * Your web view client that listen for your commands.
         */
        mWebView.setWebViewClient(new CustomWebViewClient());       
        mWebView.loadUrl("<YOUR URL TO LOAD>");
    
    }
    
    /**
     * Your Webview Client.
     * @author ru
     *
     */
    public class CustomWebViewClient extends WebViewClient {
    
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            Log.d("WEB_ERROR", "error code:" + errorCode + ":" + description);
    
            Toast.makeText(
                    MainActivity.this,
                    "Your Internet Connection May not be active Or "
                            + description, Toast.LENGTH_SHORT).show();
        }
    
        @Override
        // Method where you(get) load the 'URL' which you have clicked
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
    
            try {
                if (url != null && url.startsWith("http://")) {
                    view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    return true;
                } else {
                    return false;
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        }
    
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            // mWebView.setVisibility(View.VISIBLE);
        }
    
     }
    
    }
    

    我希望这会对您和所有人有所帮助! :)

    【讨论】:

    • 感谢您的回复,但我的网址不只是来自 webservice 的内容。所以它是字符串格式的。
    • 这也适用于您的情况。因为,正如您所说,您正在从 Web 服务获得响应(这意味着您正在获得可以直接加载到 Web 视图中的 HTML)。当您在WebView 中成功加载此内容并单击上面提到的关注“URL”,然后shouldOverrideUrlLoading 处理此请求。
    猜你喜欢
    • 1970-01-01
    • 2012-03-07
    • 2017-09-26
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 2011-10-23
    • 1970-01-01
    相关资源
    最近更新 更多