【发布时间】:2012-06-07 10:35:56
【问题描述】:
我正在向 web 服务发送文本,但首先转换为字节,然后在服务器端接收到我想将字节更改回字符串。我已经能够转换为字节,但无法转换回字符串。我查看了很多关于 SO 的教程,但到目前为止似乎都没有。
这是客户端应用程序
try {
String text = "holiday";
byte[] byte_text = text.getBytes();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI link = new URI("http://192.165.1.3:8080/HelloWorld/hello?text="+byte_text);
request.setURI(link);
HttpResponse response = client.execute(request);
} catch (Exception e1) {
e1.printStackTrace();
}
这是我在客户端所做的
byte[] why = req.getParameter("text").getBytes();
String text = new String(why);
System.out.println(text);
当我调用 getbyte 和 new String 函数时,我尝试使用 UTF 和 ASCII 编码,但我仍然得到像这样的字节结果 [B@405c0ce0
【问题讨论】:
-
我在 JB;你到底想完成什么?
-
为什么?将字节数组附加到 String 不会做您认为它会做的事情,而附加 String does 会做您认为前者应该做的事情。转换的意义何在?
标签: java string web-services servlets character-encoding