我对此有点不知所措。在
我尝试从Android客户机发送一个JSON对象到Python Flask服务器。
我按以下方式寄出:System.out.println("Building the http request");
URL url = new URL(getString(R.string.home_server_url_send_records));
SendTracksObject sendTracksObject = buildRecordsJson();
JSONArray listOfRecords = sendTracksObject.listOfRecordsJsonArray;
JSONObject sendToServerObject = new JSONObject();
sendToServerObject.put("Track_History", listOfRecords);
System.out.println("Sending: " + sendToServerObject.toString());
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(sendToServerObject.toString());
String prettyJsonString = gson.toJson(je);
System.out.println(prettyJsonString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Cache-Control", "no-cache");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
os.writeBytes(sendToServerObject.toString());
os.flush();
os.close();
int responseCode = conn.getResponseCode();
String responseMessage = conn.getResponseMessage();
System.out.println(responseCode + ": " + responseMessage);
conn.disconnect;
这对我尝试发送的大多数内容都有效,但是一旦有一个不是ASCII的字符,比如é或ð,在服务器试图破译其中的内容之前,我就会收到400个错误。我想这是因为编码扰乱了请求的结构,而Flask不知道如何解释结果。有趣的是,当我用邮递员发送完全相同的请求时,它就起作用了。在
如何确保我要发送的字符串的编码不会与烧瓶相混淆?在