我正在解析这个
{
"error": false,
"uid": "1",
"user": {
"name": "admin",
"mail": "ardypranataturnip@gmail.com"
}
}
当我运行我的代码时,我收到了错误
JSONException:java.lang.String类型的值uid无法转换为JSONObject
如何解决此错误?
private void requestLogin() {
mApiService.loginRequest(etName.getText().toString(), etPassword.getText().toString())
.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
loading.dismiss();
try {
JSONObject jsonRESULTS = new JSONObject(response.body().string());
if (jsonRESULTS.getString("error").equals("false")) {
//jika login berhasil maka name yang ada di response api akan di parsing ke aktivity selanjutnya
Toast.makeText(mContext, "LOGIN BERHASIL", Toast.LENGTH_SHORT).show();
String name = jsonRESULTS.getJSONObject("user").getString("name");
Intent intent = new Intent(mContext, MainActivity.class);
intent.putExtra("result_name", name);
startActivity(intent);
} else {
String error_message = jsonRESULTS.getString("error_msg");
Toast.makeText(mContext, error_message, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
loading.dismiss();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("debug", "onFailure: ERROR > " + t.toString());
loading.dismiss();
}
});
}