i want to get name and last name of application users by EditText view and send it to soap webservice to store in data base , i have problam when users enter data in arabic or persian it save data like this "?????" i try this code change encoding befor send data via webservice :
newLastName = new String(URLEncoder.encode(lastname.getText().toString(), "UTF-8"));
but when i check the encoding of string in server side with this code :
return mb_detect_encoding($lastName);
it`s return me ASCII !
how can i slove this problam ?
thanks
i send "سلام" to webservice and its return me "????" my problem is in php encoding system or in java encoding system ?
You should be able to use the URLDecoder.decode() function, like this:
String s = URLDecoder.decode(myString, "UTF-8");
more info string encoding easily
Related
I have a problem sending an Arabic text in email with java language.
this is my message in properties file:
mail.send=تجربة
I use this syntax in jave :
ResourceBundle dq_resource = ResourceBundle.getBundle("nls.myfile_ar");
String text= dq_resource.getString("mail.send")
but when I received email U have this text : اÙ?Ù?Ù?ضÙ?ع
I try also in java with this code :
String text= new String(dq_resource.getString("mail.send").getBytes(),Charset.forName("UTF-8"));
but I have this text in mail :
ا�?�?�?ض�?ع
You need to set a header for the mail, something like
message.setHeader("Content-Type", "text/plain; charset=UTF-8");
setHeader is a method of the Message class that allows you to set a header.
The easiest solution could be using strings normally ( without getBytes method ) changing the default encoding in your workspace for example eclipse.
Windows-->Preferences-->General-->workspace-->Text file encoding
Also you can try to convert UTF-8 to UTF-16 .
I am using the Java mail API to retrieve the emails from gmail via Imap and show it in the Webpage powered with AngularJS.
When I get the data for an email using javax.mail.Message.getContent() return as Object with charset - gb2312.
But my web page is using the UTF-8 charset, so while i am facing strange characters in the web page for some.
I need to convert from gb2312(or any) charset to the utf-8 to show correctly in the webpage.
Can anyone help with this ?
You can create a new String like this and convert it to UTF-8:
String s = new String(bytes, "OriginalCharset");
byte[] utfBytes = s.getBytes("UTF-8");
I think Java uses UTF-8 natively, but it's better to do it explicitly.
Is there a possibility to send a json text with binary data?
I'am calling it multipart/form-data cause in the html form we can send text with a binary file, how to do the same with the websockets, is that possible?
so here is an example:
var arrayBuffer = new ArrayBuffer();
// lets say here we fill our array buffer with binary data in the browser
// so here we got 2 ways
// 1) First we can send the json text indicating that the next request will be a binary data file
// so using this way the next request will have ready an Id for the uploaded file if we are working
// with database
var obj = {};
obj.somedata = "hello This is a name for my item";
websocket.send(JSON.stringify(obj)); //first sending the text
websocket.send(arrayBuffer); //sending the binary data file
// 2) Second, This is the way I prefer most, but I don't know if this is possible
// we send a json object
var obj = {};
obj.somedata = "hello This is a name for my item";
obj.file = arrayBuffer;
websocket.send(JSON.stringify(obj));
but the problem is that I can't put a binary data into a text json, the binary data gets corrupted this way, in the server side I'am using java... cause using 2 requests for sending a file and the input text data is not a good way I think, any hints?
If server cannot handle binary data (which is maybe problem with configuration or your program, I'm sending binary data this way without no problem), then you can encode your data in base64 or escape them into json. But the best way would be repairing server side...
If you wanna use base64 way, look at this: https://github.com/niklasvh/base64-arraybuffer, and for example this: Base64 Encoding in Java
I programmed an Android App, which manages data to be stored and deleted in a MySQL database (on server). Whenever on the smartphone special characters ("ä", "ü", ...) are used the symbol is converted badly.
In the log I can see that the "Umlaut" (e.g. "ä") is transmitted properly. I also use in my php file "SET NAMES 'UTF-8'", see here:
function connect()
{
....
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
mysql_query("SET NAMES 'utf8'");
// Selecting database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
return $con;
}
If I start a request via Postman, I can add words with special characters, e.g. "TÄST", the json response looks as follows:
{"catId":"35","category":"T\u00c4ST"}
So the words are well converted to UTF-8. But if I add an Item via smartphone, the response from the server (to retrieve the added item) looks like this:
{"catId":"37","category":"T?ST"}
The position in my code, where I add the parameters for the JSON Object is that (note that "name" is the string content of the edit text field):
List<NameValuePair> params = new ArrayList<NameValuePair>();
...
params.add(new BasicNameValuePair("category", catName));
After that the HTTPRequest is send:
JSONObject json = jsonParser.makeHttpRequest(url_dest, "POST", params);
If I print out the params the word "Täst" is visible... But unfortunately I'm not able to check which json string is arriving on the server (due to my bad php knowledge). So where's the problem? In my android application or in the php files located on the server?
Do I have to encode the outgoing json object in any way?
Thanks a lot for your help.
To debug on Eclipse I suggest you:
http://projects.eclipse.org/projects/tools.pdt
It is quite easy to configure.
Control the encoding in the DB; maybe the VARCHAR default latin1_swedish_ci can't save that string.
Another way to solve it could be looking to the apache commons:
http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html
in particular the functions provided by: escapeHtml4 and unescapeHtml4
#dafocus, if you set your Android app project to UTF-8 encoding in your IDE, it should ensure that UTF-8 is handled properly in the app. It looks like an encoding change somewhere. You might not need to convert TÄST to T\u00c4ST at all if the encoding is UTF-8 throughout. PHP used to be difficult with charsets. I would suggest you to look at the PHP to figure out if the handler page causes this (possible) and/or the DB - as #madx just said.
I have a russian string "этикетка". This is need to send to a web service, before sending to the web service i use encodeURIComponent to encode the string and it gives me:
'%D1%8D%D1%82%D0%B8%D0%BA%D0%B5%D1%82%D0%BA%D0%B0'
On the web service side is receive the string and decode it using the following code:
String strLbl = java.net.URLDecoder.decode(label);
but i don't get the string properly. It looses formatting and I get ѿтикетка.
Can you please suggest how can i overcome this or what is the ideal way to send russian string
Thanks and regards
As explained in the link given by NULL, decode(string) is now Deprecated in the favour of decode(string, encoding)
I would guess that the encoding and decoding method are not using the same page code.
Did you try to force UTF-8 during both process?
I misunderstood your question be the formatting of it.
Use decodeURIComponent to decode url encoded strings in JavaScript:
> decodeURIComponent('%D1%8D%D1%82%D0%B8%D0%BA%D0%B5%D1%82%D0%BA%D0%B0')
"этикетка";