Java 成功的例外
我认为JSONException只有在我的请求失败时才起作用,但当请求有效(有效用户名、密码)时,它应该将我重定向到另一个活动,但会出现JSONException 它显示从服务器接收的JSON字符串,而不是将我重定向到另一个活动 这是我的onResponse功能Java 成功的例外,java,android,json,android-volley,jsonexception,Java,Android,Json,Android Volley,Jsonexception,我认为JSONException只有在我的请求失败时才起作用,但当请求有效(有效用户名、密码)时,它应该将我重定向到另一个活动,但会出现JSONException 它显示从服务器接收的JSON字符串,而不是将我重定向到另一个活动 这是我的onResponse功能 @Override public void onResponse(String response){ try { JSONObject volley
@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}
JSON响应没有消息字符串,因此会抛出JSONException。如果您只是想在消息属性存在的情况下访问它,请在访问它之前使用。您的响应没有消息字符串,因此会发送JSONException。失败时会发送一个消息字符串,但成功时只会显示您的统计信息和一个真正的[boolean]成功键。是的,但您总是尝试访问消息属性。即使成功了。在这一点上,它会直接跳进你的抓捕区。我现在就要测试它,谢谢:)[将其作为答案发布到tho]