Android Rest 身份验证不返回状态代码或网络响应
Posted
技术标签:
【中文标题】Android Rest 身份验证不返回状态代码或网络响应【英文标题】:Android Rest Authentication not returning the status code or network response 【发布时间】:2019-09-05 20:23:12 【问题描述】:我有一个使用 django REST API 后端的 android,其中包括基于令牌的身份验证。 问题是,每当用户提供错误的电子邮件或密码时,我预计下面的代码会出现 403 错误,但 volley 库没有得到响应代码。
这是 django rest 中的支持服务。
def api_authenticate_user(request):
try:
email = request.data['email']
password = request.data['password']
user = authenticate(email=email, password=password)
if user is not None:
try:
payload = jwt_payload_handler(user)
token = jwt.encode(payload, settings.SECRET_KEY)
user_logged_in.send(sender=user.__class__, request=request, user=user)
user = User.objects.get(email=email)
user_details = 'token': token, 'names': user.get_name(), 'id': user.id
return JsonResponse(user_details, status=status.HTTP_200_OK)
except Exception as e:
raise e
else:
return JsonResponse('error': 'Invalid email address or password', 'status': status.HTTP_403_FORBIDDEN)
except KeyError:
return JsonResponse('error': 'Provide email and password', 'status': status.HTTP_403_FORBIDDEN)
在我的 android 应用程序中,这是我尝试获取响应代码的方式,以便我可以提醒用户输入正确的凭据。
private void UserLoginTask(final String email, final String password)
StringRequest request = new StringRequest(Request.Method.POST, loginUrl , new Response.Listener<String>()
@Override
public void onResponse(String response)
try
JSONObject userObj = new JSONObject(response);
userSession.initializeSession(userObj.getString("names"), email, userObj.getString("token"));
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
catch (JSONException e)
e.printStackTrace();
errorText.setText("Could not process your request. Try again");
errorText.setVisibility(View.VISIBLE);
showProgress(false);
, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
System.out.println(error.networkResponse);
if(error.networkResponse.statusCode == 403)
errorText.setText("Invalid email address or password");
else
errorText.setText("Could not process your request. Try again");
errorText.setVisibility(View.VISIBLE);
showProgress(false);
)
@Override
protected Map<String, String> getParams() throws AuthFailureError
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("password", password);
return params;
;
RequestQueue queue = Volley.newRequestQueue(Login.this);
queue.add(request);
当我尝试记录或获取 volley 错误侦听器类中所示的错误状态时,我收到此错误,表明响应网络状态为空。如何获取 403 或任何其他状态码。
2019-04-15 22:24:46.461 31397-31397/com.example.phanue.antenatal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.phanue.antenatal, PID: 31397
java.lang.NullPointerException: Attempt to read from field 'int com.android.volley.NetworkResponse.statusCode' on a null object reference
at com.example.phanue.antenatal.LoginData.Login$4.onErrorResponse(Login.java:153)
at com.android.volley.Request.deliverError(Request.java:564)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6942)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
【问题讨论】:
【参考方案1】:检查空指针以避免NullPointerException
@Override
public void onErrorResponse(VolleyError error)
if (error != null && error.networkResponse != null && error.networkResponse.statusCode == 403)
errorText.setText("Invalid email address or password");
elseerrorText.setText("Could not process your request. Try again");
【讨论】:
在API中设置状态码为403后,什么情况下网络响应为空以上是关于Android Rest 身份验证不返回状态代码或网络响应的主要内容,如果未能解决你的问题,请参考以下文章