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

只谈情不闲聊 提交于 2019-12-20 20:35:14

问题


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

I under stand that this is down to GSON.

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

   {  
       "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.

Would anyone have any suggestions?

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


回答1:


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




回答2:


That's because the parser is expecting an int whereas the actual value it got was float. What you can do is, change that value's type from int to float in your model.

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.



来源:https://stackoverflow.com/questions/35095497/java-lang-numberformatexception-expected-an-int-but-was-0-6-at-line-1-column-84

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!