onResponse在通过改造调用数据时不会执行

时间:2018-04-11 11:23:02

标签: android retrofit jsonresponse

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

2 个答案:

答案 0 :(得分:0)

您的JSON响应的模型类:

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

            }
        });

答案 1 :(得分:0)

为下面的json响应制作pojo类。

intEqual(int, int): # @intEqual(int, int)
  cmp edi, esi
  sete al
  ret
otherEqual(int, int): # @otherEqual(int, int)
  cmp edi, esi
  sete al
  ret

}

然后像下面那样制作响应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;
}

}

之后make api传递响应类如下所示..     @得到()     CallgetData();

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

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