-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#17] 통신을 위한 네트워크 관련 베이스 코드 작성 - 컨버터 추가
- Loading branch information
Showing
4 changed files
with
94 additions
and
5 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
88 changes: 88 additions & 0 deletions
88
app/src/main/java/com/moyerun/moyeorun_android/network/MoyeorunJsonConverterFactory.kt
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,88 @@ | ||
package com.moyerun.moyeorun_android.network | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.TypeAdapter | ||
import com.google.gson.reflect.TypeToken | ||
import com.moyerun.moyeorun_android.network.api.Success | ||
import okhttp3.MediaType | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.RequestBody | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import okhttp3.ResponseBody | ||
import okio.Buffer | ||
import retrofit2.Converter | ||
import retrofit2.Retrofit | ||
import java.io.IOException | ||
import java.io.OutputStreamWriter | ||
import java.lang.reflect.Type | ||
import java.nio.charset.Charset | ||
|
||
class MoyeorunJsonConverterFactory private constructor( | ||
private val gson: Gson | ||
) : Converter.Factory() { | ||
|
||
override fun responseBodyConverter( | ||
type: Type, | ||
annotations: Array<out Annotation>, | ||
retrofit: Retrofit | ||
): Converter<ResponseBody, *> { | ||
val adapter = gson.getAdapter(TypeToken.get(type)) | ||
return MoyeorunResponseBodyConverter(gson, adapter) | ||
} | ||
|
||
override fun requestBodyConverter( | ||
type: Type, | ||
parameterAnnotations: Array<out Annotation>, | ||
methodAnnotations: Array<out Annotation>, | ||
retrofit: Retrofit | ||
): Converter<*, RequestBody> { | ||
val adapter = gson.getAdapter(TypeToken.get(type)) | ||
return MoyeorunRequestBodyConverter(gson, adapter) | ||
} | ||
|
||
companion object { | ||
fun create(): MoyeorunJsonConverterFactory { | ||
return MoyeorunJsonConverterFactory(Gson()) | ||
} | ||
} | ||
|
||
class MoyeorunResponseBodyConverter<T>( | ||
private val gson: Gson, | ||
private val adapter: TypeAdapter<T> | ||
) : Converter<ResponseBody, T> { | ||
|
||
override fun convert(value: ResponseBody): T? { | ||
val jsonReader = gson.newJsonReader(value.charStream()) | ||
try { | ||
val result: Success<T> = gson.fromJson(jsonReader, Success::class.java) | ||
val data = gson.toJson(result.data) | ||
return adapter.fromJson(data) | ||
} catch (e: Exception) { | ||
throw IOException(e) | ||
} finally { | ||
value.close() | ||
} | ||
} | ||
} | ||
|
||
class MoyeorunRequestBodyConverter<T>( | ||
private val gson: Gson, | ||
private val adapter: TypeAdapter<T> | ||
): Converter<T, RequestBody> { | ||
|
||
@Throws(IOException::class) | ||
override fun convert(value: T): RequestBody { | ||
val buffer = Buffer() | ||
val writer = OutputStreamWriter(buffer.outputStream(), UTF_8) | ||
val jsonWriter = gson.newJsonWriter(writer) | ||
adapter.write(jsonWriter, value) | ||
jsonWriter.close() | ||
return buffer.readByteString().toRequestBody(MEDIA_TYPE) | ||
} | ||
|
||
companion object { | ||
private val MEDIA_TYPE: MediaType = "application/json; charset=UTF-8".toMediaType() | ||
private val UTF_8 = Charset.forName("UTF-8") | ||
} | ||
} | ||
} |
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
6 changes: 3 additions & 3 deletions
6
app/src/main/java/com/moyerun/moyeorun_android/network/client/Retrofit.kt
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,17 +1,17 @@ | ||
package com.moyerun.moyeorun_android.network.client | ||
|
||
import com.moyerun.moyeorun_android.BuildConfig | ||
import com.moyerun.moyeorun_android.network.calladapter.ApiResultCallAdapterFactory | ||
import com.moyerun.moyeorun_android.network.MoyeorunJsonConverterFactory | ||
import com.moyerun.moyeorun_android.network.api.ApiService | ||
import com.moyerun.moyeorun_android.network.calladapter.ApiResultCallAdapterFactory | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
|
||
private const val BASE_URL = BuildConfig.BASE_URL | ||
|
||
val retrofit: Retrofit = Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.addCallAdapterFactory(ApiResultCallAdapterFactory()) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.addConverterFactory(MoyeorunJsonConverterFactory.create()) | ||
.build() | ||
|
||
val apiService: ApiService = retrofit.create(ApiService::class.java) |