我有一个WebView包含表单(帖子)的html。单击某些提交按钮时,我会收到JSON响应。
WebView
如何获取此JSON?
如果我不截取请求,则json将显示在Web视图上,所以我想应该使用shouldInterceptRequest(我正在使用API 12),但是我不知道如何在其中获取json。
shouldInterceptRequest
还是有更好的方法,例如拦截响应而不是请求?
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; } });
谢谢
您应该重写的shouldOverrideUrlLoading方法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; } }