Retrofit2 って便利だけど定義と実装を別ファイルに書く必要があって面倒
そこで、 BaseApi を作成して同一ファイルに書くようにした
Java ファイルを見るとわかるとおり、APIの定義をBaseApiで処理することで同一ファイルに定義された interface が利用できない制限をクリアしている
| package in.tanjo.retrofit.api; | |
| import retrofit2.Retrofit; | |
| class BaseApi<T> { | |
| T service; | |
| public BaseApi(Class<T> tClass) { | |
| this.service = RetrofitUtil.getInstance().create(tClass); | |
| } | |
| public T getService() { | |
| return service; | |
| } | |
| } |
| package in.tanjo.retrofit.api; | |
| import com.google.gson.FieldNamingPolicy; | |
| import com.google.gson.Gson; | |
| import com.google.gson.GsonBuilder; | |
| import com.google.gson.JsonSyntaxException; | |
| import com.google.gson.TypeAdapter; | |
| import com.google.gson.stream.JsonReader; | |
| import com.google.gson.stream.JsonWriter; | |
| import android.support.annotation.Nullable; | |
| import java.io.IOException; | |
| import java.lang.reflect.Type; | |
| import java.util.Date; | |
| public class GsonUtil { | |
| @Nullable | |
| public static <T> T fromJson(String json, Class<T> tClass) { | |
| return fromJson(json, (Type) tClass); | |
| } | |
| @Nullable | |
| public static <T> T fromJson(String json, Type type) { | |
| T t = null; | |
| try { | |
| t = get().fromJson(json, type); | |
| } catch (JsonSyntaxException ignored) { | |
| } | |
| return t; | |
| } | |
| public static Gson get() { | |
| return new GsonBuilder() | |
| .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) | |
| .registerTypeAdapter(Date.class, new UnixTimestampAdapter()) | |
| .create(); | |
| } | |
| @Nullable | |
| public static <T> String toJson(T t) { | |
| String json = null; | |
| try { | |
| json = get().toJson(t); | |
| } catch (JsonSyntaxException ignored) { | |
| } | |
| return json; | |
| } | |
| private static class UnixTimestampAdapter extends TypeAdapter<Date> { | |
| @Override | |
| public void write(JsonWriter out, Date value) throws IOException { | |
| if (value == null) { | |
| out.nullValue(); | |
| return; | |
| } | |
| out.value(value.getTime() / 1000); | |
| } | |
| @Override | |
| public Date read(JsonReader in) throws IOException { | |
| if (in == null) { | |
| return null; | |
| } | |
| return new Date(in.nextLong() * 1000); | |
| } | |
| } | |
| } |
| package in.tanjo.retrofit.api; | |
| import io.reactivex.Observable; | |
| import io.reactivex.internal.schedulers.IoScheduler; | |
| import retrofit2.http.GET; | |
| public class HogeApi extends BaseApi<HogeApi.Service> { | |
| public HogeApi() { | |
| super(Service.class); | |
| } | |
| public Observable<HogeResponse> search() { | |
| return getService().search().subscribeOn(new IoScheduler()); | |
| } | |
| interface Service { | |
| @GET("api/v1/hoge/search") | |
| Observable<JobsResponse> search(); | |
| } | |
| } |
| package in.tanjo.retrofit.api; | |
| import in.tanjo.retrofit.api.BuildConfig; | |
| import okhttp3.OkHttpClient; | |
| import retrofit2.Retrofit; | |
| import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; | |
| import retrofit2.converter.gson.GsonConverterFactory; | |
| public class RetrofitUtil { | |
| private static volatile Retrofit instance = null; | |
| public static Retrofit getInstance() { | |
| if (instance == null) { | |
| synchronized (Retrofit.class) { | |
| if (instance == null) { | |
| instance = new Retrofit.Builder() | |
| .addConverterFactory(GsonConverterFactory.create(GsonUtil.get())) | |
| .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | |
| .baseUrl(BuildConfig.BASE_URL) | |
| .client(new OkHttpClient.Builder().build()) | |
| .build(); | |
| } | |
| } | |
| } | |
| return instance; | |
| } | |
| } |