排球图书馆的麻烦

时间:2018-10-12 04:50:30

标签: android android-volley

我尝试在应用程序中获得响应,当我尝试使用凌空库调用API时,它给出了com.android.volley.ServerError和响应代码400。 这是我的代码

RequestQueue requestQueue = Volley.newRequestQueue(this);
    try
    {
        //start bottom
        String url="http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/GetBookingDetails";
        url = url.replaceAll(" ", "%20");
        //String url="http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/Search/";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("EndUserIp","216.10.251.69");
        jsonObject.put("TokenId","0307b931-bd7d-4860-9c4d-4d65103ebddc");
        jsonObject.put("PNR","ZERD8U");
        jsonObject.put("BookingId","1401272");
        Log.i("JsonObject",jsonObject.toString());
        JsonObjectRequest jsonobjectreq = new JsonObjectRequest(Request.Method.POST, url,jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response)
                    {
                        Log.i("response",response.toString());
                        progressDialog.dismiss();
                    }
                }

它在邮递员中工作。我收到的错误

com.android.volley.ServerError

日志是

10-12 10:34:19.525 7149-7189/com.farehawker E/Volley: [387] BasicNetwork.performRequest: Unexpected response code 400 for http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/GetBookingDetails
10-12 10:34:19.529 7149-7149/com.farehawker I/Error: com.android.volley.ServerError
  • 我发送的json是

    {“ EndUserIp”:“ 216.10.251.69”,“ TokenId”:“ 0307b931-bd7d-4860-9c4d-4d65103ebddc”,“ PNR”:“ ZERD8U”,“ BookingId”:“ 1401272”}

Postman request Postman response

2 个答案:

答案 0 :(得分:1)

您要向服务器发送 JsonObjectRequest 的AFAIK,为此,您还需要在Volley网络通话中传递 Content-Type

在您的Volley NetworkCall中附加此标头

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json; charset=utf-8");
    return headers;
}

答案 1 :(得分:1)

用以下一项替换您的请求队列:

RequestQueue requestQueue = Volley.newRequestQueue(this);
        try {
            String url = "http://api.tektravels.com/BookingEngineService_Air/AirService.svc/rest/GetBookingDetails/";
            url = url.replaceAll(" ", "%20");

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("EndUserIp", "216.10.251.69");
            jsonObject.put("TokenId", "0307b931-bd7d-4860-9c4d-4d65103ebddc");
            jsonObject.put("PNR", "ZERD8U");
            jsonObject.put("BookingId", "1401272");
            Log.i("JsonObject", jsonObject.toString());
            JsonObjectRequest jsonobjectreq = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.i("response", response.toString());
                            //progressDialog.dismiss();
                        }
                    },
                    // The final parameter overrides the method onErrorResponse() and passes VolleyError
                    //as a parameter
                    new Response.ErrorListener() {
                        @Override
                        // Handles errors that occur due to Volley
                        public void onErrorResponse(VolleyError error) {
                            Log.e("Volley", "Error");
                        }
                    }


            ) ;
 jsonobjectreq.setRetryPolicy(new DefaultRetryPolicy(
                    5000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            jsonobjectreq.setShouldCache(false);
            requestQueue.add(jsonobjectreq);
            } catch (Exception e) {
            e.printStackTrace();
        }