java.lang.NumberFormatException:需要一个整数,但在第 1 行第 8454 列处为 0.6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35095497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:43:38  来源:igfitidea点击:

java.lang.NumberFormatException: Expected an int but was 0.6 at line 1 column 8454

javaandroidgsonretrofitretrofit2

提问by DJ-DOO

I'm using the retrofit library for my calls in a demo project.

我在演示项目中使用改造库进行调用。

I received the following error:

我收到以下错误:

java.lang.NumberFormatException: Expected an int but was 0.6 at line 1 column 8454 path $.result.results.ads[2].acres

java.lang.NumberFormatException: 预期为 int 但在第 1 行第 8454 列路径 $.result.results.ads[2].acres 处为 0.6

I under stand that this is down to GSON.

我明白这取决于 GSON。

I will show you the JSON it's getting caught in:

我将向您展示它被捕获的 JSON:

   {  
       "ad_id":739580087654,
       "property_type":"site",
       "house_type":"",
       "selling_type":"private-treaty",
       "price_type":"",
       "agreed":0,
       "priority":2,
       "description":"Beautiful elevated 0.6 acre site - zoned residential - and within easy walk to this popular and scenic coastal village\r\n\r\n\r\nthe site area is zoned residential ( i.e. can be constructed on for residential home) and has beautiful coastal views\r\n\r\nSpiddal is an exceptionally popular location , just 8 miles west of Galway City but the area has not been over developed.\r\n\r\nAll services and family amenities are location in the village centre.\r\n\r\n",
       "price":135000,
       "bedrooms":null,
       "bathrooms":null,
       "tax_section":"0",
       "square_metres":0,
       "acres":0.6,   <----------------------TRIPPING UP HERE
       "features":[  
          "Zoned residential",
          "within easy walk of coastal village of Spiddal",
          "with coastal views"
       ],
       "ber_rating":"",
       "ber_code":"",
       "ber_epi":0,             
       "city":"",
       "general_area":"Connemara",
       "postcode":null,
       "latlon_accuracy":1,
       "main_email":"",
       "cc_email":"",
       "auction_address":"",
       "start_date":1384425002,
       "listing_date":1384425002,
       "agreed_date":0,
       "auction_date":0,
       "tags":1
    },

I'm not that experienced with Retrofit so decided to learn and integrate on this project.

我对 Retrofit 没有那么丰富的经验,所以决定学习和整合这个项目。

Would anyone have any suggestions?

有人会有什么建议吗?

I don't have any control over the JSON being sent down.

我对发送的 JSON 没有任何控制权。

回答by ayanokouji

Try using a floator doubleinstead of an int; 0.6is not an integer, it is a decimal. Note that java automatically interprets decimals as doubles; an example of a float would be 0.6f.

尝试使用floatdouble代替int; 0.6不是整数,它是小数。请注意,java 自动将小数解释为doubles; 浮动的一个例子是0.6f

回答by Msp

That's because the parser is expecting an intwhereas the actual value it got was float. What you can do is, change that value's type from intto floatin your model.

那是因为解析器期望的是 ,int而它得到的实际值是float。您可以做的是,在模型中将该值的类型从 更改intfloat

This might cause problems in your code wherever you are using that value. You can solve it by casting that float value to an integer.

无论您在何处使用该值,这都可能导致代码出现问题。您可以通过将该浮点值转换为整数来解决它。