我正在構建一個使用Uber API平臺請求端點的android應用程序。我在HTTPBody中追加數據時遇到了問題,並且它顯示不支持端點等錯誤。如何使用put方法將數據添加到HTTPBody中android
這些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編碼? – Bhargav
它需要json格式 –
其實我沒有得到如何在HTTPbody中追加狀態 –