Last active
December 29, 2017 02:30
-
-
Save gibson-khs/08a51277286483dcae9aea2377bf3f9f to your computer and use it in GitHub Desktop.
kotlin_retrofit
This file contains hidden or 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
| class Api { | |
| companion object { | |
| private val BASE_URL = "http://domain/" | |
| private var apiService: ApiInterface? = null | |
| fun getService(): ApiInterface { | |
| if (apiService == null) { | |
| apiService = setApiService() | |
| } | |
| return apiService!! | |
| } | |
| private fun setApiService(): ApiInterface { | |
| val builder = OkHttpClient.Builder() | |
| val httpLoggingInterceptor = HttpLoggingInterceptor() | |
| httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY | |
| builder.interceptors().add(httpLoggingInterceptor) | |
| val okHttpClient = builder.build() | |
| val retrofit = Retrofit.Builder() | |
| .baseUrl(BASE_URL) | |
| .addConverterFactory(GsonConverterFactory.create()) | |
| .client(okHttpClient) | |
| .build() | |
| return retrofit.create<ApiInterface>(ApiInterface::class.java) | |
| } | |
| } | |
| } |
This file contains hidden or 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
| interface ApiInterface { | |
| @GET("users") | |
| fun getUsers(): Call<MutableList<User>> | |
| } |
This file contains hidden or 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
| class Network { | |
| companion object { | |
| fun deafultError(t: Throwable) { | |
| t.printStackTrace() | |
| //TODO | |
| } | |
| fun <T> request(call: Call<T>, callback: NetworkCallback<T>) { | |
| request(call, callback.success!!, callback.error) | |
| } | |
| private fun <T> request(call: Call<T>, onSuccess: (T) -> Unit, onError: ((Throwable) -> Unit)?) { | |
| call.enqueue(object : Callback<T> { | |
| override fun onResponse(call: Call<T>?, response: Response<T>?) { | |
| if (onSuccess != null) { | |
| onSuccess(response?.body()!!) | |
| } | |
| } | |
| override fun onFailure(call: Call<T>, t: Throwable) { | |
| if (onError == null) { | |
| deafultError(t) | |
| } else { | |
| onError(t) | |
| } | |
| } | |
| }) | |
| } | |
| } | |
| } |
This file contains hidden or 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
| class NetworkCallback<T> { | |
| var success: ((T) -> Unit)? = null | |
| var error: ((Throwable) -> Unit)? = null | |
| } |
This file contains hidden or 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
| //use sample | |
| Network.request(Api.getService().getUsers(), | |
| NetworkCallback<MutableList<User>>().apply { | |
| success = { | |
| // it. (MutableList<User>) | |
| } | |
| }) | |
| // error handling | |
| Network.request(Api.getService().getUsers(), | |
| NetworkCallback<MutableList<User>>().apply { | |
| success = { | |
| // it. (MutableList<User>) | |
| } | |
| fail = { | |
| // it. (Throwable) | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment