在调用方法中改造空错误

我想通过改造从 MySQL 数据库中获取最后一个 id 但是当运行项目有错误时,使用邮递员测试的调用方法值并获取值


APIClient.java :


public class APIClient {

private static final String BASE_URL = "http://**********/Api/";

private static Retrofit retrofit = null;

public static Retrofit getClient() {

    if (retrofit == null) {

        retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create())

                .build();

    }

    return retrofit;

}

}


APIIinterface.java :


public interface APIIinterface {


@POST("Agahi/getlastid.php")

Call<AgahiLastId> getAgahiLastId(); }

AgahiLastId.java :


public class AgahiLastId {


    @SerializedName("agahi_id")

    private int agahi_id;


    public int getAgahi_id() {

        return agahi_id;

    }


    public void setAgahi_id(int agahi_id) {

        this.agahi_id = agahi_id;

    }

}

主活动.java :


       public class MainActivity extends AppCompatActivity {



private APIIinterface apiIinterface;



@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


Call<AgahiLastId> agahiLastIdCall = apiIinterface.getAgahiLastId();


agahiLastIdCall.enqueue(new Callback<AgahiLastId>() {

    @Override

    public void onResponse(Call<AgahiLastId> call, Response<AgahiLastId> response) {

        if (response.isSuccessful()) {

            Log.d("lts","ok");

        }

    }


    @Override

    public void onFailure(Call<AgahiLastId> call, Throwable t) {


    }

});}

我有这一行的错误:


Call<AgahiLastId> agahiLastIdCall = apiIinterface.getAgahiLastId();

错误日志:


 Attempt to invoke the interface method 'retrofit2. Call app.zagroszoom.mseif.com.zagroszoom.webService.APIIinterface.getAgahiLastId()' on a null object reference


慕的地6264312
浏览 227回答 2
2回答

犯罪嫌疑人X

你需要在你的Mainactivity里面添加以下内容onCreate之前Call<AgahiLastId> agahiLastIdCall = apiIinterface.getAgahiLastId();apiIinterface=&nbsp;APIClient.getClient().create(APIIinterface.class);在此处了解更多信息Retrofit
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java