我正在使用Retrofit2和RxJava2CallAdapterFactory。
RxJava2CallAdapterFactory
我使用的API的返回状态代码始终为200,对于成功和响应JSON字符串,JSON结构完全不同。由于状态码始终为200,因此始终调用onResponse()方法。因此,在错误情况下,我无法从JSON中提取错误消息。
解决方案1:
我ScalarsConverterFactory用来获取响应String并手动使用Gson来解析响应。如何在不使用GSON或android中任何其他库的情况下使用改造获得String响应
ScalarsConverterFactory
该解决方案的问题 :我打算使用RxJava2CallAdapterFactory来使改装方法返回DataModel类。
我需要找到针对此问题的最佳解决方案,以这种方式,我可以继续从Retrofit方法返回数据模型类,并以某种方式从响应中识别错误条件(确定响应JSON与数据模型不匹配),然后解析错误JSON转换为数据模型。
改造客户
public static Retrofit getClient(String url) { if (apiClient == null) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build(); apiClient = new Retrofit.Builder() .baseUrl(url) /*addCallAdapterFactory for RX Recyclerviews*/ .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) /* add ScalarsConverterFactory to get json string as response */ // .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) // .addConverterFactory(GsonConverterFactory.create(gson)) .client(httpClient) .build(); } return apiClient; }
方法
public static void getLoginAPIResponse(String username, String password, String sourceId, String uuid, final HttpCallback httpCallback) { baseUrl = AppPreference.getParam(UiUtils.getContext(), SPConstants.BASE_URL, "").toString(); ApiInterface apiService = ApiClient.getClient(baseUrl).create(ApiInterface.class); Call<LoginBean> call = apiService.getLoginResponse(queryParams); call.enqueue(new Callback<LoginBean>() { @Override public void onResponse(Call<LoginBean> call, Response<LoginBean> response) { if (response.body().isObjectNull()) { httpCallback.resultCallback(APIConstants.API_LOGIN, HttpCallback.REQUEST_TYPE_GET, HttpCallback.RETURN_TYPE_FAILURE, 0, null); return; } httpCallback.resultCallback(APIConstants.API_LOGIN, HttpCallback.REQUEST_TYPE_GET, HttpCallback.RETURN_TYPE_SUCCESS, response.code(), response.body()); } @Override public void onFailure(Call<LoginBean> call, Throwable t) { // Log error here since request failed httpCallback.resultCallback(APIConstants.API_APP_VERIFICATION, HttpCallback.REQUEST_TYPE_GET, HttpCallback.RETURN_TYPE_FAILURE, 0, t); t.printStackTrace(); } }); }
接口
@GET("App/login") Call<LoginBean> getLoginResponse(@QueryMap Map<String, String> queryMap);
PS:该API目前无法更改,因为其他一些应用程序正在使用它。
Gson解析器不会返回空对象实例,以使我理解json结构和数据模型不匹配。
RestAdapter在Retrofit 2中已弃用
我正在寻找解决此问题的最佳方法,最好避免手动进行json解析,并充分利用改造和RX适配器的优势。
编辑
因此,响应码200
response.isSuccessful() == true
response.body() != null 因为Gson从不创建空实例或在JSON结构不匹配时抛出任何异常,所以也是如此
response.body() != null
response.errorBody() == null 始终作为响应从服务器作为输入流发送。
response.errorBody() == null
if (response.isSuccessful() && response.body() != null) { //control always here as status code 200 for error condition also
}else if(response.errorBody()!=null){ //control never reaches here }
编辑2
解
该解决方案基于anstaendig答案
基础API标记
public class BaseApiBean<T> { @Nullable private T responseBean; @Nullable private ErrorBean errorBean; public BaseApiBean(T responseBean, ErrorBean errorBean) { this.responseBean = responseBean; this.errorBean = errorBean; } public T getResponseBean() { return responseBean; } public void setResponseBean(T responseBean) { this.responseBean = responseBean; } public ErrorBean getErrorBean() { return errorBean; } public void setErrorBean(ErrorBean errorBean) { this.errorBean = errorBean; } }
基本解串器
public abstract class BaseDeserializer implements JsonDeserializer<BaseApiBean> { @Override public BaseApiBean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { // Get JsonObject final JsonObject jsonObject = json.getAsJsonObject(); if (jsonObject.has("result")) { /* {"result":"404"}*/ ErrorBean errorMessage = new Gson().fromJson(jsonObject, ErrorBean.class); return getResponseBean(errorMessage); } else { return getResponseBean(jsonObject); } } public abstract BaseApiBean getResponseBean(ErrorBean errorBean); public abstract BaseApiBean getResponseBean(JsonObject jsonObject); }
每个API的自定义反序列化器
public class LoginDeserializer extends BaseDeserializer { @Override public BaseApiBean getResponseBean(ErrorBean errorBean) { return new LoginResponse(null, errorBean); } @Override public BaseApiBean getResponseBean(JsonObject jsonObject) { LoginBean loginBean = (new Gson().fromJson(jsonObject, LoginBean.class)); return new LoginResponse(loginBean, null); } }
习惯回应豆
public class LoginResponse extends BaseApiBean<LoginBean> { public LoginResponse(LoginBean responseBean, ErrorBean errorBean) { super(responseBean, errorBean); } }
客户
public class ApiClient { private static Retrofit apiClient = null; private static Retrofit apiClientForFeedBack = null; private static LoginDeserializer loginDeserializer = new LoginDeserializer(); private static AppVerificationDeserializer appVerificationDeserializer = new AppVerificationDeserializer(); public static Retrofit getClient(String url) { if (apiClient == null) { GsonBuilder gsonBuilder=new GsonBuilder(); gsonBuilder.registerTypeAdapter(LoginResponse.class, loginDeserializer); gsonBuilder.registerTypeAdapter(AppVerificationResponse.class, appVerificationDeserializer); Gson gson= gsonBuilder.create(); HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(interceptor) .retryOnConnectionFailure(true) .connectTimeout(15, TimeUnit.SECONDS) .build(); apiClient = new Retrofit.Builder() .baseUrl(url) /*addCallAdapterFactory for RX Recyclerviews*/ .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) /* add ScalarsConverterFactory to get json string as response */ // .addConverterFactory(ScalarsConverterFactory.create()) // .addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create(gson)) .client(httpClient) .build(); } return apiClient; }
处理响应
public static void getLoginAPIResponse(String username, String password, String sourceId, String uuid, final HttpCallback httpCallback) { baseUrl = AppPreference.getParam(getContext(), SPConstants.MT4_BASE_URL, "").toString(); ApiInterface apiService = ApiClient.getClient(baseUrl).create(ApiInterface.class); HashMap<String, String> queryParams = new HashMap<>(); queryParams.put(APIConstants.KEY_EMAIL, sourceId + username.toLowerCase()); queryParams.put(APIConstants.KEY_PASSWORD, Utils.encodePwd(password)); Call<LoginResponse> call = apiService.getLoginResponse(queryParams); call.enqueue(new Callback<LoginResponse>() { @Override public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) { if (response.body().getResponseBean()==null) { httpCallback.resultCallback(APIConstants.API_LOGIN, HttpCallback.REQUEST_TYPE_GET, HttpCallback.RETURN_TYPE_FAILURE, 0, response.body().getErrorBean()); return; } httpCallback.resultCallback(APIConstants.API_LOGIN, HttpCallback.REQUEST_TYPE_GET, HttpCallback.RETURN_TYPE_SUCCESS, response.code(), response.body().getResponseBean()); } @Override public void onFailure(Call<LoginResponse> call, Throwable t) { // Log error here since request failed httpCallback.resultCallback(APIConstants.API_APP_VERIFICATION, HttpCallback.REQUEST_TYPE_GET, HttpCallback.RETURN_TYPE_FAILURE, 0, t); t.printStackTrace(); } }); }
因此,您从同一端点有两个不同的成功响应(状态码200)。一个是实际的数据模型,另一个是错误(都像这样的json结构?:
有效的LoginBean响应:
{ "id": 1234, "something": "something" }
错误回应
{ "error": "error message" }
您可以做的是拥有一个包装两种情况并使用自定义反序列化器的实体。
class LoginBeanResponse { @Nullable private final LoginBean loginBean; @Nullable private final ErrorMessage errorMessage; LoginBeanResponse(@Nullable LoginBean loginBean, @Nullable ErrorMessage errorMessage) { this.loginBean = loginBean; this.errorMessage = errorMessage; } // Add getters and whatever you need }
错误的包装器:
class ErrorMessage { String errorMessage; // And whatever else you need // ... }
然后,您需要一个JsonDeserializer:
JsonDeserializer
public class LoginBeanResponseDeserializer implements JsonDeserializer<LoginBeanResponse> { @Override public LoginBeanResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { // Based on the structure you check if the data is valid or not // Example for the above defined structures: // Get JsonObject final JsonObject jsonObject = json.getAsJsonObject(); if (jsonObject.has("error") { ErrorMessage errorMessage = new Gson().fromJson(jsonObject, ErrorMessage.class); return new LoginBeanResponse(null, errorMessage) } else { LoginBean loginBean = new Gson().fromJson(jsonObject, LoginBean.class): return new LoginBeanResponse(loginBean, null); } } }
然后将此反序列化器添加到中GsonConverterFactory:
GsonConverterFactory
GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(LoginBeanResponse.class, new LoginBeanResponseDeserializer()).create(): apiClient = new Retrofit.Builder() .baseUrl(url) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create(gsonBuilder)) .client(httpClient) .build();
这是我可以想到的唯一方法。但是,正如已经提到的,这种API设计是错误的,因为存在状态代码是有原因的。我仍然希望这会有所帮助。
编辑:然后,您可以在调用Retrofit的类中做什么(如果您已经使用RxJava 从转换Call<LoginBeanResponse>为Single<LoginBeanResponse>)实际上返回了正确的错误。就像是:
Call<LoginBeanResponse>
Single<LoginBeanResponse>
Single<LoginBean> getLoginResponse(Map<String, String> queryMap) { restApi.getLoginResponse(queryMap) .map(loginBeanResponse -> { if(loginBeanResponse.isError()) { Single.error(new Throwable(loginBeanResponse.getError().getErrorMessage())) } else { Single.just(loginBeanReponse.getLoginBean()) }}) }