Use jsonschema2pojo.org Paste you responce and it will generate Java classes for you.
You instantiate Retrofit instance by setting all necessary components like this:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
and your interfaces will look like:
@GET("api/service/schedule/{filial}")
Observable<Response<List<Event>>> getSchedule(@Path("filial") String filial);
where @GET- annotation defining request type,
Response<> - type used by Retrofit, carrying information whether response is successful or not (see class methods).
Observable<> - type from rxJava library, allowing to process request in reactive way.
I strongly recommend to use RxJava, because it simplifies execution in background very much. (DON't use AsyncTasks!!).
in order to use Retrofit2 add following lines to your Gradle file:
//Reactive programming
compile 'io.reactivex:rxjava:1.1.5'
compile 'io.reactivex:rxandroid:1.2.0'
compile 'com.google.code.gson:gson:2.4'
//retrofit and okhttp
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
You will instantiate Retrofit2 interface by a similar call:
retrofit.create(Api.class)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…