我对此有点一无所知。
我尝试从Android客户端向Python Flask服务器发送一个JSON对象。我通过以下方式发送:
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不知道如何解释结果。有趣的是,当我使用PostMan发送完全相同的请求时,它可以正常工作。
如何确保我尝试发送的字符串的编码不会与Flask打乱?
https://stackoverflow.com/questions/47560489
复制相似问题