以下方法發送JSON回覆。但是在接收端,我一直收到無效字符,而UTF-8不解碼數據。我究竟做錯了什麼?GSON不以UTF-8發送
響應客戶端=數據輸出流
//Get the client request
clientRequest = new BufferedReader(new InputStreamReader(connectedClient.getInputStream())); //connectedclient = socket
//Start response object
responseToClient = new DataOutputStream(connectedClient.getOutputStream());
/**
* Sends a JSON response for an object
* @param objectToEncode
* @throws Exception
*/
private void sendJSONResponse(Object objectToEncode) throws Exception{
//Encode object into JSON
String jsonString = new Gson().toJson(objectToEncode);
// HTTP Header... Status code, last modified
responseToClient.writeBytes(HTTP_OK_STATUS_CODE);
responseToClient.writeBytes(CONTENT_TYPE_JSON);
responseToClient.writeBytes("Last-modified: "+ HelperMethods.now() +" \r\n");
responseToClient.writeBytes("\r\n");
// The HTTP content starts here
responseToClient.writeBytes(jsonString);
}
什麼是'responseToClient'?你自己在編寫你的HTTP代碼嗎? –
是的。 HTTP代碼已列出(也是responseToClient是DataOutputStream –
@WilliamFalcon,我有ArrayList對象,其中包含許多中文和日文字符,我如何使用GSON庫序列化它們,通常意味着我將這些字符鬆開並獲取**?**而不是它們 –