如何使用Volley发送带有JSON正文的POST请求?

帕旺迪普

如何使用Volley库将这些参数传递给POST方法。

API链接:http : //api.wego.com/flights/api/k/2/searches?
api_key=12345&ts_code=123 JSON结构的屏幕截图

我尝试过此操作,但再次遇到错误。

StringEntity params= new StringEntity ("{\"trip\":\"[\"{\"departure_code\":\","
                     +departure,"arrival_code\":\"+"+arrival+","+"outbound_date\":\","
                     +outbound,"inbound_date\":\","+inbound+"}\"]\"}");
request.addHeader("content-type", "application/json");
request.addHeader("Accept","application/json");

请访问此处以了解API的详细信息。

斯里哈里

通常的方法是使用HashMap带有键值对的Volley作为请求参数

与以下示例类似,您需要针对特定​​需求进行自定义。

选项1:

final String URL = "URL";
// Post params to be sent to the server
HashMap<String, String> params = new HashMap<String, String>();
params.put("token", "token_value");
params.put("login_id", "login_id_value");
params.put("UN", "username");
params.put("PW", "password");

JsonObjectRequest request_json = new JsonObjectRequest(URL, new JSONObject(params),
       new Response.Listener<JSONObject>() {
           @Override
           public void onResponse(JSONObject response) {
               try {
                   //Process os success response
               } catch (JSONException e) {
                   e.printStackTrace();
               }
           }
       }, new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
               VolleyLog.e("Error: ", error.getMessage());
           }
       });

// add the request object to the queue to be executed
ApplicationController.getInstance().addToRequestQueue(request_json);

注意:HashMap可以将自定义对象作为值

选项2:

在请求正文中直接使用JSON

try {
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    String URL = "http://...";
    JSONObject jsonBody = new JSONObject();
    jsonBody.put("firstkey", "firstvalue");
    jsonBody.put("secondkey", "secondobject");
    final String mRequestBody = jsonBody.toString();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("LOG_VOLLEY", response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("LOG_VOLLEY", error.toString());
        }
    }) {
        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }

        @Override
        public byte[] getBody() throws AuthFailureError {
            try {
                return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
                VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
                return null;
            }
        }

        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            String responseString = "";
            if (response != null) {

                responseString = String.valueOf(response.statusCode);

            }
            return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
        }
    };

    requestQueue.add(stringRequest);
} catch (JSONException e) {
    e.printStackTrace();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

以字符串形式发送带有请求正文的POST请求

如何使用cURL发送带有请求参数的POST请求?

在axios中发送带有正文的POST请求

使用Volley发送带有JSON数据的POST请求

如何使用Alamofire 4在Swift 3中发送带有参数和正文的JSON数据的POST请求?

使用Volley使用JSONArray发送POST请求

如何使用带有XML正文的Alamofire发送请求

如何在带有参数和标头的Android Volley库中使用POST请求?

使用Guzzle发送带有JSON的POST请求

如何使用Retrofit发送带有JSON的POST?

Android Volley Post请求在正文中带有json对象,并在String中获取响应

如何使用PHP(cURL)中的post方法在带有标头的正文中发送JSON数据

使用JSON正文发送POST请求时出现400响应

Http Post发送带有空正文的请求

使用JavaScript(XMLHttpRequest)发送带有正文的GET请求

带有Json正文的HttpClient Post请求

如何在Qt中使用JSON正文发送POST请求

使用Guzzlehttp发送带有JSON正文的POST请求

Angular:发送带有空请求正文的POST请求

如何使用JSON正文在Java中发送发布请求

在R中发送带有正文的POST请求

如何使用cURL发送带有正文,标头和HTTP参数的POST?

Runscope –如何通过POST请求发送JSON正文?

如何将数据作为带有 JSON 正文的 POST 发送,并导航到响应?

带有标题和 JSON 正文的 Volley 中的 POST 请求

在flutter中发送带有json正文的GET请求

如何使用请求 2.18 发送带有 JSON 正文的 GET 请求?

无法发送带有正文的 POST

使用 Spring Boot 在带有 JSON 正文的 POST 请求中接收空参数