java.lang.String 的值不能转换为双精度


Value of java.lang.String cannot be converted to double

我有一个PHP Web服务,可以从MySQL数据库中提取数据。

我有一个调用该服务的安卓应用程序。

在我的WAMP服务器上本地,一切正常,但我只是将数据/服务上传到域并对其进行测试。

我收到此错误:

org.json.JSONException: Value  at Latitude of type java.lang.String cannot be converted to double

从这行代码:

double latitude = thisVenue.getDouble(TAG_LATITUDE);

导致此错误的值(通过Android Studio从日志中获取)是:

"Latitude":"51.05279"

我不明白为什么该值不能转换为双精度值。在本地,我的应用程序可以解析相同的数据集没有问题,所以我认为它与它的存储/检索方式有关?

编辑:这是以JSON返回的对象之一,请注意所有值都有引号

{"VenueID":"4","ListingID":"19","Name":"TempName1","Location":"207 10A St NW","DayOfWeek":"Mon","Featured":"0","FeaturedDistanceRange":"0","Latitude":"51.05279","Longitude":"-110.087509","StartHour":"4","StartHourType":"pm","StartMinute":"0","EndHour":"7","EndHourType":"pm","EndMinute":"0"}

我将其格式化为 JSON 如下:

    header("Content-type: application/json");
    print(json_encode(array('venues'=>$data)));

由于来自服务器的数据不是双精度的,因此您必须使用

double latitude = Double.valueOf(thisVenue.getString(TAG_LATITUDE));

检查你是否从getDouble()方法获得了有效的双精度(编译器不认为你是。如果以下内容在运行时编译并抛出 NumberfFormatException,则您知道您没有得到有效的双精度。而"纬度":"51.05279"看起来不像一个可解析的替身,"51.05279"就可以了。

double latitude = Double.parseDouble(thisVenue.getDouble(TAG_LATITUDE));