-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gaoruiyi
committed
Jul 11, 2016
1 parent
5571165
commit c5378e4
Showing
10 changed files
with
104 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package net.vsona.orz.api; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
||
/** | ||
* Created by roy on 16/7/11. | ||
*/ | ||
public class MyGson { | ||
public static Gson get() { | ||
return new GsonBuilder().create(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package net.vsona.orz.api; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import net.vsona.orz.utils.AppConstants; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.logging.HttpLoggingInterceptor; | ||
|
||
/** | ||
* Created by roy on 16/7/11. | ||
*/ | ||
public class MyOkClient { | ||
static OkHttpClient client; | ||
|
||
@NonNull | ||
public static OkHttpClient getOkHttpClient() { | ||
if (client != null) { | ||
return client; | ||
} | ||
|
||
OkHttpClient.Builder builder = new OkHttpClient.Builder(); | ||
|
||
if (AppConstants.debug) { | ||
// Log信息拦截器 | ||
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); | ||
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); | ||
//设置 Debug Log 模式 | ||
builder.addInterceptor(loggingInterceptor); | ||
} | ||
|
||
//设置cookie | ||
// CookieManager cookieManager = new CookieManager(); | ||
// cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); | ||
// builder.cookieJar(new JavaNetCookieJar(cookieManager)); | ||
// | ||
//公共参数 | ||
builder.addInterceptor(new QueryParameterInterceptor()); | ||
//Gzip压缩 | ||
// builder.addInterceptor(new GzipRequestInterceptor()); | ||
|
||
//开启缓存 | ||
// File cacheFile = new File(RockContextUtils.getApplicationContext().getExternalCacheDir(), "appCache"); | ||
// Cache cache = new Cache(cacheFile, 1024 * 1024 * 30); | ||
// builder.cache(cache).addInterceptor(new CacheInterceptor()); | ||
|
||
//开启facebook debug | ||
// builder.addNetworkInterceptor(new StethoInterceptor()); | ||
|
||
//设置超时 | ||
builder.connectTimeout(15, TimeUnit.SECONDS); | ||
builder.readTimeout(20, TimeUnit.SECONDS); | ||
builder.writeTimeout(20, TimeUnit.SECONDS); | ||
//错误重连 | ||
builder.retryOnConnectionFailure(true); | ||
|
||
client = builder.build(); | ||
return client; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,31 @@ | ||
package net.vsona.orz.api; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
||
import net.vsona.orz.utils.AppConstants; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import net.vsona.orz.BuildConfig; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.logging.HttpLoggingInterceptor; | ||
import retrofit2.Retrofit; | ||
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; | ||
import retrofit2.converter.gson.GsonConverterFactory; | ||
|
||
public class RetrofitClient { | ||
private static RetrofitClient sRetrofitClient; | ||
private Retrofit sRetrofit; | ||
static OkHttpClient client; | ||
|
||
private RetrofitClient() { | ||
} | ||
|
||
private static RetrofitClient getRetrofitClient() { | ||
if (sRetrofitClient == null) { | ||
sRetrofitClient = new RetrofitClient(); | ||
|
||
//build okHttpClient | ||
OkHttpClient client = getOkHttpClient(); | ||
|
||
Gson gson = new GsonBuilder().create(); | ||
|
||
sRetrofitClient.sRetrofit = new Retrofit.Builder().baseUrl(AppConstants.BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create(gson)) | ||
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) | ||
.client(client) | ||
.build(); | ||
} | ||
return sRetrofitClient; | ||
} | ||
|
||
@NonNull | ||
public static OkHttpClient getOkHttpClient() { | ||
if (client != null) { | ||
return client; | ||
} | ||
|
||
OkHttpClient.Builder builder = new OkHttpClient.Builder(); | ||
|
||
if (AppConstants.debug) { | ||
// Log信息拦截器 | ||
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); | ||
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); | ||
//设置 Debug Log 模式 | ||
builder.addInterceptor(loggingInterceptor); | ||
} | ||
|
||
//设置cookie | ||
// CookieManager cookieManager = new CookieManager(); | ||
// cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); | ||
// builder.cookieJar(new JavaNetCookieJar(cookieManager)); | ||
// | ||
//公共参数 | ||
builder.addInterceptor(new QueryParameterInterceptor()); | ||
//Gzip压缩 | ||
// builder.addInterceptor(new GzipRequestInterceptor()); | ||
|
||
//开启缓存 | ||
// File cacheFile = new File(RockContextUtils.getApplicationContext().getExternalCacheDir(), "appCache"); | ||
// Cache cache = new Cache(cacheFile, 1024 * 1024 * 30); | ||
// builder.cache(cache).addInterceptor(new CacheInterceptor()); | ||
|
||
//开启facebook debug | ||
// builder.addNetworkInterceptor(new StethoInterceptor()); | ||
public static Retrofit getJokeRetrofit() { | ||
|
||
//设置超时 | ||
builder.connectTimeout(15, TimeUnit.SECONDS); | ||
builder.readTimeout(20, TimeUnit.SECONDS); | ||
builder.writeTimeout(20, TimeUnit.SECONDS); | ||
//错误重连 | ||
builder.retryOnConnectionFailure(true); | ||
Gson gson = MyGson.get(); | ||
OkHttpClient client = MyOkClient.getOkHttpClient(); | ||
|
||
client = builder.build(); | ||
return client; | ||
return new Retrofit.Builder().baseUrl(BuildConfig.BASE_URL_JOKE) | ||
.addConverterFactory(GsonConverterFactory.create(gson)) | ||
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) | ||
.client(client) | ||
.build(); | ||
} | ||
|
||
public static <T> T createApi(Class<T> clazz) { | ||
return getRetrofitClient().sRetrofit.create(clazz); | ||
public static <T> T createApi(Class<T> clazz, Retrofit retrofit) { | ||
return retrofit.create(clazz); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters