2016-11-12 78 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){ 
       } 
     });