【问题标题】:Mysql, Android Retrofit: Attempt to invoke interface method on a null object referenceMysql,Android Retrofit:尝试在空对象引用上调用接口方法
【发布时间】:2018-12-04 12:08:01
【问题描述】:

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

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;
}

}

主活动

        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) {

            }
    });

【问题讨论】:

    标签: android mysqli retrofit2


    【解决方案1】:

    试试这个改变。

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

    【讨论】:

    • 应用程序不再崩溃,但不会将用户重定向到新活动或显示消息
    • 检查您是否收到回复
    • 还根据服务器响应检查响应 pojo 类是否正确
    • 我在运行 php 代码时收到 json 响应。如何检查响应 pojo 类先生?
    • 不需要你的php代码我只需要api调用后的服务器响应。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2018-11-20
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多