Caused by: java.lang.NumberFormatException: Invalid double: “” with blank value

♀尐吖头ヾ 提交于 2019-12-02 17:02:25

问题


I get ther error when JSON string have some blank value, getting from server, how to deal with blank values here is JSON

{  
   "status":"success",
   "data":[  
      {  
         "id":"1",
         "name":"ABC",
         "address":"ABC, QWE",
         "lat":"16.799999",
         "lng":"96.150002",
         "admin_id":"4",
         "is_approved":"1",
         "added":"2015-08-07 11:17:12",
         "status":"1",
         "image_file":"",
         "image_width":"",
         "image_height":""
      }
   ]
}

Gson gson = new GsonBuilder().serializeNulls().create();
Type listType = new TypeToken<List<MyData>>() {}.getType();
myDataArrayList = gson.fromJson(response.getString("data"), listType);

In MyData class I tried to change lat, long type int and changed value in json also for the same but still showing same error

public class MyData implements Parcelable {

    public int id;

    public String name;

    public String address;

    public String lat;

    public String lng;

    public String added;

    public int status;

    public String image_file;

    public int image_width;

    public int image_height;

}

Showing error in this line

myDataArrayList = gson.fromJson(response.getString("data"), listType);

回答1:


Change the type of image_width and image_height to String in MyData Class

public String image_width;

public String image_height;

As int will not be able to store "" this string, and at the time of using it you can parse it into int.




回答2:


public int image_width;     // Expected integer value while parsing 

public int image_height;   // Expected integer value while parsing

as you can see below you are having string fields . Initialize below vale to 0

"image_width":0,
"image_height":0


来源:https://stackoverflow.com/questions/38656731/caused-by-java-lang-numberformatexception-invalid-double-with-blank-value

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