How to Get the Json Response of a Post Request in a Webview

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

You should override the shouldOverrideUrlLoading method of 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?

android webview working with json post and response

If you just wish to post JSON using HTTP and get a response back, there are many posts of stackoverflow which will help you answer that. Check How to send POST request in JSON using HTTPClient? question. I think this question answers what you are trying to say. Hope this helps you. If you have any specific concern you can always comment.

Update
As you said that you already have keys and corresponding values in addition to URL.

The first step would be to create a JSON Object. Convert it to string and then you can send it using HTTPClient and get the response back. Something like:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/");

try {
// Add your data

JSONObject user = new JSONObject();
user.put("Name", "a");
user.put("pass", "123");

// Create StringEntity
StringEntity se = new StringEntity( user.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}

You can check links like this to see further exact format. I wanted to tell you the method as to how you can proceed. Hope this helps.

Get Json response when Webview redirect to another page in Flutter

I was not aware that I found the solution because the html content was very complex. By using webview_flutter plugin problem is sorted. Build your widget as it is;

WebView(
initialUrl: new Uri.dataFromString(widget.d3sc, mimeType: 'text/html').toString(),

onWebViewCreated: (controller) {
_controller = controller;
},
javascriptMode: JavascriptMode.unrestricted,
gestureNavigationEnabled: true,
onPageFinished: (_) {
readResponse();
},
)

readResponse function is called whenever navigation occurs and page opening finished. We can get page's html content as string by using following function;

    void readResponse() async
{


setState(() {
_controller.evaluateJavascript("document.documentElement.innerHTML").then((value) async {

if(value.contains("name=\"paymentId\"")){

// value contains the html data of page as string
...

I could not convert html data to json map but we can reach whatever we want in this string by simply using substring method.

Note: webview_flutter version ^1.0.7 is used for solution in this case.



Related Topics



Leave a reply



Submit