我想从RESTful Webservice in Android
删除一个项目。我的Webservice看起来像这样:
@DELETE
@Path("/schueler/{id}")
@Produces(MediaType.APPLICATION_JSON)
public void deleteSchueler(@PathParam("id") int id) {
ManagementDAO.instance.deleteSchueler(id);
}
这里有一些Android代码。首先调用AsyncTask
:
new AsyncDeleteSchueler(getApplicationContext()).execute("http://" + url.getText().toString() + ":8080/A07_Webservice/rest/ManagementService/schueler/" + Integer.parseInt(deleteId.getText().toString()));
以及此任务的代码:
public String putJSONToWebservice(String u) {
int responseCode = 0;
try {
URL url = new URL(u);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("DELETE");
connection.setDoOutput(true);
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.connect();
responseCode = connection.getResponseCode();
} catch(Exception ex) {
ex.printStackTrace();
}
return Integer.toString(responseCode);
}
你知道为什么Delete method
不起作用吗? PUT,POST等工作正常,但这段代码没有。