2014-05-02 43 views
0

如何从webview中的链接检索JSON?从Webview中点击的链接获取JSON

当前我对API做了一个Httpget请求并获取我的第一个JSON并解析它,然后将其显示在我的webview中我没有与该部分有关的问题。所以,现在我有一些链接,导致我的网页介面API调用我从我刚才分析的JSON的新信息创建... EX:

String summary = "<html><body> <a href="http://www.mydomain.com/json.php">Get New JSON</a> </body></html>"; 
webview.loadData(summary, "text/html", null); 

因此,当一个用户clinks,在网页视图链接我想要获得JSON并且能够解析它。我想我必须使用“shouldInterceptRequest”?无法计算如何实现这一点。

回答

1

您应该create and set a WebViewClient并覆盖其shouldOverrideUrlLoading()方法。这样你就有机会拦截任何被点击的链接(或重定向,& c)。

例如:

webView.setWebViewClient(new WebViewClient() 
{ 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    { 
     if (url.endsWith(".json")) 
     { 
      Toast.makeText(MainActivity.this, "json found", Toast.LENGTH_SHORT).show(); 
      return true; 
     } 
     return false; 
    } 
}); 
+0

感谢工作完美! – user2423476