使内部静态类达到顶级

时间:2016-03-12 16:53:58

标签: java android static-members

实现将谷歌的方向解析为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的单独顶级类并访问其成员变量?

我尝试模仿顶级类的静态类行为但没有成功,却出现错误......

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

@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;}

}
}

谢谢大家!