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);
}
}
Response.Listner<String>
更改为JSONArray
,如下面的代码
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);
}
因为您正在尝试将字符串数据解析为 JSONArray。
这里
JSONArray jsonArray=new JSONArray(response);
将其更改为
JSONObject jsonObject =new JSONObject(response);
我猜变量中的值response
包含数组以外的其他值。
尝试使用 JSON 对象并打印/调试值。
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
loading.dismiss();
JSONObject jsonObj = new JSONObject(response);
}
}
}