Android 将带有表情符号的 JSON 正文发送到 nodejs 服务器
Android send JSON body with emojis to a nodejs server
我正在尝试从 android 向我的 nodejs 服务器发送 PATCH 请求。我可以在 JSON 内发送纯文本而不会出现任何错误。但是当我在 JSON 中添加一些表情符号时,服务器会抛出 SyntaxError: Unexpected token in JSON
。我可以毫无问题地从邮递员那里发送表情符号。我想我的 android 代码有问题。
这是 android,
的代码
private JSONObject sendData(){
JSONObject jsonParam = new JSONObject();
jsonParam.put("text", "this will work fine");
jsonParam.put("payload", "This will throw error");
StringBuilder data = new StringBuilder();
JSONObject obj = null;
try {
URL url = new URL(baseUrl + path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
//os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
os.writeBytes(jsonParam.toString());
////
InputStream inputStream = conn.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while (line != null) {
line = bufferedReader.readLine();
data.append(line);
}
obj = new JSONObject(data.toString());
os.flush();
os.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
Log.e("SEND_REQUEST_ERROR", e.toString());
}
return obj;
}
private String convertStringToUTF8(String s) {
return new String(s.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
jsonParam.put("payload", convertStringToUTF8("This will work without error"));
我正在尝试从 android 向我的 nodejs 服务器发送 PATCH 请求。我可以在 JSON 内发送纯文本而不会出现任何错误。但是当我在 JSON 中添加一些表情符号时,服务器会抛出 SyntaxError: Unexpected token in JSON
。我可以毫无问题地从邮递员那里发送表情符号。我想我的 android 代码有问题。
这是 android,
private JSONObject sendData(){
JSONObject jsonParam = new JSONObject();
jsonParam.put("text", "this will work fine");
jsonParam.put("payload", "This will throw error");
StringBuilder data = new StringBuilder();
JSONObject obj = null;
try {
URL url = new URL(baseUrl + path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Accept", "application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
//os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
os.writeBytes(jsonParam.toString());
////
InputStream inputStream = conn.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while (line != null) {
line = bufferedReader.readLine();
data.append(line);
}
obj = new JSONObject(data.toString());
os.flush();
os.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
Log.e("SEND_REQUEST_ERROR", e.toString());
}
return obj;
}
private String convertStringToUTF8(String s) {
return new String(s.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
jsonParam.put("payload", convertStringToUTF8("This will work without error"));