繁体   English   中英

使内部静态类成为顶级

[英]Making inner static class top level

我实现了谷歌的方向解析结果成Java模型类。

HttpRequest request = requestFactory.buildGetRequest(url);
HttpResponse httpResponse = request.execute();
DirectionsResult directionsResult=httpResponse.parseAs(DirectionsResult.class);

...

 public static class DirectionsResult {
    @Key
    public List<Route> routes;
}

public static class Route {
    @Key
    public List<Leg> legs;
}

public static class Leg {
    @Key
    public Distance distance;

    @Key
    public Duration duration;

}

public static class Distance
{
    @Key
    public String text;
}

public static class Duration
{
    @Key
    public String text;
}


  ...

要访问数据,我可以使用:

  Log.d(TAG,"directions result distance text: "+directionsResult.routes.get(0).legs.get(0).distance.text);

我的问题是如何创建单独的顶层类,命名为DirectionsResult并访问其成员变量?

我试图模仿顶级的静态类行为,但没有成功,却出错了……

您创建一个新的Java文件DirectionResults.java。 您完全照原样复制类,从声明该类的行中删除关键字static。 添加正常的包装线和任何需要的进口。 这就是您需要做的所有事情。

 public class DirectionsResult {
    @Key
    public List<Route> routes;
 }

@Gabe Sechan:我已经尝试过了,并且收到以下错误消息:

03-12 20:04:14.159 12525-12917/com.rootcave.roadpal W/System.err: java.lang.IllegalArgumentException: key value
03-12 20:04:14.159 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:880)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parse(JsonParser.java:381)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parse(JsonParser.java:354)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:87)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:459)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at com.rootcave.roadpal.ride.createRide.CreateRideInteractorImpl$DirectionsFetcher.doInBackground(CreateRideInteractorImpl.java:231)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at com.rootcave.roadpal.ride.createRide.CreateRideInteractorImpl$DirectionsFetcher.doInBackground(CreateRideInteractorImpl.java:189)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at java.lang.Thread.run(Thread.java:856)
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err: Caused by: java.lang.IllegalArgumentException: key value, field public java.util.List com.rootcave.roadpal.ride.createRide.DirectionsResultModel.routes
03-12 20:04:14.163 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:880)
03-12 20:04:14.167 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parse(JsonParser.java:471)
03-12 20:04:14.167 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:780)
03-12 20:04:14.167 12525-12917/com.rootcave.roadpal W/System.err:   ... 14 more
03-12 20:04:14.167 12525-12917/com.rootcave.roadpal W/System.err: Caused by: java.lang.IllegalArgumentException: key value, field public java.util.List com.rootcave.roadpal.ride.createRide.DirectionsResultModel.routes
03-12 20:04:14.171 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:880)
03-12 20:04:14.171 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseArray(JsonParser.java:647)
03-12 20:04:14.171 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:739)
03-12 20:04:14.171 12525-12917/com.rootcave.roadpal W/System.err:   ... 16 more
03-12 20:04:14.171 12525-12917/com.rootcave.roadpal W/System.err: Caused by: java.lang.IllegalArgumentException: key value, field public java.util.List com.rootcave.roadpal.ride.createRide.DirectionsResultModel$Route.legs
03-12 20:04:14.175 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:880)
03-12 20:04:14.175 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parse(JsonParser.java:471)
03-12 20:04:14.175 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:780)
03-12 20:04:14.175 12525-12917/com.rootcave.roadpal W/System.err:   ... 18 more
03-12 20:04:14.175 12525-12917/com.rootcave.roadpal W/System.err: Caused by: java.lang.IllegalArgumentException: key value, field public java.util.List com.rootcave.roadpal.ride.createRide.DirectionsResultModel$Route.legs
03-12 20:04:14.175 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:880)
03-12 20:04:14.175 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseArray(JsonParser.java:647)
03-12 20:04:14.175 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:739)
03-12 20:04:14.175 12525-12917/com.rootcave.roadpal W/System.err:   ... 20 more
03-12 20:04:14.175 12525-12917/com.rootcave.roadpal W/System.err: Caused by: java.lang.IllegalArgumentException: key value, field public com.rootcave.roadpal.ride.createRide.DirectionsResultModel$Distance com.rootcave.roadpal.ride.createRide.DirectionsResultModel$Leg.distance
03-12 20:04:14.179 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:880)
03-12 20:04:14.179 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parse(JsonParser.java:471)
03-12 20:04:14.179 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:780)
03-12 20:04:14.179 12525-12917/com.rootcave.roadpal W/System.err:   ... 22 more
03-12 20:04:14.179 12525-12917/com.rootcave.roadpal W/System.err: Caused by: java.lang.IllegalArgumentException: key value, field public java.lang.String com.rootcave.roadpal.ride.createRide.DirectionsResultModel$Distance.value
03-12 20:04:14.179 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:880)
03-12 20:04:14.179 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parse(JsonParser.java:471)
03-12 20:04:14.179 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:780)
03-12 20:04:14.179 12525-12917/com.rootcave.roadpal W/System.err:   ... 24 more
03-12 20:04:14.179 12525-12917/com.rootcave.roadpal W/System.err: Caused by: java.lang.IllegalArgumentException: expected numeric type but got class java.lang.String
03-12 20:04:14.179 12525-12917/com.rootcave.roadpal W/System.err:     at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:843)
03-12 20:04:14.183 12525-12917/com.rootcave.roadpal W/System.err:   ... 26 more

错误发生在这一行:

