由于JSON字段上的重音而导致错误400

时间:2017-02-06 08:45:13

标签: java json

我在URL上发出GET请求,并在响应中检索一些信息。其中一条信息包含重音符号。我在身体上发出了这些信息和其他人的PUT请求。

当我提出此请求时,由于重音,我有400错误。

我提出请求时如何接受重音?

CODE

GetInformation

ArrayList<String> infos = new ArrayList<>();


String urlGet = "URL";
URL obj = new URL(urlGet);
HttpURLConnection conGet = (HttpURLConnection) obj.openConnection();

//add request header
conGet.setRequestMethod("GET");
conGet.setRequestProperty("Content-Type", "application/json");

String response = sendRequest("GET", conGet, urlGet, "");

JSONObject jobj = new JSONObject(response);
JSONObject jobj2 = jobj.getJSONObject("Applications");
JSONArray jArr = jobj2.getJSONArray("Application");
JSONObject jobj3 = jArr.getJSONObject(0);
String name = jobj3.getString("Name");
String shortDescription = jobj3.getString("ShortDescriptionLocal");
String basicat = jobj3.getString("Basicat");

infos.add(name);
infos.add(shortDescription);
infos.add(basicat);

提出其他要求

String name = infos.get(0);
String shortDescription = infos.get(1);
String basicat = infos.get(2);

String urlPut = "URL";
URL obj = new URL(urlPut);
HttpURLConnection conPut = (HttpURLConnection) obj.openConnection();

//add request header
conPut.setRequestMethod("PUT");
conPut.setRequestProperty("Content-Type", "application/json");

String urlParameters = "{\"name\": " + "\"" + name + "\"" + ",\"description\": " + "\"" + shortDescription.replace("\"",  "\\\"").replace("\n"," ")}";

String response = sendRequest("PUT", conPut, urlPut, urlParameters);

有关信息,它是包含重音符号的shortDescription字段。

非常感谢你的帮助。

0 个答案:

没有答案