How to make Post request from Android WebView Client? - android

I need to make x-www-form-urlencoded post request from Android webView client. I am using EncodingUtils for encoding. Here is my code:
String postdata = "sUrl=http%3A%2F%2Flocalhost%3A9000%2Fpayment%2Fpayu%2Fresponse&......."
.......
.......
webView.postUrl("https://test.payu.in/_payment", EncodingUtils.getBytes(POSTDATA, "BASE64"));
I am using BASE64 but what supposed to be there for x-www-form-urlencided? I tried to search but nothing is there. What is the right approach to do it?

Its simple, use a html form and put it inside viewview
webView.loadDataWithBaseURL("file:///android_asset/index.html", , "text/html", "UTF-8", null);

Related

android webview can not show content by loaddatawithbaseurl sometime

I use webview to load local html data like this
webview.loadDataWithBaseURL("about:blank", finalSrc, "text/html", "UTF-8", null);
the finalSrc is a variable of html string.
sometimes, the webview can display the corrent content,but sometimes not.
and I found that if I clear the cache of my app, the webview works well again.
so what's wrong with my app?
rather than using BaseUrl why don't use loadData. for example
webview.loadData(finalSrc, "text/html", "UTF-8");

Android Webview POST request

I'm trying to do a post request with a WebView on Android.
After searching for days and trying dozens of things i couldn't get it work. In SWIFT it's just a few lines of code so i thought there must also be a simple way to do a post request for a webview on android.
As (for 2016) EncodingUtils and HTTPClient are deprecated this are my current approaches:
String url = "http://example.com/php.php";
String postData = null;
postData = "param1=" + URLEncoder.encode("1234567890", "UTF-8");
webcontent.postUrl(url,postData.getBytes());
//or
webcontent.postUrl(url, Base64.encode(postData.getBytes(), Base64.DEFAULT));
Both just result in a blank screen. There is just one parameter to be sent and a string containing html from the server should be received.
In addition, the php on the server returns a html-string with colored background irrespective of any input, but even this isn't displayed so maybe the whole request never reaches the server?
Thanks in advance!
In Android you do not use webView to access the content of the HTTP response. You'll need to use HttpClient for that purpose!
See this nice tutorial which explains the fundamentals! Also see this video if you find it hard!
Hope it helps!

Android webview automatically decode string

We have send a POST request to android web view , but the web view did some url decode automatically and the server side get a wrong data.
Eg: we have a signature value like aedTH5634+hjsGT78-67ty when we POST this value through webview , webview automatically convert + value to space.SO in the server signature value is wrong.How I Avoid this decode.
IOS webview workig fine it send exact value what we have POST.How we avoid this decoding from the android webview.
Help is highly appreciable,
Thanks,
not sure how it will help others with a similar situation but i will still give my $0.02, I was able to solve my issue like this, I used the following methods and ensured that the encoding is retained for encoding value use base64 and convert the data to base 64, read more about this here
void loadData (String data,
String mimeType,
String encoding)
convert data to base64 like this
String base64Data = android.util.Base64.encodeToString(yourdata.getBytes("UTF-8"), android.util.Base64.DEFAULT);
and finally put everything together like this
webView.loadData(base64Data, "text/html; charset=utf-8", "base64");
Maybe an Android WebView Bug?
Just a workaround:
webView.loadUrl(URLEncoder.encode(yourStr));
code works.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
this.evaluateJavascript(javascriptCommand, null);
} else {
this.loadUrl(javascriptCommand);
}

android webview postUrl change user-agent

all
I want to POST url in webview with custom user-agent,how can i do it?
I know
webView.loadUrl(url,extraHeaders);
can with the custom head data,but the request is GET
and,
webView.postUrl(v, EncodingUtils.getBytes(data, "base64"));
can POST the data,but it can't change the head data...
How can I do it ?
Before using postUrl have your tried something along the lines of:
WebSettings settings = mWebView.getSettings();
settings.setUserAgentString("<Add User Agent Here>");
mWebView.postUrl(.....);

Had to load data twice to make WebView refresh in Android

When I first create the activity, everything goes fine. However, after I choose from menu to change some text of the String values and set the webview by
webview.loadData(result, "text/html; charset=UTF-8", null);
webview.loadData(result, "text/html; charset=UTF-8", null);
I have to do it twice, or the webview will keep unchanged. Is there anyone knows what happens here? Since the result String is the same, why webview force me to loadData twice?
Avoid WebView#loadData(String data, String mimeType, String encoding) - it's buggy.
Use WebView#loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) instead.
So your instruction will be like:
webview.loadDataWithBaseURL(null,result,"text/html", "utf-8", null);
Don't know what's your problem but looking at the webview documentation, you are using the loadData method wrongly :
Webview:loadData documentation
You probably should call your webview like this :
webview.loadData(result, "text/html", "UTF-8");
Don't know if it will solve your issue at all.
Yes with loadDataWithBaseURL it does refresh the data, but then it ignores the CSS body background-color! ... At least it can't parse "%23000000" which works with loadData.
I am loading local HTML data into my webview, and this webview is inside recyclerview,
When I try webview.loadData() when it renders 1st time it working fine, but when I scrolling upward downward every inflated webview`s get messed-up.
When I try second webview.loadDataWithBaseURL() its working like charm.
so,when you're loading the HTML locally and it references assets such as images & css which are also packaged locally use webview.loadDataWithBaseURL()

Categories

Resources