DirectionsResultModel directionsResultModel = httpResponse.parseAs(DirectionsResultModel.class);

这是json响应,我正在尝试解析,我对旅行距离和旅行持续时间字段感兴趣:

http response parse as string: {
                                                                 "geocoded_waypoints" : [
                                                                    {
                                                                       "geocoder_status" : "OK",
                                                                       "place_id" : "ChIJhQEXk8ZXY0cRUnc0gzfpLX0",
                                                                       "types" : [ "locality", "political" ]
                                                                    },
                                                                    {
                                                                       "geocoder_status" : "OK",
                                                                       "place_id" : "ChIJj7jA0mL6YUcRhAf0Exw-MhI",
                                                                       "types" : [ "locality", "political" ]
                                                                    }
                                                                 ],
                                                                 "routes" : [
                                                                    {
                                                                       "bounds" : {
                                                                          "northeast" : {
                                                                             "lat" : 45.3581754,
                                                                             "lng" : 15.543227
                                                                          },
                                                                          "southwest" : {
                                                                             "lat" : 44.118924,
                                                                             "lng" : 14.2724169
                                                                          }
                                                                       },
                                                                       "copyrights" : "Map data ©2016 Google",
                                                                       "legs" : [
                                                                          {
                                                                             "distance" : {
                                                                                "text" : "243 km",
                                                                                "value" : 243413
                                                                             },
                                                                             "duration" : {
                                                                                "text" : "4 hours 2 mins",
                                                                                "value" : 14529
                                                                             },
                                                                             "end_address" : "Zadar, Croatia",
                                                                             "end_location" : {
                                                                                "lat" : 44.117578,
                                                                                "lng" : 15.219711
                                                                             },
                                                                             "start_address" : "Lovran, Croatia",
                                                                             "start_location" : {
                                                                                "lat" : 45.2969573,
                                                                                "lng" : 14.2724169
                                                                             },
                                                                             "steps" : [
                                                                                {
                                                                                   "distance" : {
                                                                                      "text" : "0.1 km",
                                                                                      "value" : 136
                                                                                   },
                                                                                   "duration" : {
                                                                                      "text" : "1 min",
                                                                                      "value" : 28
                                                                                   },
                                                                                   "end_location" : {
                                                                                      "lat" : 45.2961227,
                                                                                      "lng" : 14.2724438
                                                                                   },
                                                                                   "html_instructions" : "Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eLokva ul.\u003c/b\u003e",
                                                                                   "polyline" : {
                                                                                      "points" : "_ansGsqbvADGb@i@P[DYBE?AHGH?LBHDXp@Zv@"
                                                                                   },
                                                                                   "start_location" : {
                                                                                      "lat" : 45.2969573,
                                                                                      "lng" : 14.2724169
                                                                                   },
                                                                                   "travel_mode" : "DRIVING"
                                                                                },
                                                                                {
                                                                                   "distance" : {
                                                                                      "text" : "66 m",
                                                                                      "value" : 66
                                                                                   },
                                                                                   "duration" : {
                                                                                      "text" : "1 min",
                                                                                      "value" : 18
                                                                                   },
                                                                                   "end_location" : {
                                                                                      "lat" : 45.2956534,
                                                                                      "lng" : 14.2729462
                                                                                   },
                                                                                   "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e toward \u003cb\u003eUl. dr. Nilo cara\u003c/b\u003e",
                                                                                   "maneuver" : "turn-left",
                                                                                   "polyline" : {
                                                                                      "points" : "w{msGwqbvA@CTYXc@RQVQ"
                                                                                   },
                                                                                   "start_location" : {
                                                                                      "lat" : 45.2961227,
                                                                                      "lng" : 14.2724438
                                                                                   },
                                                                                   "travel_mode" : "DRIVING"
                                                                                },
                                                                                {
                                                                                   "distance" : {
                                                                                      "text" : "0.7 km",
                                                                                      "value" : 671
                                                                                   },
                                                                                   "duration" : {
                                                                                      "text" : "2 mins",
                                                                                      "value" : 144
                                                                                   },
                                                                                   "end_location" : {
                                                                                      "lat" : 45.2968208,
                                                                                      "lng" : 14.2758713
                                                                                   },
                                                                                   "html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eUl. dr. Nilo cara\u003c/b\u003e",
                                                                                   "maneuver" : "turn-left",
                                                                                   "polyline" : {
                                                                                      "points" : "yxmsG}tbvAE]AO@[BW?SAYCQCIIOOOOEaAGUCsACi@EUEKIGKCKAO?OBSHMHKPKp@MdAOdB]|A[LKBK@E?KAIQ

编辑:即时通讯使用Android Studio

编辑:这确实是一个简单的错误,这是该类的最终外观:

public class DirectionsResultModel {
@Key("routes")
private List<Route> routes;

public List<Route> getRoutes() {
    return routes;
}

public static class Route {
    @Key("legs")
    private List<Leg> legs;

    public List<Leg> getLegs() {return legs;}
}

public static class Leg {
    @Key("distance")
    private Distance distance;

    @Key("duration")
    private Duration duration;

    public Distance getDistance() {
        return distance;
    }
    public Duration getDuration() {
        return duration;
    }

}

public static class Distance {
    @Key
    private String text;
    @Key
    private int value;

    public int getValue() {return value;}
    public String getText() {return text;}
}

public static class Duration {
    @Key
    private String text;
    @Key
    private int value;

    public String getText() {return text;}
    public int getValue() {return value;}

}
}

谢谢你们!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM