Volley - BasicNetwork.performRequest:意外响应代码 400 POST

Posted

技术标签:

【中文标题】Volley - BasicNetwork.performRequest:意外响应代码 400 POST【英文标题】:Volley - BasicNetwork.performRequest: Unexpected response code 400 POST 【发布时间】:2019-08-06 23:09:19 【问题描述】:

邮递员标题

["key":"Content-Type","name":"Content-Type","type":"text","value":"application/json","key":"token","type":"text","value":"ffBJpLW55i"]

即使我把令牌值而不是令牌字符串它也不会工作

                    headers.put("Content-Type", "application/json");
                    headers.put("token", "ffBJpLW55i");

2019-03-15 21:51:15.384 20577-20620/com.sleepyhitman.ab4_internship_2019try2 E/Volley:[816] BasicNetwork.performRequest:https://tralalala.com/api-spot-get-all 的意外响应代码 400

    private void loadRecyclerViewData(final String token)
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            StringRequest listRequest = new StringRequest(Request.Method.POST,URL_DATA+"api-spot-get-all",
                    new Response.Listener<String>() 
                @Override
                public void onResponse(String response) 
                    try 
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray array = jsonObject.getJSONArray("result");
                        for (int i =0; i<array.length(); i++) 
                            JSONObject o = array.getJSONObject(i);
                            ListItem list = new ListItem(
                                    o.getString("id"),
                                    o.getString("name"),
                                    o.getString("country"),
                                    o.getString("whenToGo"),
                                    o.getBoolean("isFavorite")
                            );
                            listItems.add(list);
                        
                        ListItemAdapter listItemAdapter = new ListItemAdapter(getApplicationContext(), listItems);
                        recyclerView.setAdapter(listItemAdapter);

                     catch (JSONException e) 
                        e.printStackTrace();
                    
 //
                
            ,
            new Response.ErrorListener() 
                @Override
                public void onErrorResponse(VolleyError error) 

                
            )

                @Override

                public Map<String, String> getHeaders() throws AuthFailureError 
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("Content-Type", "application/json");
                    headers.put("token", token);
                    return headers;
                

                protected Map<String, String> getParams() 
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("country", "");
                    params.put("windProbability", "");
                    return params;
                
;



    requestQueue.add(listRequest);



【问题讨论】:

【参考方案1】:

这部分是你的凌空请求的错误部分,

URL_DATA+"api-spot-get-all"  

在使用 StringRequest 时应该有一个字符串格式的 URL。

【讨论】:

这不是问题,显然额外的标题 Content-Type 是问题..

以上是关于Volley - BasicNetwork.performRequest:意外响应代码 400 POST的主要内容,如果未能解决你的问题,请参考以下文章

深入volley自己来写volley

反应原生找不到 com.android.volley:volley:1.1.1

Volley框架源码分析

Volley-->

如何为跨类 Volley 方法调用创建适当的 Volley 侦听器

[转] Android Volley完全解析,初识Volley的基本用法