2015-11-25 50 views
2

我嘗試刪除參數與此:HttpDelete與android的setEntity參數?

private class SendfeedbackDeleteStudio extends AsyncTask<String, Void, String> { 
    private static final String LOG_TAG = "DeleteStudio"; 
    Bundle extras = getIntent().getExtras(); 
    final String token= extras.getString("TOKEN"); 
    @Override 
    protected String doInBackground(String... params) { 
     String venid = params[0]; 
     Utils.log("venid: " + venid); 
     final String url_delete_studio = Constant.URI_BASE_FAVOURITE; 
     String contentType; 
     contentType = "application/x-www-form-urlencoded"; 
     // do above Server call here 
     List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1); 
     nameValuePair.add(new BasicNameValuePair("vendor_id", venid)); 
     try 
     { 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpDelete httpDelete = new HttpDelete(url_delete_studio); 
      httpDelete.setHeader("Content-Type", contentType); 
      httpDelete.setHeader("Authorization", "Bearer " + token); 
      httpDelete.setHeader("Accept", "application/json"); 
      httpDelete.setHeader("Accept-Charset", "utf-8"); 
      httpDelete.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
      HttpResponse response = httpClient.execute(httpDelete); 
      HttpEntity entity = response.getEntity(); 
      if (entity != null) { 
       // EntityUtils to get the reponse content 
       String content = EntityUtils.toString(entity); 
       Utils.log("daftar content: " + content); 
       JSONObject hasiljson = new JSONObject(content); 
       Utils.log("hasiljson object: " + hasiljson); 
       String success = hasiljson.getString("success"); 
       Utils.log("success: " + success); 
      } 
      // writing response to log 
      Log.d("Http Response:", response.toString()); 
     } 
     catch (Exception e) 
     { 
      Log.e(LOG_TAG, String.format("Error during delete: %s", e.getMessage())); 
     } 
     return "processing"; 
    } 

    @Override 
    protected void onPostExecute(String message) { 
     //process message 
     clickFavourites(); 
    } 
} 

,但它得到紅色httpDelete.setEntity(新UrlEncodedFormEntity(的NameValuePair));,它似乎不能認識到我發送刪除的參數。如何刪除venid參數?

+0

刪除參數從那裏被稱爲? – Amy

+0

從數據庫(mysql) –

回答

1

HTTP DELETE的行爲類似於GET變體,因此它不會接受任何輸入。

如果您正在尋找提供身體刪除,您可能需要考慮使用POST到接受身體的位置。

,或者您可以使用此

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; 
import java.net.URI; 
import org.apache.http.annotation.NotThreadSafe; 

@NotThreadSafe 
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase { 
    public static final String METHOD_NAME = "DELETE"; 
    public String getMethod() { return METHOD_NAME; } 

    public HttpDeleteWithBody(final String uri) { 
     super(); 
     setURI(URI.create(uri)); 
    } 
    public HttpDeleteWithBody(final URI uri) { 
     super(); 
     setURI(uri); 
    } 
    public HttpDeleteWithBody() { super(); } 
} 

這是從here

+0

如何使用我的網址和參數? –

+0

或者更好的使用GET,並且在api的SQL中使用DELETE命令? –

+0

@carijawaban你有權訪問api的代碼嗎?如果是的話,我建議你改變代碼來接收POST而不是DELETE,然後把實體放到HTTP POST中,然後在你的API代碼中使用SQL DELETE –