0

我面臨一個問題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(); 

回答

0

使用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

相關問題