Mysql,Android改造:尝试在空对象引用上调用接口方法

时间:2018-12-04 12:08:01

标签: android mysqli retrofit2

我正在使用MySQL数据库开发一个Android应用程序。我想检查用户名是否在MySQL数据库表中注册。我是android和翻新的初学者。我收到错误尝试在空对象引用上调用接口方法的错误。下面是我的代码

ApiClient

public class ApiClient {
public static final String BASE_URL = "http://ipaddresshere:8080/";
public static Retrofit retrofit = null;

public static Retrofit getApiClient(){
    if(retrofit == null){
        retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();

    }
    return retrofit;
}

}

用户

public class User{
@SerializedName("response")
private String Response;

public String getResponse(){
    return Response;
}

}

MainActivity

        Call<User> call = apiInterface.performUser(un);
    call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if(response.body().getResponse().equals("ok")){
                    Intent intentLoadNewActivity = new Intent(MainActivity.this, NewC.class);
                    startActivity(intentLoadNewActivity);
                }else if(response.body().getResponse().equals("failed")){
                    Toast.makeText(MainActivity.this, "Not registered.", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
    });

1 个答案:

答案 0 :(得分:0)

尝试此更改。

    ApiInterface apiInterface=ApiClient.getApiClient().create(ApiInterface.class);
    Call<User> call = apiInterface.performUser(un);