目前我正在使用 HttpClient 从 android 连接到 .net WEB API,并且我已经能够执行 GET 和 POST 来读取/写入数据。但是我想做一个更新和删除。
我尝试使用 POST 来执行此操作,但它很容易创建更多记录。这是我的 POST 代码,如何将其更改为 PUT 或 DELETE?
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mywebsite.net/api/employees/6");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("firstName", "UpdatedHello"));
nameValuePairs.add(new BasicNameValuePair("lastName", "World"));
nameValuePairs.add(new BasicNameValuePair("employee_name", "UpdatedHello World"));
nameValuePairs.add(new BasicNameValuePair("password", "xxx"));
nameValuePairs.add(new BasicNameValuePair("isActive", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);