2016-11-12 80 views
0

我想从此API获取stopId,但我很难使用retrofit 2 + gson解析它。我只有使用较简单的JSON API的经验。任何人都可以帮我吗?如何在Android Studio中解析复杂的JSON与Retrofit 2

{ 
    "direction": "inbound", 
    "timetable": { 
     "$type": "Tfl.Api.Presentation.Entities.Timetable, Tfl.Api.Presentation.Entities", 
     "departureStopId": "940GZZCRECR", 
     "routes": [{ 
      "$type": "Tfl.Api.Presentation.Entities.TimetableRoute, Tfl.Api.Presentation.Entities", 
      "stationIntervals": [{ 
       "$type": "Tfl.Api.Presentation.Entities.StationInterval, Tfl.Api.Presentation.Entities", 
       "id": "0", 
       "intervals": [{ 
        "$type": "Tfl.Api.Presentation.Entities.Interval, Tfl.Api.Presentation.Entities", 
        "stopId": "940GZZCRLEB", 
        "timeToArrival": 2 
       }, { 
        "$type": "Tfl.Api.Presentation.Entities.Interval, Tfl.Api.Presentation.Entities", 
        "stopId": "940GZZCRSAN", 
        "timeToArrival": 3 
       }] 
      }, { 

      }, { 

      }], 
      "schedules": [ 

      ] 
     }] 
    } 
} 

回答

1

你必须建立适当的模型层次,例如:

BaseModel

public class BaseModel { 
    String direction; 
    Timetable timetable; 
} 

时间表

public class Timetable { 
    String $type; 
    String departureStopId; 
    List<Route> routes; 
} 

路线

public class Route { 
    String $type; 
    List<StationInterval> stationIntervals; 
    List<Integer> schedules; 
} 

StationInterval

public class StationInterval { 
    String $type; 
    int id; 
    List<Interval> intervals; 
} 

间隔

public class Interval { 
    String $type; 
    String stopId; 
    int timeToArrival; 
} 

而且使改造呼叫像往常一样:

@GET("some_url") 
Call<BaseModel> loadSomeData(); 
+0

我想过这个,但我想可能有更好的方法来做到这一点。但是,这似乎很好!当我实施它时,我会给你一个机会,它会起作用。 –

+0

另外,你可以创建自定义的'TypeAdapter',并实现自己的解析你的json的机制,但我不建议你这样做。 –

+0

我不断收到这个错误'预计BEGIN_ARRAY,但是在BEGIN_OBJECT行1列2'。然而,我正在使用的API [链接](https://api.tfl.gov.uk/Line/tram/Timetable/940GZZCRECR?direction=inbound&app_id=&app_key=) –

1

使用此工具自动创建模型。只需粘贴一个示例json响应。 http://pojo.sodhanalibrary.com

请记住检查和编辑变量的类型,有时它们可​​以为null。之后,照常打电话。

1

从JSON生成POJO的简单而有效的方法是http://www.jsonschema2pojo.org/ 将上述链接生成的模型包含在内后,如果需要设置Retrofit 2.0的某些信息,则可以继续阅读此内容。

现在,你必须定义一个接口API的

public interface MyAPI { 
    @GET("/url") 
    Call<ResponseModel> getData(); 
} 

然后创建一个类来获得改造客户

public class MyDataClient { 

    public static final String BASE_URL = ""; 
    private static Retrofit retrofit = null; 


    public static Retrofit getClient() { 
     if (retrofit==null) { 
      HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); 
      logging.setLevel(HttpLoggingInterceptor.Level.BODY); 
      OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
      httpClient.addInterceptor(logging); 

      retrofit = new Retrofit.Builder() 
        .baseUrl(BASE_URL) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .client(httpClient.build()) 
        .build(); 
     } 
     return retrofit; 
    } 
} 

然后,当你需要调用API做到这一点,

 MyAPI apiService =MyDataClient.getClient().create(MyAPI.class); 
    Call<ResponseModel> call = apiService.getData(); 
    call.enqueue(new Callback<ResponseModel>() { 
       @Override 
       public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) { 
       } 
       @Override 
       public void onFailure(Call<ResponseModel> call, Throwable t){ 
       } 
     });