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
Android: How to Send an Image as Email Attachment from Application
Jsonexception: Value of Type Java.Lang.String Cannot Be Converted to Jsonobject
Room Delete Query Does Not Remove the Row in Database
Apk Installation Failed: [Install_Failed_Verification_Failure]
Edittext Background Base Line Color Changing Based on Its Focus in Android
Converting Edittext to Int - Android
How to Make Image Fill Relativelayout Background Without Stretching
Adb Error (Error: Protocol Fault (Couldn't Read Status): Invalid Argument)
Android Adb Devices Does Not Detect My Phone
Concatenate Multiple Strings in Xml
How to Set the Image from Drawable Dynamically in Android
How Remove Unnecessary Space Between Cards in Recyclerview
How to Add a Newline to a Textview in Android
How to Kill Native Applications from 'Adb Shell'
How to Get the Full File Path from Uri
What Is the Simplest and Most Robust Way to Get the User'S Current Location on Android