如何在android中使用put方法向HTTPBody添加数据
我正在构建一个使用Uber API ride request端点的android应用程序。我在HTTPBody中添加数据时遇到问题,它显示出错误,例如不支持端点 这些是curl命令:如何在android中使用put方法向HTTPBody添加数据,android,json,web-services,httprequest,http-put,Android,Json,Web Services,Httprequest,Http Put,我正在构建一个使用Uber API ride request端点的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 '{"sta
curl -X PUT 'https://sandbox-api.uber.com/v1/sandbox/requests/{REQUEST_ID}'
\ -H 'Content-Type: application/json'
\ -H 'Authorization: Bearer '
\ -d '{"status":"accepted"}'
代码:
首先,您希望PUT body的类型为
application/json
,但您正在将httpPut
对象的实体设置为UrlEncodedFormEntity
所以你需要先解决这个问题。
首先,您需要创建StringEntity
对象,并将其contentType
属性设置为application/json
在您的例子中,由于json字符串将是{“status”:“accepted”}
您需要像这样实例化StringEntity
类
StringEntity input = new StringEntity("{\"status\":\"accepted\"}");
然后将内容类型设置为
input.setContentType("application/json");
然后将httpput
entity属性设置为我们刚才创建的输入enity,如下所示:
httpput.setEntity(input);
就这样,就这样吧
httpput.setEntity(new UrlEncodedFormEntity(params));
前两行
因此,您的代码将如下所示
代码:
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);
httpput.setHeader("Authorization","Bearer "+token);
httpput.setHeader("Content-type", "application/json");
// Create the string entity
StringEntity input = new StringEntity("{\"status\":\"accepted\"}");
// set the content type to json
input.setContentType("application/json");
// set the entity property of the httpput
// request to the created input.
httpput.setEntity(input);
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;
}
如果您想将此带到下一步,那么您需要在java概念中逐步实现Json序列化和反序列化,并学习如何从java对象生成Json字符串,然后,您可以将Java对象序列化为json字符串,并使用生成的json字符串实例化
StringEntity
。服务器如何期望PUT主体?json?xml?url编码?它需要json格式实际上我不知道如何在HttpBody中附加状态您使用的是哪个http库?org.apache.httpcomponents:httpclient:4.5这些依赖项我正在使用,然后我还收到类似{“消息”:“此端点不支持方法”。,“代码”:“不允许方法”}我已经按照u建议对上述代码进行了更改,StringEntity输入=新的StringEntity(“{\”状态\“:\”接受\“}”);setContentType(“应用程序/json”);httpput.setEntity(输入)@RohanChavan那么这意味着HttpPut是不允许尝试HttpPost的
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);
httpput.setHeader("Authorization","Bearer "+token);
httpput.setHeader("Content-type", "application/json");
// Create the string entity
StringEntity input = new StringEntity("{\"status\":\"accepted\"}");
// set the content type to json
input.setContentType("application/json");
// set the entity property of the httpput
// request to the created input.
httpput.setEntity(input);
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;
}