gpt4 book ai didi

java - 如何读取 Volley 意外响应代码 500

转载 作者:行者123 更新时间:2023-12-01 09:19:22 25 4
gpt4 key购买 nike

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

最佳答案

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

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

将 null 更改为 errorListener。

关于java - 如何读取 Volley 意外响应代码 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40271186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com