Retrofit 2 + RxAndroid:处理错误(4xx和大约200)

时间:2017-03-27 12:53:52

标签: android error-handling retrofit2 rx-android http-error

我看到很多关于使用改装与RxAndroid相结合的错误处理的文章和问题,但我无法正确设置我的配置。

我想要完成什么:

  1. 收到4xx代码时:在onError()
  2. 上处理它
  3. 收到2xx代码时:

    - >尝试解析预期的响应并将其传递给onNext()

    - >如果不可能,请尝试将JSON答案转换为MyAPIError类(带有errorNum和消息的简单POJO)并在onError上传递它。

  4. 因此,2xx或4xx http代码可能最终出现在onError()中。

    目前我有什么:

      

    ServiceGenerator.java

    public class ServiceGenerator {
    
        private static final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor()
                .setLevel(HttpLoggingInterceptor.Level.BODY);
    
    
        private static final Gson gson = new GsonBuilder()
                .registerTypeAdapter(DateTime.class, (JsonDeserializer<DateTime>) (json, typeOfT, context) -> new DateTime(json.getAsJsonPrimitive().getAsLong())).create();
    
    
        public static Retrofit buildRetrofit() {
            return new Retrofit.Builder()
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .baseUrl("....myURL...")
                    .client(getOkHttpClient()).build();
        }
    
        private static OkHttpClient getOkHttpClient() {
            return new OkHttpClient.Builder()
                    .addInterceptor(interceptor)
                    .addInterceptor(chain -> {
                        Request request = chain.request().newBuilder()
                                   .addHeader(ProductionSettings.HEADER_KEY_TOKEN, "myToken here")
                                    .build();
                        return chain.proceed(request);
                    })
                    .followRedirects(true)
                    .followSslRedirects(true)
                    .build();
        }
    
    
        public static <S> S createService(final Class<S> serviceClass) {
            return buildRetrofit().create(serviceClass);
        }
    }
    
      

    NetworkComunnication.java

    public class NetworkCommunication implements UserAPI {
        private static NetworkCommunication instance;
        private final UserAPI userAPI;
    
        private NetworkCommunication() {
            this.userAPI = ServiceGenerator.createService(UserAPI.class);
        }
    
        public static NetworkCommunication getInstance() {
            if (instance == null) {
                instance = new NetworkCommunication();
            }
            return instance;
        }
    
        @Override
        public Observable<UserResponse> updateUser(@Path(UsersSchema.ObjectId) String objectId, @Body final Map<String, Object> body) {
            return userAPI.updateUser(objectId, body);
        }
    }
    
      

    UserIteractor.java

    public class UserIteractor {
        private final UserMapper mapper;
    
        public UserIteractor() {
            this.mapper = new UserMapper();
        }
    
        public Observable<Boolean> updateUser(Long userObjectID, UserViewModel model) {
            Map<String, Object> reqBody = mapper.mapToUpdateRequestBody(model);
            return NetworkCommunication
                    .getInstance()
                    .updateUser(userObjectID, reqBody)
                    .map(prodUserResponse -> true);
        }
    
    }
    

    我希望在它到达UserInteractor类之前处理所有内容,因此它对所有Interactors都是通用的,我不必为所有请求创建相同的逻辑。 我怎样才能使这个尽可能通用并尽可能“顶”?

0 个答案:

没有答案