0

I can not use the data from the method. I do not know why. Where did the mistake? I have the following json:

{"code":"1","status":"Basarili","login":"OK","kullanici":"1"}

JSONObject veri_json;
try {
    veri_json = new JSONObject(response); 
    try {
        String code,status,login,kullanici;
        code = veri_json.getString("code").toString();
        status = veri_json.getString("status").toString();
        login = veri_json.getString("login").toString();
        kullanici = veri_json.getString("kullanici").toString();

        if(code=="1") {
            etKullanici.setText(status);
        }

    } 
    catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();  
    }
} 
catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Debugger note:

code="1" login="OK" status="Basarili" kullanici="1"

but doesn't work

if(code=="1") {
    etKullanici.setText(status);
}

I am sorry for my bad English.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Dynasty
  • 29
  • 4

3 Answers3

2

Your variable "code" is a string. You need to check the value not the reference. Change it to:

if (code.equals("1"))
Thomas R.
  • 7,988
  • 3
  • 30
  • 39
1

JSONObject veri_json; try { veri_json = new JSONObject(response);

                                try {
                                    String code,status,login,kullanici;
                                    code = veri_json.getString("code");
                                    status = veri_json.getString("status");
                                    login = veri_json.getString("login");
                                    kullanici = veri_json.getString("kullanici");


                                    if(code.equalsIgnoreCase("1")) {
                                        etKullanici.setText(status);
                                    }

} catch (JSONException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

in above code i have to remove toString while u get object data. You have to tried above code it works like charm

Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29
1

You must compare the String with

String.equals()

Like:

if (code.equals("1")) {...}

code == "1" tests for reference equality (so it will never return true because "1" is the same as new String("1"), which isn't the same object as code.

DominicM
  • 2,186
  • 5
  • 24
  • 42