Volley库出现问题

3

我在我的应用程序中尝试获取响应,在使用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();
                    }
                }

它在Postman中能正常工作。我遇到的错误是:
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请求 Postman响应


400 表示你的服务器已经宕机。 - sushildlh
@Sushil Kumar 但是在Postman上可以运行。 - user1
@Jay,我发布了我的日志和 API 调用。 - user1
@user1 在你的URL末尾添加“/”。 - Parul
@Parul,我在URL末尾添加了“/”,但它没有起作用。 - user1
显示剩余9条评论
2个回答

2
据我所知,您正在向服务器发送 JsonObjectRequest,为此,您需要在 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;
}

我认为 JsonObjectRequest 会自动添加 "application/json" 作为头部信息。 - Parul
是的@Parul,但我想确保他向服务器发送了正确的请求。在他发布其他细节之前,我们无法找出问题所在。 - Tejas Pandya
问题是Volley不支持重定向URL。而且该URL在内部重定向到以“/”结尾的新URL。所以@user1从https://github.com/samkirton/android-volley下载Volley代码并将其添加为模块。我认为这个代码解决了重定向问题。 - Parul

2

将您的请求队列替换为以下内容:

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();
        }

在这段代码中,我只是在 URL 的末尾添加了“/”。我不知道为什么之前它对你不起作用。 - Parul
为什么在URL末尾添加/是必要的? - user1
谢谢您的帮助。我可以得到您的电子邮件或WhatsApp号码吗? - user1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接