简体   繁体   English

从retrofit2和rxjava中的错误中获取url

[英]get url from error in retrofit2 and rxjava

I'm using retrofit 2 with rxjava to send rest requests.我正在使用带有 rxjava 的改造 2 来发送休息请求。 and Gson as json parse.和 Gson 作为 json 解析。

if Gson cannot parse json string then throws java.lang.IllegalStateException with some message.如果 Gson 无法解析 json 字符串,则抛出java.lang.IllegalStateException并带有一些消息。 my issue is that i don't know this error happened on which url.我的问题是我不知道这个错误发生在哪个网址上。

if error type is HttpException i can get url from that but in other types like java.lang.IllegalStateException and TimeoutException just access to error message is available and i can't see the url.如果错误类型是HttpException我可以从中获取 url,但在其他类型中,如java.lang.IllegalStateExceptionTimeoutException只能访问错误消息,而我看不到 url。

how can i achieve to url in rxjava and retrofit catch.我怎样才能在 rxjava 中实现 url 并改造捕获。

that's my code:那是我的代码:

mainApi.getSomething(offset, limit, distance, latitude, longitude)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({it:Response<Something>
                val entities = it.body()?.entities
                callBack.onResponse(it.raw(), entities)
            }, {//it:throwable
                if (t is HttpException) {
                   Log.d("logUrl", "HttpException : ${t.response().raw().request().url()} : ${t.code()} :${t.message()}")
                } else//something like IllegalStateException or TimeoutException
                     Log.d("logUrl", "Failure : ${t.localizedMessage}")
                })
               })

new Consumer() method always provide a message on any type of error even if GSON error即使 GSON 错误,新的 Consumer() 方法也始终提供有关任何类型错误的消息

 mGetDeviceObserver.subscribeOn(Schedulers.newThread())
         .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Consumer<RegData>() {
    @Override
    public void accept(@NonNull RegData mRegData) throws Exception {
    }
}, new Consumer<Throwable>() {
    @Override
    public void accept(@NonNull Throwable t) throws Exception {
       if(t instanceof HttpException){ Log.d("path",((HttpException) t).response().raw().request().url().url().toString()); }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM