【问题标题】:java.lang.NumberFormatException: Expected an int but was 0.6 at line 1 column 8454java.lang.NumberFormatException:应为 int,但在第 1 行第 8454 列为 0.6
【发布时间】:2016-05-07 20:04:03
【问题描述】:

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

我收到以下错误:

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

我知道这取决于 GSON。

我将向您展示它被捕获的 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
    },

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

有人有什么建议吗?

我无法控制向下发送的 JSON。

【问题讨论】:

  • 使用float 代替int
  • 在我的响应对象中?这不会导致存在整数的问题吗?
  • 在实际(java)代码中。
  • 这就是我所指的,在我的响应对象的java代码中
  • @TheCoffeeKid 谢谢你,成功了

标签: java android gson retrofit retrofit2


【解决方案1】:

尝试使用floatdouble 而不是int0.6 不是整数,而是小数。注意java自动将小数解释为doubles;一个浮点数的例子是0.6f

【讨论】:

  • 我正在寻找改造调用代码中的错误,但正如您所指出的,它在数据对象中。简单实用
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-01
    • 2020-06-21
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2019-04-28
    • 2022-09-16
    • 1970-01-01
    相关资源
    最近更新 更多