Nukhba Arshad Nukhba Arshad - 3 months ago 23
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.

JSON:

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


POJO:
public class LoginModel {

@Expose
public static int id;

@Expose
public String name;

@Expose
private String status;
@Expose
private String login;
...}


interface:

String url = "http://192.168.10.5/tourist/v1";
@FormUrlEncoded
@POST("/login")
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>() {
@Override
public void success(LoginModel model, Response response) {

//finish();
//startActivity(getIntent());

email.setText("");
pass.setText("");

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);
editor.commit();



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);
i.putExtra(MainActivity.EXTRA_USER,"username");
startActivity(i);



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

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

} 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);
startActivity(i);

}
}

@Override
public void failure(RetrofitError error) {
finish();
startActivity(getIntent());
String merror = error.getMessage();
Toast.makeText(LoginActivity.this, merror, Toast.LENGTH_LONG).show();
}
});
}

Answer

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:

{
 "email":"some@email.com",
 "age":25,
 "man":true,
 "password":"123456"
}
Comments