在改进onResponse方法时得到零点异常

时间:2016-07-31 17:22:38

标签: android nullpointerexception retrofit2

我的Json:

{
  "status": "1",
  "login": "sucess"
}

这是我的模特课:

public class LoginModel {

    @SerializedName("status")
    @Expose
    private  String status;

    @SerializedName("login")
    @Expose
    private  String login;
...}

这是我的界面:

public interface RestInterface {

    String url = "http://192.168.10.2/tourist/v1";

    @FormUrlEncoded
    @POST("/login")
    Call<LoginModel> Login(@Field("email") String email,
                           @Field("pass") String passs);
}

这是我的通话方式:

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(RestInterface.url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    //Creating Rest Services
    final RestInterface restInterface = retrofit.create(RestInterface.class);

    //Calling method to get check login
    Call<LoginModel> response = restInterface.Login(Email.getText().toString(), Password.getText().toString());
    response.enqueue(new Callback<LoginModel>() {
        @Override
        public void onResponse(Response<LoginModel> response, Retrofit retrofit) {
            finish();
            startActivity(getIntent());

            Email.setText("");
            Password.setText("");

            LoginModel lm = response.body();

            if ( lm.getStatus().equals("1")) {  //login Success

                Toast.makeText(LoginActivity.this, "Login In SuccessFully", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(LoginActivity.this,AfterLoginActivity.class);
                startActivity(i);

            } else if (lm.getStatus().equals("0"))  // login failure
            {
                Toast.makeText(LoginActivity.this, "Invalid UserName/Password ", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Throwable t) {
            finish();
            startActivity(getIntent());
            //  String merror = error.getMessage();
            Toast.makeText(LoginActivity.this, "try again", Toast.LENGTH_LONG).show();            }
    });

在line =&gt;处获得零点异常。lm.getstatus()等于(1); 在改装1.9之前它工作正常然后我把它改成了Retrofit 2并且现在得到零点异常了!

1 个答案:

答案 0 :(得分:1)

https://inthecheesefactory.com/blog/retrofit-2.0/en对Retrofit 2.0中的新URL声明模式进行了澄清。它建议

- Base URL: always ends with /
- @Url: DO NOT start with /

在您的情况下,网址可能会被解释为“http://192.168.10.2/login”。 尝试使用

String url = "http://192.168.10.2/tourist/v1/";

@POST("login")