【问题标题】:Trouble with Volley LibraryVolley 库的问题
【发布时间】:2018-10-12 04:50:29
【问题描述】:

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

【问题讨论】:

  • 400 表示您的服务器已关闭
  • 您需要发布API调用的代码sn-p和来自logcat的正确日志。
  • @Sushil Kumar 但它正在使用邮递员。
  • 显示你的代码和错误日志
  • @Jay 我发布了我的日志和 api 调用

标签: android android-volley


【解决方案1】:

将您的 requestqueuw 替换为以下一个:

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 的末尾附加了“/”。我不知道,为什么它之前对你不起作用。
  • 为什么要在url后面加上/?
  • 谢谢你的帮助。我可以有你的电子邮件或whatsapp 没有
【解决方案2】:

AFAIK 您将 JsonObjectRequest 发送到服务器,为此,您还需要通过 Volley 网络调用传递 Content-Type 。

在你的 Volley NetworkCall 中附加这个 header 。

@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,但我想确保他将正确的请求传递给服务器。在他也发布一些其他细节之前,我们无法解决问题
  • 问题是 volley 不支持重定向 url。并且该 url 在内部重定向到以“/”结尾的新 url。所以@user1 从github.com/samkirton/android-volley 下载 Volley 代码并将其添加为模块。我认为这段代码解决了重定向问题。
猜你喜欢
  • 1970-01-01
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多