我正在使用android volley对RESTful API执行一些网络请求。当我使用邮递员在本地测试API时,它返回预期值'true'。当我使用AsyncTask从返回的JSON对象中获取布尔结果时,它也返回相同的'true'值。但是,当我使用volley执行相同操作时,即使返回的值实际为真,返回的JSON响应也始终为“false”。
这是我的RESTful API代码。
$app->post('/login', function () use ($app) {
// reading post params
$email = $app->request()->post('email');
$password = $app->request()->post('password');
$response = array();
//$db = new DbHandler ();
// check for correct email and password
if (true) {
// get the user by email
//$user = $db->getUserByEmail($email);
$response ['error'] = true;
$response ['message'] = 'Login failed. Incorrect credentials';
} else {
// user credentials are wrong
$response ["error"] = false;
$response ['message'] = "Login Successful";
}
echoRespnse(200, $response);
});
这是我使用邮递员
运行时返回的值
这是我的截击操作
public class LoginActivity extends AppCompatActivity {
private Button login;
private EditText email, password;
private boolean error;
private String message;
@Override
public void onClick(View v) {
final RegisterInBackground registerGCMID = new RegisterInBackground();
//registerGCMID.execute();
HashMap<String, String> params = new HashMap<String, String>();
params.put("email", email.getText().toString());
params.put("password", password.getText().toString());
RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, ApplicationConstants.url_sign_in, new JSONObject
(params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
error = response.getBoolean(TAG_ERROR);
message = response.getString(TAG_MESSAGE);
Toast.makeText(LoginActivity.this, error + " value of error", Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Toast.makeText(LoginActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(request);
if (!error) { //when it reaches here the error changes from true to false
//Toast.makeText(LoginActivity.this, error + "", Toast.LENGTH_SHORT).show();
Intent i = new Intent(LoginActivity.this, SportSelection.class);
startActivity(i);
finish();
}
然而,当我使用AsyncTask
执行完全相同的操作时它会起作用 class RegisterInBackground extends AsyncTask<String, String, String> {
JSONObject json;
//boolean error;
String message, userEmail, accountType;
@Override
protected String doInBackground(String... args) {
Intent i;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", email.getText().toString()));
params.add(new BasicNameValuePair("password", password.getText().toString()));
json = jsonParser.makeHttpRequest(ApplicationConstants.url_sign_in, "POST", params);
// check log cat for response
//Log.d("Create Response", json.toString());
try {
error = json.getBoolean(TAG_ERROR);
message = json.getString(TAG_MESSAGE);
if (!error) {
i = new Intent(LoginActivity.this.getApplicationContext(),
SportSelection.class);
startActivity(i);
LoginActivity.this.finish();
//Log.d("Message is", message);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
在为错误分配值为true后,错误值被更改为false是什么?