Skip to content

Commit 6f3ea69

Browse files
author
zhaolizhi
committed
Retrofit Singleton
1 parent 03435da commit 6f3ea69

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

app/src/main/java/com/wingsofts/gankclient/App.kt

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.wingsofts.gankclient.di.component.ApiComponent
55
import com.wingsofts.gankclient.di.component.DaggerApiComponent
66
import com.wingsofts.gankclient.di.module.ApiModule
77
import com.wingsofts.gankclient.di.module.AppModule
8-
import retrofit2.Retrofit
98
import javax.inject.Inject
109

1110
/**

app/src/main/java/com/wingsofts/gankclient/di/component/ApiComponent.kt

+2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package com.wingsofts.gankclient.di.component
33
import com.wingsofts.gankclient.App
44
import com.wingsofts.gankclient.di.module.ApiModule
55
import dagger.Component
6+
import javax.inject.Singleton
67

78
/**
89
* Created by wing on 16-11-23.
910
*/
11+
@Singleton
1012
@Component(modules = arrayOf(ApiModule::class))
1113
interface ApiComponent{
1214

app/src/main/java/com/wingsofts/gankclient/di/module/ApiModule.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ import dagger.Module
99
import dagger.Provides
1010
import okhttp3.Cache
1111
import okhttp3.HttpUrl
12-
import okhttp3.Interceptor
1312
import okhttp3.OkHttpClient
1413
import okhttp3.logging.HttpLoggingInterceptor
1514
import retrofit2.Retrofit
1615
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
1716
import retrofit2.converter.gson.GsonConverterFactory
1817
import rx.schedulers.Schedulers
1918
import java.io.File
19+
import javax.inject.Singleton
2020

2121
/**
2222
* Created by wing on 16-11-23.
2323
*/
2424
@Module(includes = arrayOf(AppModule::class))
2525
class ApiModule {
26-
@Provides fun provideRetrofit(baseUrl: HttpUrl, client: OkHttpClient, gson: Gson) =
26+
@Provides @Singleton fun provideRetrofit(baseUrl: HttpUrl, client: OkHttpClient, gson: Gson) =
2727
Retrofit.Builder()
2828
.client(client)
2929
.baseUrl(baseUrl)
@@ -33,6 +33,7 @@ class ApiModule {
3333

3434
@Provides fun provideBaseUrl() = HttpUrl.parse("http://gank.io/api/")
3535
@Provides fun provideOkhttp(context: Context,interceptor: HttpLoggingInterceptor): OkHttpClient {
36+
Log.d("ddddd","new")
3637
val cacheSize = 1024 * 1024 * 10L
3738
val cacheDir = File(context.cacheDir, "http")
3839
val cache = Cache(cacheDir, cacheSize)

0 commit comments

Comments
 (0)