How to make webview posts?

I want to make an HTTP request using webview.

webView.setWebViewClient(new WebViewClient(){ public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } public boolean shouldOverrideUrlLoading(WebView view, String url) { webView.postUrl(Base_Url, postData.getBytes()); return true; } }); 

The above code snippet loads a web page. I want a response to this request.

How can I get an HTTP request response using webview?

Thanks at Advance

+4
source share
2 answers

WebView does not allow you to access HTTP response content.

To do this, you need to use HttpClient , and then forward the content to the view using the loadDataWithBaseUrl function and specifying the base URL so that the user can use web browsing to continue navigating the website.

Example:

 // Executing POST request HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); httppost.setEntity(postContent); HttpResponse response = httpclient.execute(httppost); // Get the response content String line = ""; StringBuilder contentBuilder = new StringBuilder(); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); while ((line = rd.readLine()) != null) { contentBuilder.append(line); } String content = contentBuilder.toString(); // Do whatever you want with the content // Show the web page webView.loadDataWithBaseURL(url, content, "text/html", "UTF-8", null); 
+5
source

First add the http library support to the gradle file: To be able to use

useLibrary 'org.apache.http.legacy'

After that, you can use the following code to execute the mail request in your webview:

 public void postUrl (String url, byte[] postData) String postData = "submit=1&id=236"; webview.postUrl("http://www.belencruzz.com/exampleURL",EncodingUtils.getBytes(postData, "BASE64")); 

http://belencruz.com/2012/12/do-post-request-on-a-webview-in-android/

+6
source

Source: https://habr.com/ru/post/1446922/


All Articles