我有一个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