문제

How can I retrieve the JSON from a link in webview?

Currently I do a Httpget request to a API and get my first JSON and parse it then I display it in my webview I have no issues with that part. So now I have some links that lead to a API call in my webview i created with the new info from the json I just parsed... 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);

So when a user clinks that link in the webview i want to get the JSON and be able to parse it. I think I have to use "shouldInterceptRequest"? Can't figure how to implement that.

도움이 되었습니까?

해결책

You should create and set a WebViewClient and override its shouldOverrideUrlLoading() method. That way you get the chance to intercept any links clicked (or redirections, &c).

For example:

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;
    }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top