首页 > 解决方案 > Volley 库的问题

问题描述

我尝试在我的应用程序中获得响应,当我尝试使用 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

邮递员请求 邮递员响应

标签: androidandroid-volley

解决方案


AFAIK 您正在向服务器发送JsonObjectRequest ,为此,您还需要通过您的 Volley network call传递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;
}

推荐阅读