Skip to content

Commit

Permalink
modify api frame
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoruiyi committed Jul 11, 2016
1 parent 5571165 commit c5378e4
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 81 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ android {
}
buildTypes {
debug {
buildConfigField "Integer", "SPLASH_TIME", '1000'
debuggable true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
buildConfigField "Integer", "SPLASH_TIME", '1000'
debuggable false
minifyEnabled true
zipAlignEnabled true
Expand All @@ -27,10 +29,12 @@ android {
}
productFlavors {
dev {
buildConfigField "String", "BASE_URL_JOKE", '"http://apis.baidu.com/showapi_open_bus/"'
buildConfigField "String", "BASE_URL", '"http://apis.baidu.com/showapi_open_bus/"'

}
product {
buildConfigField "String", "BASE_URL_JOKE", '"http://apis.baidu.com/showapi_open_bus/"'
buildConfigField "String", "BASE_URL", '"http://apis.baidu.com/showapi_open_bus/"'
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/net/vsona/orz/AppModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ThreadExecutor provideThreadExecutor(JobExecutor jobExecutor) {
@Provides
@Singleton
ApiService providesApiService(RetrofitManager retrofitManager) {
return retrofitManager.getService();
return retrofitManager.getJokeService();
}
@Provides
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/net/vsona/orz/api/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

public class Api {

public static IApiService getService(){
return RetrofitClient.createApi(IApiService.class);
private static IJokeApiService sJokeService;

public static IJokeApiService getJokeService() {
if (sJokeService == null) {
sJokeService = RetrofitClient.createApi(IJokeApiService.class, RetrofitClient.getJokeRetrofit());
}
return sJokeService;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Created by roy on 16/6/28.
*/
public interface IApiService {
public interface IJokeApiService {
@Headers("apikey:83ec99fff780989a5376a1bc595ed5ff")
@GET("showapi_joke/joke_text")
Observable<JokeEntity> getJoke(@Query("page") int page);
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/net/vsona/orz/api/MyGson.java
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();
}
}
63 changes: 63 additions & 0 deletions app/src/main/java/net/vsona/orz/api/MyOkClient.java
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;
}

}
84 changes: 11 additions & 73 deletions app/src/main/java/net/vsona/orz/api/RetrofitClient.java
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);
}

}
2 changes: 1 addition & 1 deletion app/src/main/java/net/vsona/orz/ui/joke/JokePresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void load() {

public void loadList(final int page) {
System.out.println("JokePresenter.loadList");
Api.getService()
Api.getJokeService()
.getJoke(page)
.subscribeOn(Schedulers.io())
.map(new Func1<JokeEntity, List<ContentlistEntity>>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.os.Handler;

import net.vsona.orz.BuildConfig;

import javax.inject.Inject;


Expand All @@ -11,7 +13,7 @@
public class SplashPrensenter implements SplashContract.Presenter {

private final SplashContract.View mView;
private final long minTime = 1888l;
private final long minTime = BuildConfig.SPLASH_TIME;
private long startTime;
private long endTime;
//执行操作数
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/net/vsona/orz/utils/AppConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class AppConstants {
public static String APP_PLATFORM;
public static String APP_CHANNEL = "dev";

public static final String BASE_URL = BuildConfig.BASE_URL;




Expand Down

0 comments on commit c5378e4

Please sign in to comment.