Android 如何在 WebView 中获取 POST 请求的 JSON 响应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10213487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:54:01  来源:igfitidea点击:

How can I get the JSON response of a POST request in a WebView?

androidjsonpostandroid-webview

提问by jul

I've got a WebViewwith html containing a form (post). When clicking some submit button I get a JSON response.

我有一个WebView包含表单(post)的 html。单击某个提交按钮时,我会收到 JSON 响应。

How can I get this JSON?

我怎样才能得到这个 JSON?

If I don't intercept the request the json is displayed on the webview, so I guess I should use shouldInterceptRequest(I'm using API 12), but I don't know how to get the json in it.

如果我不拦截请求,json 会显示在 webview 上,所以我想我应该使用shouldInterceptRequest(我使用的是 API 12),但我不知道如何在其中获取 json。

Or maybe there's a better way, like intercepting the response instead of the request?

或者也许有更好的方法,比如拦截响应而不是请求?

mWebView.loadUrl(myURL);  

isPageLoaded = false; // used because the url is the same for the response

mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public WebResourceResponse shouldInterceptRequest (WebView view, String url) {

        if(isPageLoaded){
            // get the json
            return null;
        }
        else return super.shouldInterceptRequest(view, url);
    }

    public void onPageFinished(WebView view, String url) {
        isPageLoaded = true;
    }
});

Thanks

谢谢

采纳答案by Ronnie

You should override the shouldOverrideUrlLoadingmethod of WebViewClient

你应该覆盖的shouldOverrideUrlLoading方法WebViewClient

@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {

    if(flag) { 

            URL aURL = new URL(url); 
            URLConnection conn = aURL.openConnection(); 
            conn.connect(); 
            InputStream is = conn.getInputStream(); 
            // read inputstream to get the json..
            ...
            ...
            return true;
    }

    return false
}

@override
public void onPageFinished (WebView view, String url) {
    if (url contains "form.html") {
        flag = true;
    }
}

Also take a look at this How do I get the web page contents from a WebView?

也看看这个如何从 WebView 获取网页内容?

回答by Abs

Here is how i achieved this as the method mentioned started to complain about UI thread network connection.

这是我如何实现这一点,因为提到的方法开始抱怨 UI 线程网络连接。

Using ChromeWebView Client and console log the output

使用 ChromeWebView 客户端和控制台记录输出

    public void OnSignUp_Click(View v) {
    WebView mWebview = new WebView(this);

    mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

    final Activity activity = this;

    mWebview.setWebChromeClient(new WebChromeClient() {
        public boolean onConsoleMessage(ConsoleMessage cmsg)
        {

                /* process JSON */
            cmsg.message();
            return true;

        }
    });

    mWebview.setWebViewClient(new WebViewClient() {

        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            Log.e("Load Signup page", description);
            Toast.makeText(
                    activity,
                    "Problem loading. Make sure internet connection is available.",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            view.loadUrl("javascript:console.log(document.body.getElementsByTagName('pre')[0].innerHTML);");
        }
    });

    mWebview.loadUrl("https://yourapp.com/json");

}

Hope it helps others.

希望它可以帮助其他人。

回答by pacman

Well, the short answer is that it works quite similar to shouldOverrideUrlLoading(WebView view, String url), as illustrated in the WebView tutorial.

To get you started, see the code below. You simply override the shouldInterceptRequest(WebView view, String url) method of your WebViewClient. Obviously you don't have to do that inline, but for the sake of compactness that's what I did:

嗯,简而言之,它的工作方式与 shouldOverrideUrlLoading(WebView view, String url) 非常相似,如 WebView 教程中所示。

要开始使用,请参阅下面的代码。您只需覆盖 WebViewClient 的 shouldInterceptRequest(WebView view, String url) 方法。显然,您不必内联,但为了紧凑起见,我就是这样做的:

Take a look at this.

看看这个