Мое Android приложение отправляет запрос в REST API для аутентификации пользователя. Сервер вернет истину с этим запросом. Но в моем приложении работает onErrorResponse()
без onResponse()
. И ошибка, которую я напечатал:
com.android.volley.ParseError: org.json.JSONException: Value [{"id":1,"username":"vuthehuyht","full_name":"Vũ Thế Huy","phone_number":"0972809817","password":"hoanglan"}] of type org.json.JSONArray cannot be converted to JSONObject
authenticateUser function
final RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("phone_number", phoneNumber);
jsonObject.put("password", password);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "http://192.168.53.100:3001/user/auth", jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
loadingBar.dismiss();
Toast.makeText(LoginActivity.this, "Login successfully", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loadingBar.dismiss();
Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_SHORT).show();
System.out.println(error.toString());
}
});
requestQueue.add(jsonObjectRequest);
user.controller. js
exports.auth = (req, res) => {
const phone_number = req.body.phone_number;
const password = req.body.password;
User.findAll({
where: {
phone_number: phone_number,
password: password
}
})
.then(data => {
res.send(data);
res.end();
})
.catch(err => {
res.status(500).send({
message: err.message | "Could not found user!"
});
res.end();
})
};
Как можно Я это исправлю?