Android webview automatically decode string - android

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);
}

Related

Jsoup connect doesn't work correctly when there is an absolute link?

I'm using Jsoup to get html from web sites. for example I have the links in my HTML page look like this:
String url="http://kitchen.sayidaty.net/node/8544/كوكيزبالشوفان/حلويات";
Document doc=Jsoup.connect(url).get();
this link parse NOTHING and throws IOException, but when I open this link manually in browser and take it back to my code it get changed and works fine ! like this :
String url="http://kitchen.sayidaty.net/node/8544/%D9%83%D98%B2-%D8%A7%D9%86/%D8%AD%D9%84%D9%88%D9%8A%D8%A7%D8%AA#ingredients";
Document doc=Jsoup.connect(url).get();
is there any way to get the absolute link (the second one) , I tried this but same result :'(
link.attr("abs:href")
The problem you're facing has to do with the charset specification http protocol handle when you do transactions like POST and GET. You must use a application/x-www-form-urlencoded MIME format (For more information about HTML form encoding, consult the HTML specification).
In your case, for this to work you have to use Android URLencoder, but only at the end of the address you want to use, to avoid problems. So transform:
String url="http://kitchen.sayidaty.net/node/8544/كوكيزبالشوفان/حلويات";
Into:
String auxUrl= URLEncoder.encode("كوكيزبالشوفان/حلويات", "utf-8");
String url="http://kitchen.sayidaty.net/node/8544/" + auxUrl;
Then proceed with your
Document doc=Jsoup.connect(url).get();

encoded strings issues

I am working on my very first Android application, I am getting some Arabic messages in the JSON response from a web service in two different formats. when I display one of them get translated correctly but other get printed as it is in the encoded message.
Here is the first one:
\u0635\u0641\u0631 \u0627\u0644\u0645\u0638\u0641\u0631
This is converted to proper Arabic string as intended.
but
کامران
does not, I was expecting that its a UTF-8 encoded message, but I'm unable to convert it. can anyone help me to understand this encoded message?
Here is how I tried to convert but its unchanged:
public String decodeString(String encodedString) {
try {
return new String(encodedString.getBytes(), "UTF-8");
} catch(Exception e){
e.printStackTrace();
return encodedString;
}
}
any help is appreciated.
Thank you very much for your time and assistance in this matter.
When I check (کامران) message here: http://www.cafewebmaster.com/online_tools/utf8_decode I get the correct response.
Please also share some details on the encoding scheme i-e what is the difference between both encodings.
کامران Looks like the HTML encoding of the Unicode code points. You'll need to decode the HTML, using e.g Apache commons StringEscapeUtils.unescapeHtml().
Here's the gradle dependency for the library:
compile 'commons-lang:commons-lang:2.6'

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!

How to make Post request from Android WebView Client?

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);

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