Nukhba Arshad Nukhba Arshad - 1 year ago 97
JSON Question

retrofit is not getting the id of JSON object

it's getting name, status and login successfully. But it's assigning 0 to u_id (user id) , which should be 4. Maybe it's getting Id as a string from a Json perhaps?I dunno the correct JSON format for integers.


"id": "4",
"name": "nuku",
"status": "1",
"login": "sucess"

public class LoginModel {

public static int id;

public String name;

private String status;
private String login;


String url = "";
void Login(@Field("email") String email,
@Field("pass") String pass, Callback<LoginModel> cb);

retrofit function call:

RestAdapter adapter = new RestAdapter.Builder().setEndpoint(RestInterface.url).build();

//Creating Rest Services
final RestInterface restInterface = adapter.create(RestInterface.class);

//Calling method to get check login
restInterface.Login(email.getText().toString(), pass.getText().toString(), new Callback<LoginModel>() {
public void success(LoginModel model, Response response) {



int id = model.getUserId();
if (model.getStatus().equals("1")) { //login Success

SharedPreferences sharedPreferences = getSharedPreferences("MyLogin.txt", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("FirstLogin", true);

Toast.makeText(LoginActivity.this, "Login In SuccessFully"+model.getUserId(), Toast.LENGTH_SHORT).show();

manager.setPreferences(LoginActivity.this, "status", "1");
String status=manager.getPreferences(LoginActivity.this,"status");

Intent i = new Intent(LoginActivity.this,MainActivity.class);

String name = model.getUser_name();
i.putExtra("username", name);
// String image = p.getPlace_image();
// i.putExtra("Thumbnail", image);

// String nameStr = email.getText().toString();

// i.putExtra("text1", nameStr);

} else if (model.getStatus().equals("0")) // login failure
Toast.makeText(LoginActivity.this, "Invalid UserName/Pass ", Toast.LENGTH_SHORT).show();
Intent i = new Intent(LoginActivity.this, LoginActivity.class);


public void failure(RetrofitError error) {
String merror = error.getMessage();
Toast.makeText(LoginActivity.this, merror, Toast.LENGTH_LONG).show();

Answer Source

Either your LoginModel is not correct or the one sending the JSON is not doing it right. Your ID in the JSON is a String, not an Integer (notice the double quotes)

You'll have to change the ID type to String in your LoginModel, but, IMO, a better solution would be receive it as an Integer.

This is an example of a JSON with an Integer in it:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download