2012-12-22 106 views
1

我有一個Android設備創建一個JSON格式的消息,並將其發送到Tomcat服務器。服務器正在運行一個Java Web應用程序,它接收這個JSON格式的消息,然後處理它。從Android設備發送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()); 
} 

回答

1

這是怎樣的消息是由服務器看到...

這是一個網址編碼版本你在服務器上看到的JSON。您需要先解碼(使用類似URLDecoder的東西),或者以請求解碼的方式從請求中檢索它。

相關問題