Uber APIライドリクエストエンドポイントを使用するアンドロイドアプリを構築しています.HTTPBodyでデータを追加する際に問題があり、エンドポイントがサポートされていないなどのエラーが表示されます。androidにputメソッドを使用してデータを追加する方法
これらは、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本体をどのように想定していますか?ジュソン? xml? URLエンコードされた? – Bhargav
json形式が必要です –
実際にHTTPbodyにステータスを追加する方法が得られていません –