Get response status code using Retrofit 2.0 and RxJava

后端 未结 3 2011
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 15:48

I\'m trying to upgrade to Retrofit 2.0 and add RxJava in my android project. I\'m making an api call and want to retrieve the error code in case of an error response from th

3条回答
  •  天命终不由人
    2020-11-29 16:20

    You should note that as of Retrofit2 all responses with code 2xx will be called from onNext() callback and the rest of HTTP codes like 4xx, 5xx will be called on the onError() callback, using Kotlin I've came up with something like this in the onError() :

    mViewReference?.get()?.onMediaFetchFinished(downloadArg)
      if (it is HttpException) {
        val errorCode = it.code()
        mViewReference?.get()?.onMediaFetchFailed(downloadArg,when(errorCode){
          HttpURLConnection.HTTP_NOT_FOUND -> R.string.check_is_private
          else -> ErrorHandler.parseError(it)
        })
      } else {
        mViewReference?.get()?.onMediaFetchFailed(downloadArg, ErrorHandler.parseError(it))
      }
    

提交回复
热议问题