对于解析数据,我使用改造(gson)库。我在解析双值'位置'时遇到问题。您可以在下面看到完整的JSON。我解析字符串数组'单词'很棒,但是双值我在TextView中什么都没有。任何人都可以说出了什么是错误的,我需要解决什么以及我错过了什么?
感谢您的帮助!
JSON
MainActivity.java
w3wNetworkFactory.w3wGetInstance().w3wGetApiCall().getPosition(w3w_API_KEY, new Words("gliders.inquest.hardly"), new Callback<PositionResponse>() {
@Override
public void success(PositionResponse positionResponse, Response response) {
position_w3w.setText(Arrays.toString(positionResponse.getPosition()).replaceAll("\\[|\\]", ""));
//Log.d("w3w location: ", Arrays.toString(positionResponse.getPosition()));
String mLatLng = Arrays.toString(positionResponse.getPosition()).replaceAll("\\[|\\]", "");
//Convert String to double
double value = Double.parseDouble(mLatLng);
//Set marker to map
mGoogleMap.addMarker(new MarkerOptions().position(value)
.title("Geolocation system")
.snippet("Your last current location which was available!")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));
}
@Override
public void failure(RetrofitError error) {
error.toString();
}
});
PositionResponse.java
public class PositionResponse {
private double[] position;
public double[] getPosition() {
return position;
}
}
答案 0 :(得分:0)
尝试使用double
。 long
它是整数。在具有浮点的JSON数字中。
答案 1 :(得分:0)
要回答有关Google地图的问题,您需要将lat和lon分开,然后创建LatLng object。我认为它应该是这样的:
@Override
public void success(PositionResponse positionResponse, Response response) {
position_w3w.setText(Arrays.toString(positionResponse.getPosition()).replaceAll("\\[|\\]", ""));
//Log.d("w3w location: ", Arrays.toString(positionResponse.getPosition()));
String mLatLng = Arrays.toString(positionResponse.getPosition()).replaceAll("\\[|\\]", "");
String[] arrLatLng = mLatLng.split(","); //separate out lat and lon
LatLng latLon = new LatLng(Double.parseDouble(arrLatLng[0]), Double.parseDouble(arrLatLng[1])); //create the LatLng object
//Convert String to double
//double value = Double.parseDouble(mLatLng); this wouldn't work, you need to do both values individually
//Set marker to map, use LatLng object latLon
mGoogleMap.addMarker(new MarkerOptions().position(latLon)
.title("Geolocation system")
.snippet("Your last current location which was available!")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(latLon).zoom(12).build();
mGoogleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
有关完全设置Google Maps API的详细信息,请参阅我的其他答案:Cannot add map in Android Studio as stated in Google "Getting Started" page; Android Studio gives error