Java 使内部静态类成为顶级

Java 使内部静态类成为顶级,java,android,static-members,Java,Android,Static Members,我正在将google的方向结果解析为Java模型类 HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse httpResponse = request.execute(); DirectionsResult directionsResult=httpResponse.parseAs(DirectionsResult.class); ... public static class DirectionsR

我正在将google的方向结果解析为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;
}


  ...
我的问题是如何创建名为DirectionsResult的独立顶级类并访问其成员变量


我试图模仿top类的静态类行为,但没有成功,出现了错误…

您创建了一个新的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;}

}
}
公共类方向ResultModel{
@关键(“路线”)
私人名单路线;
公共列表getRoutes(){
返回路线;
}
公共静态类路由{
@钥匙(“腿”)
私人名单;
public List getLegs(){return legs;}
}
公共静态类腿{
@键(“距离”)
私人距离;
@关键(“持续时间”)
私人期限;
公共距离getDistance(){
返回距离;
}
公共持续时间getDuration(){
返回时间;
}
}
公共静态类距离{
@钥匙
私有字符串文本;
@钥匙
私有int值;
public int getValue(){return value;}
公共字符串getText(){return text;}
}
公共静态课程时长{
@钥匙
私有字符串文本;
@钥匙
私有int值;
公共字符串getText(){return text;}
public int getValue(){return value;}
}
}

谢谢大家!

或者只需单击类名并按F6键移动。假设他使用与您相同的IDE。而且他使用的版本也是同一个热键,假设他使用的是谷歌支持的唯一IDE,并且没有以某种方式重新映射这些键。可以肯定的是,它们在所有平台上都是一样的。当eclipse是唯一受支持的IDE时,我认识很多使用IntelliJ的开发人员。现在我知道很多人在使用Eclipse和VisualStudio。这是一个错误的假设。我没有检查JSON,但它说其中一个字段在Java模型中定义为数字,在JSON中是一个非数字字符串。是的,我在Distance类和Duration类中添加了字符串字段值,它们应该是int,因为发生了错误。这是一个非常简单的错误,我应该更仔细地检查错误日志。谢谢大家
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;}

}
}