Java tutorial
/* * Copyright (C) 2015 Drakeet <drakeet.me@gmail.com> * * This file is part of Meizhi * * Meizhi is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Meizhi is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Meizhi. If not, see <http://www.gnu.org/licenses/>. */ package com.hexdo.hexexamples.network; import com.squareup.okhttp.Interceptor; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.logging.HttpLoggingInterceptor; import retrofit.GsonConverterFactory; import retrofit.Retrofit; import retrofit.RxJavaCallAdapterFactory; import java.io.IOException; import java.util.List; /** * 2015-08-07T03:57:47.229Z * Created by drakeet on 8/9/15. */ public class GOERetrofit { public static final String API_BASE_URL = "http://goewww.atb2c.com:9900/"; private static OkHttpClient httpClient = new OkHttpClient(); private static Retrofit.Builder builder = new Retrofit.Builder().baseUrl(API_BASE_URL) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()); public static <S> S createService(Class<S> serviceClass) { return createService(serviceClass, null); } public static <S> S createService(Class<S> serviceClass, final String authToken) { List<Interceptor> interceptors = httpClient.interceptors(); interceptors.clear(); if (authToken != null) { interceptors.add(new Interceptor() { @Override public com.squareup.okhttp.Response intercept(Chain chain) throws IOException { Request original = chain.request(); // Request customization: add request headers Request.Builder requestBuilder = original.newBuilder().header("Authorization", authToken) .method(original.method(), original.body()); Request request = requestBuilder.build(); return chain.proceed(request); } }); } HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); // set your desired log level logging.setLevel(HttpLoggingInterceptor.Level.BODY); interceptors.add(logging); Retrofit retrofit = builder.client(httpClient).build(); return retrofit.create(serviceClass); } }