final ProgressDialog loading = ProgressDialog.show(Login.this, "Verifying", "Please wait...", false, false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.LOGIN_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
loading.dismiss();
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String code = jsonObject.getString("code");
if (code.equals("login_failed")) {
builder.setTitle("Login Error");
displayAlert(jsonObject.getString("message"));
} else {
Intent intent = new Intent(Login.this, Success.class);
Bundle bundle = new Bundle();
bundle.putString("name", jsonObject.getString("name"));
bundle.putString("email", jsonObject.getString("email"));
intent.putExtras(bundle);
startActivity(intent);
}
}
3 respuestas
Cambie Response.Listner<String>
a JSONArray
como el siguiente código
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
loading.dismiss();
JSONArray jsonArray=new JSONArray(response);
JSONObject jsonObject=jsonArray.getJSONObject(0);
String code=jsonObject.getString("code");
if(code.equals("login_failed"))
{
builder.setTitle("Login Error");
displayAlert(jsonObject.getString("message"));
}
else {
Intent intent=new Intent(Login.this,Success.class);
Bundle bundle=new Bundle();
bundle.putString("name",jsonObject.getString("name"));
bundle.putString("email",jsonObject.getString("email"));
intent.putExtras(bundle);
startActivity(intent);
}
Supongo que el valor en la variable response
contiene algún otro valor que no sea la matriz.
Intente usar el objeto json e imprima / depure el valor.
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
loading.dismiss();
JSONObject jsonObj = new JSONObject(response);
}
}
}
Porque está intentando analizar datos de cadena en JSONArray .
Aquí
JSONArray jsonArray=new JSONArray(response);
Cambia esto a
JSONObject jsonObject =new JSONObject(response);
Nuevas preguntas
android
Android es el sistema operativo móvil de Google, que se utiliza para programar o desarrollar dispositivos digitales (teléfonos inteligentes, tabletas, automóviles, televisores, ropa, vidrio, IoT). Para temas relacionados con Android, use etiquetas específicas de Android, tales como intención de Android, actividad de Android, adaptador de Android, etc. Para preguntas que no sean de desarrollo o programación, pero relacionadas con el marco de Android, use este enlace: https: // android.stackexchange.com.