在Java中,我有一个String,我想将其编码为字节数组(UTF8或其他编码).或者,我有一个字节数组(在一些已知的编码中),我想将其转换为Java字符串.我该如何进行这些转换?
我正在开发项目,其中包括服务器(JavaEE app)和客户端(Android app)的通信.XML作为HTTP请求的POST参数之一发送(名为"xml").我传递给服务器的其他POST参数也很少,但在下面的功能中,为了简单起见,我删除了它们.出现的问题是某些字母未正确传送到服务器 - 例如字符?(请注意,这不是德语Ü,顺便说一下,它是正确传送的).发送代码如下:
private String postSyncXML(String XML) {
String url = "http://10.0.2.2:8080/DebugServlet/DebugServlet";
HttpClient httpclient = new DefaultHttpClient();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("xml",XML));
UrlEncodedFormEntity form;
try {
form = new UrlEncodedFormEntity(nameValuePairs);
form.setContentEncoding(HTTP.UTF_8);
HttpPost httppost = new HttpPost(url);
httppost.setEntity(form);
HttpResponse response = (HttpResponse) httpclient .execute(httppost);
HttpEntity resEntity = response.getEntity();
String resp = EntityUtils.toString(resEntity);
Log.i(TAG,"postSyncXML srv response:"+resp);
return resp;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) …Run Code Online (Sandbox Code Playgroud) 当我得到JSON然后有\ u003c和\ u003e而不是<和>.我想在java中将它们转换回utf-8.任何帮助将受到高度赞赏.谢谢.