【发布时间】:2019-03-15 10:32:58
【问题描述】:
我使用 API 登录,如果登录成功,它会返回代码 200 和响应。如果不正确的电子邮件或密码将返回代码 401,json 对象包含错误 问题是我想得到网络响应,即使请求代码不等于 200 像图像
但是凌空抛出异常BasicNetwork.performRequest: Unexpected response code 401
这是我的代码
public interface OnResponse{
void onResponse(JSONObject response) throws Exception;
void onErrorResponse(VolleyError volleyError);
}//OnResponse
public static void newRequest(Context context, String url, final Map<String, String>params, final OnResponse onResponse){
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RequestQueue queue = Volley.newRequestQueue(context);
//
//onErrorResponse()
//onResponse()
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
// - - - - - - - - - - - - - - - - - - - - -
try {
JSONObject object = new JSONObject(response);
onResponse.onResponse(object);
} catch (Exception e) {
Log.e(TAG, "FLAG-1");
e.printStackTrace();
}
// - - - - - - - - - - - - - - - - - - - - -
}, error -> {
Log.e(TAG, "FLAG-2");
onResponse.onErrorResponse(error);
}){
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
volleyError.printStackTrace();
Log.e("zxc", volleyError.getMessage()+"");
Log.e("zxc", volleyError.getLocalizedMessage()+"");
return super.parseNetworkError(volleyError);
}
};
//
stringRequest.setParams(params);
stringRequest.setShouldCache(false);
queue.add(stringRequest);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
}//newRequest()`
【问题讨论】:
-
停止使用volley并使用其他http库
-
检查您的 zxc 日志,您可能会在此处得到回复