Android的改造GET请求ConversionException问题 [英] Android Retrofit GET request ConversionException issue

查看:225
本文介绍了Android的改造GET请求ConversionException问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的改造,使REST请求和(使用@SerializedName批注与GSON)创建相应的模型对象。有偶尔导致ConversionException虽然一个特定的GET请求,我无法追查原因。我会在同一时间10-20相似的GET请求几乎具有不同的参数,其中约90%的正确返回。剩余的失败,一个ConversionException:

retrofit.converter.ConversionException:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_OBJECT但STRING在第1行1列

在这个转换的异常发生,任何时候,我尝试再次作出这样的请求(不杀应用程序),它会始终具有相同的转换例外。如果我杀了应用程序,并开始完全新鲜的,它可能会工作。

我试图寻找在浏览器对于有这种转换的异常请求,JSON响应,但它看起来总是正确的。我已经做了不合格的要求,并成功的与JSON看起来完全相同之间有许多比较。

我的问题:

1)为什么这些转换异常发生时,据我所知,响应数据始终是正确的?

2)不改造做任何形式的缓存GET请求的?这或许可以解释为什么重新请求失败的请求继续失败,直到我杀了,然后重新启动应用程序。

谢谢!


解决方案

  

1)为什么这些转换异常发生时,据我所知,响应数据始终是正确的?


您预期的数据,看起来像这样:

  {foo的栏}

但GSON找到喜欢的东西更多:

 您好!

据预计开始的JSON对象(又名 {字符),但它发现带状的字符,而不是


  

2)不改造做任何形式的缓存GET请求的?这或许可以解释为什么重新请求失败的请求继续失败,直到我杀了,然后重新启动应用程序。


改造没有任何缓存

根据您所使用的HTTP客户端,它可以缓存依赖于一个标题 GET 请求的响应。通常你必须选择加入缓存行为在一个HTTP客户端,所以如果你还没有做到这一点,我怀疑它的功能。

I'm using Retrofit to make REST requests and create the corresponding model objects (with gson using the @SerializedName annotation). There is one particular GET request that occasionally causes a ConversionException though, and I am having trouble tracking down the cause. I will make 10-20 similar GET requests nearly at the same time with varying parameters and about 90% of them return correctly. The remaining fail with a ConversionException:

retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1

Once this conversion exception has occurred, any time I try to make that request again (without killing the app) it will always have the same conversion exception. If I kill the app and start completely fresh, it will probably work.

I've tried looking at the json response in the browser for requests that had this conversion exception, but it always looks correct. I've done many comparisons between "failed" requests and successful ones and the json looks identical.

My questions:

1) Why are these conversion exceptions occurring, when as far as I can tell, the response data is always correct?

2) Does retrofit do any sort of caching for GET requests? This might explain why re-requesting a failed request continues to fail until I kill and restart the application.

Thanks!

解决方案

1) Why are these conversion exceptions occurring, when as far as I can tell, the response data is always correct?

You expected data that looks like this:

{"foo":"bar"}

But Gson found something more like:

Hello!

It was expecting a JSON object beginning (aka the { character) but it found a string-like character instead.

2) Does retrofit do any sort of caching for GET requests? This might explain why re-requesting a failed request continues to fail until I kill and restart the application.

Retrofit has no caching whatsoever.

Depending on what HTTP client you are using, it may cache the response of a GET request depending on the headers. Usually you have to opt-in to cache behavior in an HTTP client so if you haven't done that, I doubt it's enabled.

这篇关于Android的改造GET请求ConversionException问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