代码之家  ›  专栏  ›  技术社区  ›  Hcetipe

HTTP POST请求通过截击发送JsonObject

  •  1
  • Hcetipe  · 技术社区  · 6 年前

    我正试图通过Android上的Volley库发出httppost请求。

    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
              }
           ]
        }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Wasim Abuzaher    6 年前

    试着丢掉那个 final jsonBody JSO对象

    编辑:

    您将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-什么有效

        2
  •  0
  •   nupadhyaya    6 年前
    params.put("Authorization", "Token EnVo01jqN2XJIAd8vlLkw");
    

    拆下 Token 文本:

    params.put("Authorization", "EnVo01jqN2XJIAd8vlLkw");
    
    推荐文章