Skip to content

Commit

Permalink
Added API error response exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gomsang committed Mar 10, 2020
1 parent 174c7fd commit f5be7ad
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.gomsang.lab.nearmask"
minSdkVersion 22
targetSdkVersion 29
versionCode 4
versionName "1.3"
versionCode 5
versionName "1.4"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
package com.gomsang.lab.publicmask.libs.http.mask

import com.gomsang.lab.publicmask.libs.constants.WebAddresses
import com.gomsang.lab.publicmask.libs.http.kakao.KakaoAPI
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.IOException


class Mask {
val api: MaskAPI

constructor() {

class StringAdapter : TypeAdapter<String?>() {
@Throws(IOException::class)
override fun read(reader: JsonReader): String {
if (reader.peek() === JsonToken.NULL) {
reader.nextNull()
return ""
}
return reader.nextString()
}

@Throws(IOException::class)
override fun write(writer: JsonWriter, value: String?) {
if (value == null) {
writer.nullValue()
return
}
writer.value(value)
}
}


class NullStringToEmptyAdapterFactory<T> : TypeAdapterFactory {
override fun <T> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
val rawType = type.rawType as Class<T>
return if (rawType != String::class.java) {
null
} else StringAdapter() as TypeAdapter<T>
}
}

val gson =
GsonBuilder().registerTypeAdapterFactory(NullStringToEmptyAdapterFactory<Any>()).create()

val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(WebAddresses.URL_MASK_API)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
api = retrofit.create<MaskAPI>(MaskAPI::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object StatUtil {
"few" -> return "1+"
"empty" -> return "재고소진"
}
return "error"
return "에러"
}
fun convertStatToDetailString(stat : String): String{
when (stat) {
Expand All @@ -17,7 +17,7 @@ object StatUtil {
"few" -> return "1개 이상 30개 미만"
"empty" -> return "재고소진"
}
return "error"
return "에러"
}

fun convertTypeToString(type : String): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class MapFragment : BaseFragment<FragmentMapBinding, MapViewModel>(), OnMapReady

stocks.forEach { stock ->
Logger.log("CAMERA_CHANGED", Gson().toJson(stock))
if (stock.remainStat == null) stock.remainStat = ""
val infoWindow = InfoWindow()
infoWindow.adapter = object : InfoWindow.DefaultTextAdapter(context!!) {
override fun getText(infoWindow: InfoWindow): CharSequence {
Expand All @@ -88,6 +89,9 @@ class MapFragment : BaseFragment<FragmentMapBinding, MapViewModel>(), OnMapReady
marker.icon = MarkerIcons.RED
"empty" ->
marker.icon = MarkerIcons.GRAY
else -> {
marker.icon = MarkerIcons.GRAY
}
}
marker.map = map
infoWindow.open(marker)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class SearchFragment : BaseFragment<FragmentSearchBinding, SearchViewModel>() {
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (!viewModel.latestSearchKeyword.equals(s.toString())) {
if (viewModel.latestSearchKeyword != s.toString()) {
layoutSwitch(false)
}
}
Expand Down

0 comments on commit f5be7ad

Please sign in to comment.