Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Android设备向服务器发送JSON消息_Json_Http_Servlets_Url Encoding - Fatal编程技术网

从Android设备向服务器发送JSON消息

从Android设备向服务器发送JSON消息,json,http,servlets,url-encoding,Json,Http,Servlets,Url Encoding,我有一个Android设备,它可以创建JSON格式的消息并将其发送到Tomcat服务器。服务器正在运行一个Java Web应用程序,该应用程序接收此JSON格式的消息,然后将对其进行处理 我的问题是,当服务器接收到消息时,服务器会将某些字符('{'、'['、'“'、']'和'}')替换为它们的十六进制表示形式,这会导致服务器无法识别消息的JSON错误 我的问题是,是否有人知道是什么导致了这个问题,以及我将如何着手解决这个问题(我确信这不是JSON代码,而是消息在发送/接收方面的处理方式) 以下是

我有一个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}
    ]
}
这是服务器查看消息的方式(即
消息
字符串的内容):

如您所见,消息字符串具有各种十六进制值来替换某些字符

非常感谢

编辑:我想我应该包括我的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());
}
HttpClient http_client=newdefaulthttpclient();
//设置长超时限制
HttpConnectionParams.setConnectionTimeout(http_client.getParams(),10000);
HttpConnectionParams.setSoTimeout(http_client.getParams(),10000);
HttpResponse响应;
//设置json\u对象
JSONObject json_object=convertImageFeaturesToJSON(数据,x,y);
试一试{
HttpPost http_post=新的HttpPost(URL_BASE+“/featureuploader”);
i(标记,json_object.toString());
StringEntity se=新的StringEntity(json_object.toString());
setContentType(“application/json;charset=UTF-8”);
setContentEncoding(新的BasicHeader(HTTP.CONTENT_TYPE,“application/json;charset=UTF-8”);
http_post.setEntity(se);
http_post.setHeader(“接受”、“应用程序/json”);
http_post.setHeader(“内容类型”、“应用程序/json”);
response=http\u client.execute(http\u post);
if(响应!=null){
String response_String=httpResponseToString(response.getEntity().getContent());
如果(!response_string.startsWith(“Received:”)){
Log.i(标记“Received:ERROR!-”+response_string.substring(0,20));
}否则{
Log.i(标记“接收:”+响应字符串);
}
}否则{
Log.e(标记“未从服务器接收”);
}               
}捕获(例外e){
Log.e(标记“Error:+e.getMessage());
}
这是服务器查看消息的方式

这是您在服务器上看到的JSON的URL编码版本。您需要先对其进行解码(使用类似的方式),或者以解码后的方式从请求中检索它

这是服务器查看消息的方式

这是您在服务器上看到的JSON的URL编码版本。您需要先对其进行解码(使用类似的方式),或者以解码后的方式从请求中检索它

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());
}