如何发送带有参数和内容类型的HttpDelete请求android

问题描述:

我面临一个问题HttpDelete在android中请求发送rest API。 当我点击API时,它显示错误。需要内容类型,我将其设置为标题,但没有找到任何需要帮助的解决方案。 这是我的代码,我曾尝试:如何发送带有参数和内容类型的HttpDelete请求android

DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); 
       String paramString = URLEncodedUtils.format(nameValuePairs, "utf_8"); 
       url += "?" + paramString; 
       HttpDelete httpDelete = new HttpDelete(url); 
       httpDelete.setHeader("Iauth" ,"1"); 
       httpDelete.setHeader("Msessid" ,sessionid); 
       httpDelete.addHeader("content-type","x-www-form-urlencoded"); 
       Log.d("URLLLLL",url); 
       // httpPost.setHeader("content-type","application/x-www-form-urlencoded;charset=UTF-8"); 
       HttpResponse httpResponse = defaultHttpClient.execute(httpDelete); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       inputStream = httpEntity.getContent(); 

使用HttpURLConnection的,因为DefaultHttpClient从API级别22

URL url = new URL("http://www.example.com/resource"); 
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); 
    httpCon.setDoOutput(true); 
    httpCon.addRequestProperty(
     "Content-Type", "application/x-www-form-urlencoded"); 
    httpCon.addRequestProperty("Iauth" ,"1"); 
    httpCon.addRequestProperty("Msessid" ,sessionid); 
    httpCon.setRequestMethod("DELETE"); 
    httpCon.connect(); 
+0

好先生过时但后来我怎样才能得到性反应?你能帮我吗 –

+1

使用这个turorial https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/ – USKMobility

+0

你也可以使用官方文档https://开发人员。 android.com/reference/java/net/HttpURLConnection.html – USKMobility