0

我尝试使用 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);
4

2 回答 2

0

您的 JSON 响应的模型类:

虚拟的.java

public class dummy {


    /**
     * 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"}]
     */

    private int code;
    private Object error_message;
    private List<DataBean> data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getError_message() {
        return error_message;
    }

    public void setError_message(Object error_message) {
        this.error_message = error_message;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * 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
         */

        private String ID;
        private String msisdn;
        private String operator_id;
        private String service;
        private String status;
        private String apps_status;
        private String sub_start;
        private String sub_end;
        private String renewal;

        public String getID() {
            return ID;
        }

        public void setID(String ID) {
            this.ID = ID;
        }

        public String getMsisdn() {
            return msisdn;
        }

        public void setMsisdn(String msisdn) {
            this.msisdn = msisdn;
        }

        public String getOperator_id() {
            return operator_id;
        }

        public void setOperator_id(String operator_id) {
            this.operator_id = operator_id;
        }

        public String getService() {
            return service;
        }

        public void setService(String service) {
            this.service = service;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getApps_status() {
            return apps_status;
        }

        public void setApps_status(String apps_status) {
            this.apps_status = apps_status;
        }

        public String getSub_start() {
            return sub_start;
        }

        public void setSub_start(String sub_start) {
            this.sub_start = sub_start;
        }

        public String getSub_end() {
            return sub_end;
        }

        public void setSub_end(String sub_end) {
            this.sub_end = sub_end;
        }

        public String getRenewal() {
            return renewal;
        }

        public void setRenewal(String renewal) {
            this.renewal = renewal;
        }
    }
}

打电话给Call<dummy> getData();

Call<dummy> call1 = apiInterface.getData();
        call1.enqueue(new Callback<dummy>() {
            @Override
            public void onResponse(Call<dummy> call, final Response<dummy> response) {
                if (response.body() != null) {
                }
            }

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

            }
        });
于 2018-04-11T11:34:40.897 回答
0

为json响应制作pojo类,如下所示..

public class Datum {

@SerializedName("ID")
private String iD;
@SerializedName("msisdn")
private String msisdn;
@SerializedName("operator_id")
private String operatorId;
@SerializedName("service")
private String service;
@SerializedName("status")
private String status;
@SerializedName("apps_status")
private String appsStatus;
@SerializedName("sub_start")
private String subStart;
@SerializedName("sub_end")
private String subEnd;
@SerializedName("renewal")
private String renewal;

public String getID() {
    return iD;
}

public void setID(String iD) {
    this.iD = iD;
}

public String getMsisdn() {
    return msisdn;
}

public void setMsisdn(String msisdn) {
    this.msisdn = msisdn;
}

public String getOperatorId() {
    return operatorId;
}

public void setOperatorId(String operatorId) {
    this.operatorId = operatorId;
}

public String getService() {
    return service;
}

public void setService(String service) {
    this.service = service;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getAppsStatus() {
    return appsStatus;
}

public void setAppsStatus(String appsStatus) {
    this.appsStatus = appsStatus;
}

public String getSubStart() {
    return subStart;
}

public void setSubStart(String subStart) {
    this.subStart = subStart;
}

public String getSubEnd() {
    return subEnd;
}

public void setSubEnd(String subEnd) {
    this.subEnd = subEnd;
}

public String getRenewal() {
    return renewal;
}

public void setRenewal(String renewal) {
    this.renewal = renewal;
}

}

然后做出响应 pojo 类,如下所示..

public class ResponseData {

@SerializedName("code")
private Integer code;
@SerializedName("error_message")
private Object errorMessage;
@SerializedName("data")
private List<Datum> data = null;

public Integer getCode() {
    return code;
}

public void setCode(Integer code) {
    this.code = code;
}

public Object getErrorMessage() {
    return errorMessage;
}

public void setErrorMessage(Object errorMessage) {
    this.errorMessage = errorMessage;
}

public List<Datum> getData() {
    return data;
}

public void setData(List<Datum> data) {
    this.data = data;
}

}

然后在使api通过响应类之后,如下所示.. @Get() CallgetData();

当您调用如下代码的活动时..

    Call<List<ResponseData>> call = api.getData();

    call.enqueue(new Callback<List<ResponseData>>() {
    @Override
    public void onResponse(Call<List<ResponseData>> call, Response<List<ResponseData>> response) {
        if (response.isSuccessful() && response.body().isSuccessful()){
            ResponseData data=response.body();
            //Log.d("Message:",data.getErrorMessage);
        }
    }

    @Override
    public void onFailure(Call<List<Hero>> call, Throwable t) {
        Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
});
于 2018-04-11T12:02:52.003 回答