无法在android中检测webview中的http url

时间:2013-12-11 12:56:29

标签: android url webview hyperlink

我通过点击链接检测webview中的网址,它应该在应用程序外打开网址。

我的代码如下:

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

获取错误: - 网页不可用

提前致谢....

enter image description here

enter image description here

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

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

enter image description here

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您需要覆盖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);
    }

 }

}

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