JsonObject (com.google.gson) , JSONObject (org.json)
FlowerApi
@GET("/flower")
Call<JsonObject> getFlowers();
FlowerApi api = ApiFactory.createRetrofitService(FlowerApi.class);
Call<JsonObject> call = api.getFlowers();
retrofit.Response response = call.execute();
if (response.isSuccess()) {
JsonObject object = (JsonObject) response.body();
}
Api Factory .
public class ApiFactory {
private static final int TIMEOUT = 60;
private static final int WRITE_TIMEOUT = 120;
private static final int CONNECT_TIMEOUT = 10;
private static final OkHttpClient CLIENT = new OkHttpClient();
private static final String BASE_URL = "flowers.com";
static {
CLIENT.setConnectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS);
CLIENT.setWriteTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS);
CLIENT.setReadTimeout(TIMEOUT, TimeUnit.SECONDS);
}
@NonNull
public static <S> S createRetrofitService(Class<S> serviceClass) {
return getRetrofit().create(serviceClass);
}
@NonNull
private static Retrofit getRetrofit() {
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(CLIENT)
.build();
}
}
build.gradle
compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
source
share