有问题的行是con.setDoOutput(true); 。 删除将修复错误。
您可以使用addRequestProperty或setRequestProperty将请求标头添加到DELETE,但不能添加请求主体。
getOutputStream()仅适用于具有正文的请求,如POST。 在没有正文的请求上使用它,比如DELETE,会抛出一个ProtocolExceptionexception。 相反,你应该用addHeader()来添加你的头文件,而不是调用getOutputStream()。
我知道有点晚了,但是如果有人像我一样在这里search谷歌,我就这样解决了:
conn.setRequestProperty("X-HTTP-Method-Override", "DELETE"); conn.setRequestMethod("POST");
在旧的Android版本(<= 4.4)上,这是HttpURLConnection的限制。
虽然你可以select使用HttpClient ,但我不推荐它,因为它是一个旧版库,有几个问题已经从Android 6中删除 。
我会build议使用一个新的最近库像OkHttp :
OkHttpClient client = new OkHttpClient(); Request.Builder builder = new Request.Builder() .url(getYourURL()) .delete(RequestBody.create( MediaType.parse("application/json; charset=utf-8"), getYourJSONBody())); Request request = builder.build(); try { Response response = client.newCall(request).execute(); String string = response.body().string(); // TODO use your response } catch (IOException e) { e.printStackTrace(); }
尝试下面的方法调用HttpDelete方法,它适用于我,希望为您工作以及
String callHttpDelete(String url){ try { HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 15000); HttpConnectionParams.setSoTimeout(httpParams, 15000); //HttpClient httpClient = getNewHttpClient(); HttpClient httpClient = new DefaultHttpClient();// httpParams); HttpResponse response = null; HttpDelete httpDelete = new HttpDelete(url); response = httpClient.execute(httpDelete); String sResponse; StringBuilder s = new StringBuilder(); while ((sResponse = reader.readLine()) != null) { s = s.append(sResponse); } Log.v(tag, "Yo! Response recvd ["+s.toString()+"]"); return s.toString(); } catch (Exception e){ e.printStackTrace(); } return s.toString(); }
你不能只使用addHeader()方法?
这是我的Delete请求方法。
只是它是额外的RequestProperty post请求
connection.setRequestProperty("X-HTTP-Method-Override", "DELETE");
下面是完整的方法。
public void executeDeleteRequest(String stringUrl, JSONObject jsonObject, String reqContentType, String resContentType, int timeout) throws Exception { URL url = new URL(stringUrl); HttpURLConnection connection = null; String urlParameters = jsonObject.toString(); try { connection = (HttpURLConnection) url.openConnection(); //Setting the request properties and header connection.setRequestProperty("X-HTTP-Method-Override", "DELETE"); connection.setRequestMethod("POST"); connection.setRequestProperty("User-Agent", USER_AGENT); connection.setRequestProperty(CONTENT_TYPE_KEY, reqContentType); connection.setRequestProperty(ACCEPT_KEY, resContentType); connection.setReadTimeout(timeout); connection.setConnectTimeout(defaultTimeOut); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); // Send request DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); responseCode = connection.getResponseCode(); // To handle web services which server responds with response code // only try { response = convertStreamToString(connection.getInputStream()); } catch (Exception e) { Log.e(Log.TAG_REST_CLIENT, "Cannot convert the input stream to string for the url= " + stringUrl + ", Code response=" + responseCode + "for the JsonObject: " + jsonObject.toString(), context); } } catch ( Exception e ) { if (!BController.isInternetAvailable(context)) { IntentSender.getInstance().sendIntent(context, Constants.BC_NO_INTERNET_CONNECTION); Log.e(Log.TAG_REST_CLIENT, "No internet connection", context); } Log.e(Log.TAG_REST_CLIENT, "Cannot perform the POST request successfully for the following URL: " + stringUrl + ", Code response=" + responseCode + "for the JsonObject: " + jsonObject.toString(), context); throw e; } finally{ if (connection != null) { connection.disconnect(); } } }
我希望它有帮助。
DELETE请求是GET请求的一种扩展forms,按照您不能在DELETE请求的主体中写入的android文档。 HttpUrlConnection会抛出“ 无法写协议exception ”。
如果你仍然想在正文中写入参数,我build议你使用OKHttp库。
OKHttp文档
如果你有兴趣使用更简单的库,那么你可以尝试SimpleHttpAndroid库
有一件事要记住,如果你没有在身体上写任何东西,然后删除线
conn.setDoOutput(true);
谢谢,希望它可以帮助。
为了给这个问题添加闭包,它发现没有支持的方法来发送包含头部数据的HTTP DELETE请求。
解决scheme是让客户端改变他们的API来接受一个标准的GET请求,这个请求表明这个动作应该是一个删除操作,包含了要删除的项目的ID。
http://clienturl.net/api/delete/id12345