[英]Converting from Java String to Windows-1252 Format
I want to send a URL request, but the parameter values in the URL can have french characters (eg. è). 我想发送一个URL请求,但URL中的参数值可以包含法语字符(例如è)。 How do I convert from a Java String to Windows-1252 format (which supports the French characters)?
如何从Java String转换为Windows-1252格式(支持法语字符)?
I am currently doing this: 我目前正在这样做:
String encodedURL = new String (unencodedUrl.getBytes("UTF-8"), "Windows-1252");
However, it makes: param=Stationnement extèrieur
into param=Stationnement extérieur
. 然而,它使:
param=Stationnement extèrieur
进入param=Stationnement extérieur
。
How do I fix this? 我该如何解决? Any suggestions?
有什么建议么?
Edit for further clarification: 编辑以进一步说明:
The user chooses values from a drop down. 用户从下拉列表中选择值。 When the language is French, the values from the drop down sometimes include French characters, like 'è'.
当语言为法语时,下拉列表中的值有时包括法语字符,如'è'。 When I send this request to the server, it fails, saying it is unable to decipher the request.
当我将此请求发送到服务器时,它失败,说它无法解密请求。 I have to figure out how to send the 'è' as a different format (preferably Windows-1252) that supports French characters.
我必须弄清楚如何将'è'作为支持法语字符的不同格式(最好是Windows-1252)发送。 I have chosen to send as Windows-1252.
我选择发送Windows-1252。 The server will accept this format.
服务器将接受此格式。 I don't want to replace each character, because I could miss a special character, and then the server will throw an exception.
我不想替换每个字符,因为我可能会错过一个特殊字符,然后服务器将抛出异常。
Use URLEncoder to encode parameter values as application/x-www-form-urlencoded data: 使用URLEncoder将参数值编码为application / x-www-form-urlencoded data:
String param = "param="
+ URLEncoder.encode("Stationnement extr\u00e8ieur", "cp1252");
尝试使用
String encodedURL = new String (unencodedUrl.getBytes("UTF-8"), Charset.forName("Windows-1252"));
As per McDowell's suggestion, I tried encoding doing: 根据McDowell的建议,我尝试编码:
URLEncoder.encode("stringValueWithFrechCharacters", "cp1252")
but it didn't work perfectly. URLEncoder.encode("stringValueWithFrechCharacters", "cp1252")
但它没有完美地工作。 I replayced "cp1252"
with HTTP.ISO_8859_1
because I believe Android does not have the support for Windows-1252 yet. 我replayced
"cp1252"
与HTTP.ISO_8859_1
因为我相信Android不具备适用于Windows 1252的支持呢。 It does allow for ISO_8859_1, and after reading here , this supports MOST of the French characters, with the exception of 'Œ', 'œ', and 'Ÿ'. 它允许ISO_8859_1,阅读后在这里 ,这支持大多数法国人的字符,以“O E”,“O E”,和“Y”的除外。
So doing this made it work: 所以这样做使它工作:
URLEncoder.encode(frenchString, HTTP.ISO_8859_1);
Works perfectly! 完美的工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.