【发布时间】:2014-11-05 02:46:16
【问题描述】:
我有一个 android 应用程序,它可以咨询并插入到 web 服务中。 这一切都是通过 apache HTTPClient 和 JSON 实现的。
例如,我将一个新用户插入到数据库中。
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);
所有完美创建,现在我想使用我在其余 api 中创建的方法,方法 PUT 覆盖例如 ID 为 5 的用户的名称如果我想做一个 get 输入我的 url + / 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());
}
我应该做哪些改变?
【问题讨论】:
-
不,只是不要在 bd 中输入任何内容...
-
也不例外?奇怪,是不是正确的网址?
-
您可以访问 REST 服务日志吗?有两种选择,一是您没有发送数据,二是服务出现问题。
标签: android json rest androidhttpclient