JSONException:java.lang.String类型的值uid无法转换为JSONObject

时间:2017-07-31 03:09:33

标签: java android json jsonexception

我正在解析这个

{
  "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();
                }
            });
}

0 个答案:

没有答案