我尝试使用 Retrofit 调用数据,但我遇到的问题是我的内部方法callBack ()
无法执行,(onResponse 已执行但 onFailure 已执行)我的代码有什么问题?提前致谢。
这是我打电话进行改造的时候:
private void checkSubscription() {
Call<APIResponse<List<DataStatusSubscribe>>> call = ServicesFactory.getService(this).getStatusSubscribe(own.msisdn);
call.enqueue(new Callback<APIResponse<List<DataStatusSubscribe>>>() {
// ======== This method won't execute ===========
**@Override
public void onResponse(Call<APIResponse<List<DataStatusSubscribe>>> call, Response<APIResponse<List<DataStatusSubscribe>>> response) {
if (response.isSuccessful() && response.body().isSuccessful()){
List<DataStatusSubscribe> dataStatusSubscribes = response.body().data;
String status = dataStatusSubscribes.get(dataStatusSubscribes.size()-1).getStatus();
if (status.equals("0")){
showPopupSubscribe();
}
}**
}
// ======== This method won't execute ===========
@Override
public void onFailure(Call<APIResponse<List<DataStatusSubscribe>>> call, Throwable t) {
}
});
}
这是我的 JSON 数据结构:
{
"code": 200,
"error_message": null,
"data": [
{
"ID": "6917",
"msisdn": "2013304044",
"operator_id": "3",
"service": "AB3",
"status": "0",
"apps_status": "0",
"sub_start": "2018-04-10 16:48:28",
"sub_end": "2018-04-10 16:59:50",
"renewal": "0000-00-00"
}
]
}
这是我的 APIResponse :
public class APIResponse<T> extends BaseResponse {
public T data;
}
这是我的 BaseResponse :
public abstract class BaseResponse {
@SerializedName("code")
public String code;
public boolean isSuccessful(){
if(code.equalsIgnoreCase("200")){
return true;
}
return false;
}
}
这是 ServiceFactory.java :
public class ServicesFactory {
private static boolean ENABLE_LOGGING = BuildConfig.DEBUG;
private static String BASE_URL = "http://abc/def/";
public static Services getService(Context context) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(generateClient(context))
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(Services.class);
}
private static OkHttpClient generateClient(Context context) {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS);
if (ENABLE_LOGGING) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
clientBuilder.addInterceptor(loggingInterceptor);
}
return clientBuilder.build();
}
}
这是我的界面:
@FormUrlEncoded
@POST("checkSubscribe")
Call<APIResponse<List<DataStatusSubscribe>>> getStatusSubscribe(@Field("msisdn") String msisdn);