它成功获得名称,状态和登录信息。但它将0分配给u_id(用户ID),它应该是4.也许它可以将Id作为Json的字符串获取?我不知道整数的正确JSON格式。
JSON:
{
"id": "4",
"name": "nuku",
"status": "1",
"login": "sucess"
}
POJO: 公共类LoginModel {
@Expose
public static int id;
@Expose
public String name;
@Expose
private String status;
@Expose
private String login;
...}
接口:
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);
}
改装功能调用:
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();
}
});
}
答案 0 :(得分:0)
您的LoginModel不正确或发送JSON的人没有正确执行。 JSON中的ID是字符串,而不是整数(注意双引号)
您必须在LoginModel中将ID类型更改为String,但是,IMO,更好的解决方案是将其作为整数接收。
这是一个带有Integer的JSON示例:
{
"email":"some@email.com",
"age":25,
"man":true,
"password":"123456"
}
答案 1 :(得分:0)
我尝试将Id作为字符串,但改装为其分配了null,因此我只使用此echo json_encode将我的JSON转换为整数($ output,JSON_NUMERIC_CHECK);它工作了!