我认为JSONException仅在我的请求失败时有效,但是当请求有效时(有效的用户名,密码),它应该将我重定向到另一个活动,而是出现JSONException。
它显示从服务器收到的JSON字符串,而不是将我重定向到另一个活动。
这是我的 onResponse 功能
@Override
public void onResponse(String response){
try {
JSONObject volleyResponse = new JSONObject(response);
boolean success = volleyResponse.getBoolean("success");
String message = volleyResponse.getString("message");
String UUID = volleyResponse.getString("unique_user_id");
String LLOGIN = volleyResponse.getString("last_login");
String RDATE = volleyResponse.getString("registration_date");
String MONEY = volleyResponse.getString("money");
if(success){
Intent intent = new Intent(Authentication.this, Mainpage.class);
intent.putExtra(KEY_USERNAME, strUsername);
intent.putExtra(KEY_UUID, UUID);
intent.putExtra(KEY_LLOGIN, LLOGIN);
intent.putExtra(KEY_RDATE, RDATE);
intent.putExtra(KEY_MONEY, MONEY);
startActivity(intent);
}
} catch(JSONException e) {
response = response.replace("\"", "");
response = response.replace("status:false,message:", "");
response = response.replace("{", "");
response = response.replace("}", "");
messageText.setText(response);
}
}
JSON响应成功时:
{"unique_user_id":"4e99a28a-0cb2-30a9-ac51-ccd4629bcef1","last_name":"therealaxis","password":"$2a$10$9qRjW\/vJreCQg3u5dO6eW.8PhZBTpGaPNK5qRIYP.XTx2PVY1yrOi","last_login":"1 week ago","registration_date":"1 week ago","money":"100.00","success":true}