连接Android旋转器和php文件数组



下面是一个函数,用于在旋转控件中显示数据。

这是我得到的响应。原因:java.lang.NullPointerException: Attempt to invoke interface method 'retrofit2。调用co. key .smartcare.apiutilities. apiservice . fetchdocs()'对空对象引用

功能:public void fetchDoctors(){

apiService.fetchDocs().enqueue(new Callback<List<Doctor>>() {
@Override
public void onResponse( Call<List<Doctor>> call, Response<List<Doctor>> response ) {
List<Doctor> doctors = new ArrayList<>();
List<String> docs = new ArrayList<>();
if( response.body() != null ){
doctors.addAll(response.body());
for( int i = 0; i <= doctors.size(); i++ ){
docs.add(doctors.get(i).getName());
}
ArrayAdapter<String> doctorsSpinner = new ArrayAdapter<>(getApplicationContext(), layout.simple_spinner_item, docs);
doctorsSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
doctor.setAdapter(doctorsSpinner);
}else{
Toast.makeText(BookingActivity.this, "0 doctors have been retrieved. nKindly, contact customer care", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure( Call<List<Doctor>> call, Throwable t ) {
Toast.makeText(BookingActivity.this, "Error: "+t.toString(), Toast.LENGTH_SHORT).show();
}
});
}

在这种情况下,您的改装api调用接口不能正确初始化。将这些添加到您的构建中。gradle。

implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'

然后将其添加到java代码中。

public static ApiInterface getClient() {
if(retrofitServerOne==null) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(loggingInterceptor)
.callTimeout(1, TimeUnit.MINUTES)
.connectTimeout(1, TimeUnit.MINUTES)
.build();
retrofitServerOne=new Retrofit.Builder()
.baseUrl("Url u need")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build().create(ApiInterface.class);
}
return retrofitServerOne;
}

试试这个方法,它也会显示api调用的日志。将ApiInterface替换为我们的接口

相关内容

  • 没有找到相关文章

最新更新