0

I am parsing this

{
  "error": false,
  "uid": "1",
  "user": {
    "name": "admin",
    "mail": "ardypranataturnip@gmail.com"
  }
}

when I run my code, I've got the error

JSONException: Value uid of type java.lang.String cannot be converted to JSONObject

How do I solve this error ?

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();
                }
            });
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • you cannot convert object User to String, that is why you getting an exception – Gusti Arya Jul 31 '17 at 03:15
  • Your question has no `uid` key being parsed – OneCricketeer Jul 31 '17 at 04:20
  • jsonRESULTS.getString("error").equals("false")) . Is this correct?? – Rishav Chudal Jul 31 '17 at 04:26
  • I tried to parse the response that you have, used org.json.jar and didn't get any exception: public static void main(String[] args) { JSONObject obj; String name = ""; String test = "{ \"error\": false, \"uid\": \"1\",\"user\": { \"name\": \"admin\", \"mail\": \"ardypranataturnip@gmail.com\" } }"; try { obj = new JSONObject(test); name = obj.getJSONObject("user").getString("name"); System.out.println(name); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } – deepakl Jul 31 '17 at 04:43
  • now i got ths error > JSONException: Value Connection of type java.lang.String cannot be converted to JSONObject – ardy pranata turnip Jul 31 '17 at 04:44
  • yes, its work before, but when i try to login again, it doesn't work @RishavChudal – ardy pranata turnip Jul 31 '17 at 04:51
  • I was talking to remove the inverted comma from the false. – Rishav Chudal Jul 31 '17 at 04:53

0 Answers0