有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java解析双值并在TextView中设置

对于解析数据,我使用改型(gson)库。我在解析双值“位置”时遇到问题。您可以在下面看到完整的JSON。我解析字符串数组“words”很好,但在TextView中没有双值。谁能说出到底哪里出了问题,我需要修复什么,我错过了什么

谢谢你的帮助

JSON

enter image description here

主要活动。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();
            }
        });

位置响应。java

public class PositionResponse {
    private double[] position;

    public double[] getPosition() {
        return position;
    }
}

共 (2) 个答案

  1. # 1 楼答案

    尝试使用doublelong这是整数。在JSON数字中使用浮点数

  2. # 2 楼答案

    要回答关于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