Last active
April 29, 2020 09:19
-
-
Save yoobi/ee5c152e13944b32411d8e1f81d973ab to your computer and use it in GitHub Desktop.
TLS 1.2 for Retrofit, Glide and Exoplayer // https://stackoverflow.com/q/61459509
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
/* | |
You'll find the extension `enableTls12()`: | |
- [Gist](https://gist.github.com/ankushg/8c0c3144318b1c17abb228d6211ba996) | |
- [The related article](https://ankushg.com/posts/tls-1.2-on-android/) | |
*/ | |
val clientBuilder by lazy { | |
OkHttpClient.Builder() | |
.readTimeout(15, TimeUnit.SECONDS) | |
.connectTimeout(15, TimeUnit.SECONDS) | |
.enableTls12() | |
} |
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
/* Important: | |
You'll need to add these implentation in your gradle | |
implementation 'com.github.bumptech.glide:annotations:4.11.0' | |
implementation('com.github.bumptech.glide:okhttp3-integration:4.11.0'){ | |
exclude group: 'glide-parent' | |
} | |
*/ | |
@GlideModule | |
class CustomGlideModule : AppGlideModule() { | |
override fun registerComponents(context: Context, glide: Glide, registry: Registry) { | |
registry.replace( | |
GlideUrl::class.java, | |
InputStream::class.java, | |
OkHttpUrlLoader.Factory(clientBuilder.build()) | |
) | |
} | |
} | |
// How to use your customGlideModule | |
GlideApp.with(this) | |
.load(URL) | |
.into(image) |
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
/* Important: | |
You'll need to add this implentation in your gradle | |
implementation "com.google.android.exoplayer:extension-okhttp:2.11.4" | |
*/ | |
val dataSourceFactory: DataSource.Factory = OkHttpDataSourceFactory( | |
clientBuilder.build(), | |
Util.getUserAgent(context, "RecyclerView VideoPlayer") | |
) | |
// then just use your dataSourceFactory with your mediaSource and play the video | |
val videoSource: MediaSource = ProgressiveMediaSource.Factory(dataSourceFactory) | |
.createMediaSource(Uri.parse(mediaUrl)) |
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
/* just use the clientBuilder we created before*/ | |
val retrofit = Retrofit.Builder() | |
.addConverterFactory(MoshiConverterFactory.create(moshi)) | |
.addCallAdapterFactory(CoroutineCallAdapterFactory()) | |
.client(clientBuilder.build()) | |
.baseUrl(BASE_URL) | |
.build() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment