JAVA 删除 API 和数组字符串正文
JAVA Delete API with Array String Body
提前抱歉我用谷歌搜索的英语,
我使用 API 并制作了一个允许使用它的 JAVA 软件。
我需要做一个删除和软件。
我必须执行删除,并使用提供的软件来测试 API,我看到我必须在正文中添加该行才能将其删除,如下所示:
["email","Termine",“13/03/2018 09:52:20”,等...,“”]。
正文必须包含一个字符串数组,其中包含要删除的行的所有内容。
我可以让它在测试软件中运行。
但是我不明白如何使用 JAVA 进行删除。我可以让它在软件测试中工作。这就是我现在所做的:
public static String delete(String json, String nomUrl) throws IOException {
URL url = new URL(baseUrl + "survey/"+ nomUrl + "/data");
//String json = "[\"Marc@Houdijk.nl\",\"Contacte\",\"10/04/2018 11:30:05\",\"Avoriaz\",\"Office de Tourisme\",\"Accueil OT\",\"Neerlandais\",\"Semaine 6\",\"Periode 2\",\"16\",\"\",\"Hiver 2018\",\"BJBR-CDQB\",\"04/12/2018 14:15:13\",\"04/12/2018 14:15:13\",\"04/12/2018 14:15:13\",\"\",\"Direct\",\"\",\"\",\"\"]\n";
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("DELETE");
con.setRequestProperty("Content-Type","application/json");
con.setRequestProperty("Accept","application/json");
con.setRequestProperty("Authorization","Bearer "+token);
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(json);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
StringBuilder responce = new StringBuilder();
responce.append("\nSending 'DELETE' request to URL : ").append(url);
responce.append("\nResponse Code : ").append(responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
responce.append("\n").append(inputLine);
}
in.close();
return responce.toString();
}
我为 post 和 get 所做的事情启发了我。但是我看不到如何使用我的字符串数组将正文正确添加到我的删除函数中,因为它不起作用,而且互联网也没有帮助我......
预先感谢您的帮助!
编辑:我的代码终于可以运行了。所以如果你想删除body,你可以使用这段代码。但是,问题来自 json:我是法国人,所以我的文字和特殊字符上有些重音。清理我的字符串后,一切正常。
你可以创建一个POJO class 包含RequestBody 需要的字段并将其发送到API,通过序列化对象(序列化意味着将Java 对象转换成JSON 这可以通过 GSON 库完成)。在 API 端,您可以轻松获得 ArrayList 或任何您想要的,只需要在服务器端也创建相同的 POJO class,RequestBody 会将此 JSON 反序列化为适当的 class, 现在通过 class 的对象你可以得到任何你想要的变量。希望这有帮助。
编辑:我的代码终于可以运行了。所以如果你想删除body,你可以使用这段代码。但是,问题来自 json:我是法国人,所以我的文字和特殊字符上有些重音。清理我的字符串后,一切正常。
提前抱歉我用谷歌搜索的英语, 我使用 API 并制作了一个允许使用它的 JAVA 软件。 我需要做一个删除和软件。 我必须执行删除,并使用提供的软件来测试 API,我看到我必须在正文中添加该行才能将其删除,如下所示: ["email","Termine",“13/03/2018 09:52:20”,等...,“”]。 正文必须包含一个字符串数组,其中包含要删除的行的所有内容。 我可以让它在测试软件中运行。
但是我不明白如何使用 JAVA 进行删除。我可以让它在软件测试中工作。这就是我现在所做的:
public static String delete(String json, String nomUrl) throws IOException {
URL url = new URL(baseUrl + "survey/"+ nomUrl + "/data");
//String json = "[\"Marc@Houdijk.nl\",\"Contacte\",\"10/04/2018 11:30:05\",\"Avoriaz\",\"Office de Tourisme\",\"Accueil OT\",\"Neerlandais\",\"Semaine 6\",\"Periode 2\",\"16\",\"\",\"Hiver 2018\",\"BJBR-CDQB\",\"04/12/2018 14:15:13\",\"04/12/2018 14:15:13\",\"04/12/2018 14:15:13\",\"\",\"Direct\",\"\",\"\",\"\"]\n";
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("DELETE");
con.setRequestProperty("Content-Type","application/json");
con.setRequestProperty("Accept","application/json");
con.setRequestProperty("Authorization","Bearer "+token);
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(json);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
StringBuilder responce = new StringBuilder();
responce.append("\nSending 'DELETE' request to URL : ").append(url);
responce.append("\nResponse Code : ").append(responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
responce.append("\n").append(inputLine);
}
in.close();
return responce.toString();
}
我为 post 和 get 所做的事情启发了我。但是我看不到如何使用我的字符串数组将正文正确添加到我的删除函数中,因为它不起作用,而且互联网也没有帮助我...... 预先感谢您的帮助!
编辑:我的代码终于可以运行了。所以如果你想删除body,你可以使用这段代码。但是,问题来自 json:我是法国人,所以我的文字和特殊字符上有些重音。清理我的字符串后,一切正常。
你可以创建一个POJO class 包含RequestBody 需要的字段并将其发送到API,通过序列化对象(序列化意味着将Java 对象转换成JSON 这可以通过 GSON 库完成)。在 API 端,您可以轻松获得 ArrayList 或任何您想要的,只需要在服务器端也创建相同的 POJO class,RequestBody 会将此 JSON 反序列化为适当的 class, 现在通过 class 的对象你可以得到任何你想要的变量。希望这有帮助。
编辑:我的代码终于可以运行了。所以如果你想删除body,你可以使用这段代码。但是,问题来自 json:我是法国人,所以我的文字和特殊字符上有些重音。清理我的字符串后,一切正常。