【问题标题】:How to read Volley Unexpected response code 500如何阅读 Volley Unexpected 响应代码 500
【发布时间】:2016-10-26 20:02:23
【问题描述】:

大家好,我不知道如何从服务器读取实际的详细错误消息,我得到的只是 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;
    }
}

【问题讨论】:

  • 你应该使用改造而不是凌空。
  • 哦?!我用了凌空,因为它似乎很受欢迎。我会研究改造,看看它的全部内容:),谢谢你的建议!

标签: java android server


【解决方案1】:

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

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

将 null 更改为 errorListener。

【讨论】:

  • 天哪,完美!!谢谢!我对构造函数一无所知,所以这是一个很好的收获。我正朝着正确的方向前进,但我仍然无法阅读完整的错误消息。这就是我得到的。 E/Volley:[83012] BasicNetwork.performRequest:xxxx.com/xxxxx E/Volley 的意外响应代码 500:错误。 HTTP状态码:500 E/Volley: ServerError D/Maps:: Error: null
  • 试试这个 Log.d("Maps:", "Error:" + new String(error.networkResponse.data));完成=真;
  • 大声笑是的!再次朝着正确的方向前进!我能够获得详细消息的一些输出,但由于某种原因,它没有显示全部。就像它因数据过多或其他原因而被切断。您知道为什么没有打印出整个消息吗?我知道它不是完整消息的唯一原因是我使用 Mozilla REST 客户端来帮助我解决错误,但最终目标是让我的应用程序处理所有日志记录并生成有用的调试信息。
  • 日志猫消息似乎有长度限制,即4K。如果您的消息比它大,请尝试打破它。参考:stackoverflow.com/questions/8888654/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
相关资源
最近更新 更多