如何阅读Volley Unexpected响应代码500

时间:2016-10-26 20:02:24

标签: java android server

大家好我不知道如何从服务器读取实际的详细错误消息,我得到的是E / Volley:[73767] BasicNetwork.performRequest:https://xxxx.com/xxxxx的意外响应代码500我看到了人们添加一个onErrorResponse监听器,但我的工作没有工作,所以我明显遗漏了一些东西,下面是我的代码,任何帮助表示赞赏。

请求:

JSONObject jsonFavorites = new JSONObject();
                        String userId = Integer.toString(2);
                        String waypointID = Integer.toString(eventInfo.waypointId);
                        String waypointType = Integer.toString(eventInfo.stopType);
                        try {
                            jsonFavorites.put("action", favoriteAction);
                            jsonFavorites.put("uid", userId);
                            jsonFavorites.put("waypointid", waypointID);
                            jsonFavorites.put("waypoint_type", waypointType);
                            //fetchData(bounds);
                        } catch (Exception e) {

                        }
                        try{
                            GetUserFavoritesRequest favoritesRequest = new GetUserFavoritesRequest(jsonFavorites, new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    //Parse the response that was received from the server.
                                    Log.d("Maps:", " Parsing Response");
                                    try {
                                        Log.i("tagconvertstr", "[" + response + "]");
                                        //List<String> allFavorites = new ArrayList<String>();

                                        JSONArray cast = new JSONArray(response);

                                        if(userFavoritewaypointId.contains(eventInfo.waypointId)){
                                            favoriteAction = "remove";
                                            infoFavoriteButton.setImageResource(R.drawable.favorites_disabled);
                                        }else{
                                            favoriteAction = "add";
                                            infoFavoriteButton.setImageResource(R.drawable.favorites);
                                        }
                                        finished = true;
                                    } catch (JSONException e) {
                                        //adding or removing favorites was unsuccessful.
                                        Log.d("Maps:", " Failed getting a response from server for adding or removing favorites");
                                        e.printStackTrace();

                                        //Set the finished flag to true to let everyone know that we
                                        //finished receiving a response from the server.
                                        finished = true;
                                    }
                                }
                            }, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    //Parse the response that was received from the server.
                                    NetworkResponse networkResponse = error.networkResponse;
                                    if (networkResponse != null) {
                                        Log.e("Volley", "Error. HTTP Status Code:"+networkResponse.statusCode);
                                    }

                                    if (error instanceof TimeoutError) {
                                        Log.e("Volley", "TimeoutError");
                                    }else if(error instanceof NoConnectionError){
                                        Log.e("Volley", "NoConnectionError");
                                    } else if (error instanceof AuthFailureError) {
                                        Log.e("Volley", "AuthFailureError");
                                    } else if (error instanceof ServerError) {
                                        Log.e("Volley", "ServerError");
                                    } else if (error instanceof NetworkError) {
                                        Log.e("Volley", "NetworkError");
                                    } else if (error instanceof ParseError) {
                                        Log.e("Volley", "ParseError");
                                    }
                                    Log.d("Maps:", " Error: " + error.getMessage());
                                    finished = true;
                                }
                            });
                            RequestQueue queue = Volley.newRequestQueue(getActivity());
                            queue.add(favoritesRequest);

                        } catch (Exception e) {
                            //We failed to start a login request.
                            Log.d("Maps:", " Failed to start response for adding or removing favorites");

                            //Set the finished flag to true to let everyone know that we
                            //finished receiving a response from the server.
                            finished = true;
                        }

GetUserFavoritesRequest.java

public class GetUserFavoritesRequest extends StringRequest {
    private static final String LOGIN_REQUEST_URL = "https://xxxx.com/xxxxx";
    private Map<String, String> params;

    public GetUserFavoritesRequest(JSONObject getFavorites, Response.Listener<String> listener, Response.ErrorListener errorListener){
        super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);
        params = new HashMap<>();

        try {
            if(getFavorites.get("action").toString().equals("get")){
                Log.d("Maps: ", "Looks like we are retrieving a list of favorite waypoints");
                params.put("action", getFavorites.get("action").toString());
                params.put("uid", getFavorites.get("uid").toString());
            }else if(getFavorites.get("action").toString().equals("add")){
                Log.d("Maps: ", "Looks like we are adding a favorite waypoint" + getFavorites);
                params.put("action", getFavorites.get("action").toString());
                params.put("uid", getFavorites.get("uid").toString());
                params.put("waypointid", getFavorites.get("waypointid").toString());
                params.put("waypoint_type", getFavorites.get("waypoint_type").toString());
            }else if(getFavorites.get("action").toString().equals("remove")) {
                Log.d("Maps: ", "Looks like we are removing a favorite waypoint" + getFavorites);
                params.put("action", getFavorites.get("action").toString());
                params.put("uid", getFavorites.get("uid").toString());
                params.put("waypointid", getFavorites.get("waypointid").toString());
                params.put("waypoint_type", getFavorites.get("waypoint_type").toString());
            }
        }catch (Exception e){

        }
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }
}

1 个答案:

答案 0 :(得分:1)

我认为GetUserFavoritesRequest的构造函数可能存在错误

super(Request.Method.POST, LOGIN_REQUEST_URL, listener, null);

将null更改为errorListener。