我正在尝试通过Android上的Volley库发出HTTP POST请求。
事实上,我想检索 API 返回给我的 JSON 对象。
API 文档为我提供了这个有效的 curl 请求:
curl -H "Authorization: Token $API_KEY"
--header "Content-Type: application/json"
--compressed
--data @-
https://api.idbus.com/v1/search <<JSON
{
"origin_id": 1,
"destination_id": 17,
"date": "2015-08-15",
"passengers": [
{ "id": 1, "age": 30 },
{ "id": 2, "age": 30 },
{ "id": 3, "age": 1 }
]
}
JSON
但我想用 Volley 将其转换为 HTTP 请求。
问题是当我启动代码时,我在 curl 请求工作时收到 404 错误。
我认为问题来自我将 JSON 对象传递到请求中的方式。 但是我没有发现错误我希望有人可以帮助我,提前谢谢你!
这是我的代码:
String url = "https://api.idbus.com/v1/search";
final JSONObject jsonBody = new JSONObject();
JSONArray passengers = new JSONArray();
try {
passengers.put(getPerson(1, 30));
passengers.put(getPerson(2, 30));
passengers.put(getPerson(3, 1));
} catch (JSONException e){
e.printStackTrace();
}
try {
jsonBody.put("origin_id", 1);
jsonBody.put("destination_id", 17);
jsonBody.put("date", "2015-08-15");
jsonBody.put("passengers", passengers);
} catch (JSONException e){
e.printStackTrace();
}
JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, url, jsonBody,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
mTextView.setText(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Authorization", "Token EnVo01jqN2XJIAd8vlLkw");
params.put("Content-Type","application/json");
return params;
}
};
queue.add(stringRequest);
}
JSONObject getPerson(int id, int age) throws JSONException {
JSONObject person = new JSONObject();
person.put("id", id);
person.put("age", age);
return person;
}
我的 Json 对象看起来像:
{
"origin_id":1,
"destination_id":17,
"date":"2015-08-15",
"passengers":[
{
"id":1,
"age":30
},
{
"id":2,
"age":30
},
{
"id":3,
"age":1
}
]
}
尝试从 JSONjsonBody
中丢失final
编辑:
您以字符串形式发送 int 值,因此另一端的服务器可能对此不太满意: 您发送的内容:
{"origin_id":"1",
"destination_id":"17",
"date":"2015-08-15",
"passengers":[{"id":"1","age":"30"},{"id":"2","age":"30"},{"id":"3","age":"1"}]}
您应该发送:
{"origin_id":1,
"destination_id":17,
"date":"2015-08-15",
"passengers":[{"id":1,"age":30},{"id":2,"age":30},{"id":3,"age":1}]}
用:
try {
passengers.put(getPerson(1, 30));
passengers.put(getPerson(2, 30));
passengers.put(getPerson(3, 1));
} catch (JSONException e){
e.printStackTrace();
}
try {
jsonBody.put("origin_id", 1);
jsonBody.put("destination_id", 17);
jsonBody.put("date", "2015-08-15");
jsonBody.put("passengers", passengers);
} catch (JSONException e){
e.printStackTrace();
}
getPerson
现在看起来应该使用int
而不是String
来id
和age
:
JSONObject getPerson(int id, int age) throws JSONException
编辑 2 - 有效的方法
嗯,尝试使用 POSTMAN (getpostman.com( 并从那里提交相同的 POST 以查看抛出的错误,这可能是服务器端问题
params.put("Authorization", "Token EnVo01jqN2XJIAd8vlLkw");
删除Token
文本:
params.put("Authorization", "EnVo01jqN2XJIAd8vlLkw");