【发布时间】:2016-12-06 09:00:37
【问题描述】:
我正在构建一个使用 Uber API 乘车请求端点的 android 应用程序。我在 HTTPBody 中附加数据时遇到了问题,它显示了不支持端点等错误。
这些是 curl 命令:
curl -X PUT 'https://sandbox-api.uber.com/v1/sandbox/requests/{REQUEST_ID}'
\ -H 'Content-Type: application/json'
\ -H 'Authorization: Bearer '
\ -d '{"status":"accepted"}'
代码:
public JSONObject getStatus(String address, String requestId, String product_id, float start_latitude, float start_longitude, float end_latitude, float end_longitude, String token) {
try {
httpClient = new DefaultHttpClient();
httpput = new HttpPut("https://sandbox-api.uber.com/v1/requests/"+requestId);
**params.add(new BasicNameValuePair("status", "accepted"));**
httpput.setHeader("Authorization","Bearer "+token);
httpput.setHeader("Content-type", "application/json");
httpput.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpput);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSONStr", json);
} catch (Exception e) {
e.getMessage();
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
【问题讨论】:
-
服务器如何期待 PUT 正文? json? xml? url 编码?
-
需要json格式
-
实际上我没有得到如何在 HTTPbody 中附加状态
-
你用的是哪个http库?
-
org.apache.httpcomponents:httpclient:4.5 我正在使用的这些依赖项
标签: android json web-services httprequest http-put