我有一个 Android 设备,它创建 JSON 格式的消息并将其发送到 Tomcat 服务器。服务器正在运行一个 Java Web 应用程序,该应用程序接收此 JSON 格式的消息,然后对其进行处理。
我的问题是,当服务器收到消息时,服务器会将某些字符('{'、'['、'"'、']' 和 '}')替换为看起来像它们的十六进制表示,这会导致服务器无法识别消息的 JSON 错误。
我的问题是是否有人知道导致此问题的原因以及我将如何解决此问题(我确定这不是 JSON 代码,而是消息在发送/接收方面的处理方式)。
这是我的服务器端代码:
@Override
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Setup response
PrintWriter out = response.getWriter();
// Store input message as String
StringBuilder sb = new StringBuilder();
String message;
// Build String
while ((message = request.getReader().readLine()) != null) {
sb.append(message);
}
// Full message as String
message = sb.toString();
// Convert message to JSON format
Gson gson = new Gson();
ImageFeatures features = gson.fromJson(message, ImageFeatures.class);
out.println("Normal data: " + features);
out.println();
}
这是我收到的错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 12
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
com.google.gson.Gson.fromJson(Gson.java:795)
com.google.gson.Gson.fromJson(Gson.java:761)
com.google.gson.Gson.fromJson(Gson.java:710)
com.google.gson.Gson.fromJson(Gson.java:682)
这是在 Android 设备将消息发送到服务器之前生成的 JSON 格式的消息:
{
"image_x_size":800,
"image_y_size":1600,
"features":
[
{"cart_x":5.0,"cart_y":124.0,"polar_angle":0.0,"polar_dist":0.0,"size":15.0},
{"cart_x":5.0,"cart_y":124.0,"polar_angle":0.0,"polar_dist":0.0,"size":15.0}
]
}
这是服务器看到消息的方式(即message
字符串的内容):
%7B+%09%22image_x_size%22%3A800%2C+%09%22image_y_size%22%3A1600%2C+%09%22features%22%3A+%09%09%5B+%09%09%09%7B%22cart_x%22%3A5.0%2C%22cart_y%22%3A124.0%2C%22polar_angle%22%3A0.0%2C%22polar_dist%22%3A0.0%2C%22size%22%3A15.0%7D%2C+%09%09%09%7B%22cart_x%22%3A5.0%2C%22cart_y%22%3A124.0%2C%22polar_angle%22%3A0.0%2C%22polar_dist%22%3A0.0%2C%22size%22%3A15.0%7D+%09%09%5D+%7D+
如您所见,消息字符串具有替换某些字符的各种十六进制值。
提前谢谢了!
编辑:我想我应该包括我的 Android 代码以进一步清楚
HttpClient http_client = new DefaultHttpClient();
// Set long timeout limit
HttpConnectionParams.setConnectionTimeout(http_client.getParams(), 10000);
HttpConnectionParams.setSoTimeout(http_client.getParams(), 10000);
HttpResponse response;
// Setup json_object
JSONObject json_object = convertImageFeaturesToJSON(data, x, y);
try {
HttpPost http_post = new HttpPost(URL_BASE + "/featureuploader");
Log.i(TAG, json_object.toString());
StringEntity se = new StringEntity(json_object.toString());
se.setContentType("application/json; charset=UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json; charset=UTF-8"));
http_post.setEntity(se);
http_post.setHeader("Accept", "application/json");
http_post.setHeader("Content-Type", "application/json");
response = http_client.execute(http_post);
if (response != null) {
String response_string = httpResponseToString(response.getEntity().getContent());
if (! response_string.startsWith("Received: <html>")) {
Log.i(TAG, "Received: ERROR! - " + response_string.substring(0, 20));
} else {
Log.i(TAG, "Received: " + response_string);
}
} else {
Log.e(TAG, "Did not receive from server");
}
} catch (Exception e) {
Log.e(TAG, "Error: " + e.getMessage());
}