java.lang.NumberFormatException:对于输入字符串:“-0. 086167157”

java.lang.NumberFormatException: For input string: "-0.‎086167157"

所以我为此苦苦挣扎了一段时间,对我来说,我看不出为什么 -0. 086167157 不是有效的双精度值。我只是想将从 API 调用返回的字符串值转换为双精度值(经度值)

错误信息如下:

java.lang.NumberFormatException: For input string: "-0.‎086167157"
W/System.err:     at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)
W/System.err:     at java.lang.Double.parseDouble(Double.java:547)
W/System.err:     at com.google.gson.stream.JsonReader.nextDouble(JsonReader.java:909)
W/System.err:     at com.google.gson.Gson.read(Gson.java:284)
W/System.err:     at com.google.gson.Gson.read(Gson.java:278)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.read(ReflectiveTypeAdapterFactory.java:129)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
W/System.err:     at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
W/System.err:     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
W/System.err:     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.read(ReflectiveTypeAdapterFactory.java:129)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
W/System.err:     at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:119)
W/System.err:     at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:218)
W/System.err:     at retrofit2.OkHttpCall.onResponse(OkHttpCall.java:112)
W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err:     at java.lang.Thread.run(Thread.java:762)

/

@Parcel(implementations = { LocationRealmProxy.class },
    value = Parcel.Serialization.BEAN,
    analyze = {Location.class})
public class Location extends RealmObject {

@PrimaryKey
@SerializedName("id")
int locationId;

@SerializedName("company_id")
int companyId;

@SerializedName("company_name")
String companyName;

@SerializedName("logo")
String logo;

@SerializedName("address_one")
String addressOne;

@SerializedName("address_two")
String addressTwo;

@SerializedName("address_three")
String addressThree;

@SerializedName("town")
String town;

@SerializedName("county")
String county;

@SerializedName("postcode")
String postcode;

@SerializedName("country")
String country;

@SerializedName("latitude")
Double latitude;

@SerializedName("longitude")
Double longitude;

@SerializedName("venue_type")
String venueType;

@SerializedName("type")
String type;

@SerializedName("added_date")
String addedDate;

@SerializedName("modified_date")
String modifiedDate;

@SerializedName("archived_date")
String archivedDate;

@SerializedName("status")
String status;

@SerializedName("map_thumbnail")
String mapThumbnail;

@SerializedName("venue_type_id")
int venueTypeId;

public Location(){

}

public Location(int locationId, int companyId, String companyName, String logo, String addressOne, String addressTwo, String addressThree,
                String town, String county, String postcode, String country, Double latitude, Double longitude, String venueType, String type,
                String addedDate, String modifiedDate, String archivedDate, String status, String mapThumbnail, int venueTypeId) {
    this.locationId = locationId;
    this.companyId = companyId;
    this.companyName = companyName;
    this.logo = logo;
    this.addressOne = addressOne;
    this.addressTwo = addressTwo;
    this.addressThree = addressThree;
    this.town = town;
    this.county = county;
    this.postcode = postcode;
    this.country = country;
    this.latitude = latitude;
    this.longitude = longitude;
    this.venueType = venueType;
    this.type = type;
    this.addedDate = addedDate;
    this.modifiedDate = modifiedDate;
    this.archivedDate = archivedDate;
    this.status = status;
    this.mapThumbnail = mapThumbnail;
    this.venueTypeId = venueTypeId;
}

//Getters and setters

/

 @GET(NetworkConstants.LOCATIONS_ENDPOINT)
Call<LocationsResponse> getAllLocations(@Header("api-token") String token);

我正在使用 Gson 进行网络调用改造

您的字符串在句号之后有一个不可见的非打印字符。复制并粘贴到十六进制转储,您会看到:

00000000  2d 30 2e e2 80 8e 30 38  36 31 36 37 31 35 37 |-0....086167157|

好像是left-to-right mark

解决方法:在API中修正。客户端没有理由过滤输入中的垃圾数字。

Double.parseDouble("-0.086167157") 应该可以! 但是您的错误消息中的字符串有问题。似乎有一个隐藏的角色,重点在哪里。