我有一個android應用程序,可以在Web服務中進行諮詢和插入。 所有這一切都通過Apache HTTPClient和JSON。在Android中發送「PUT」請求以休息api
所以例如我插入一個新的用戶到數據庫。
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("name", name);
jsonObject2.put("number", num);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
都完全建立,現在好了,我想用我的REST API創建的方法,方法PUT覆蓋例如ID爲5的用戶的名稱。如果我想要做一大家進入我的網址+/ID並獲取特定用戶。到「PUT」我這樣做,但不起作用。
@Override
protected String doInBackground(String... params) {
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPut httpPut = new
HttpPut("http://000.000.0.000:0000/xxxxxx/webresources/net.xxxxx.users/5");
String json = "";
// // 3. build jsonObject
// JSONObject jsonObject2 = new JSONObject();
// jsonObject2.put("idGuarderias", idG);
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.put("name",newName);
// jsonObject.put("guarderiasIdGuarderias",jsonObject2);
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPut.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPut.addHeader("Accept", "application/json");
httpPut.addHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPut);
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
我應該做什麼改變?
有什麼錯誤? – 2014-09-11 08:31:31
不,不要在bd裏輸入任何東西... – 2014-09-11 08:35:53
也沒有例外嗎?很奇怪,它是否是正確的URL? – 2014-09-11 08:39:18