@@ -37,7 +37,6 @@ import android.annotation.SuppressLint
37
37
import android.content.Context
38
38
import android.graphics.Point
39
39
import android.os.Handler
40
- import android.os.HandlerThread
41
40
import android.provider.Settings
42
41
import android.util.Log
43
42
import com.google.gson.Gson
@@ -46,6 +45,7 @@ import java.io.IOException
46
45
import java.io.Serializable
47
46
import org.json.JSONArray
48
47
import com.google.gson.reflect.TypeToken
48
+ import android.os.Looper
49
49
50
50
@SuppressLint("SimpleDateFormat")
51
51
object API {
@@ -270,19 +270,26 @@ END
270
270
client.newCall(request).enqueue(object: Callback {
271
271
override fun onFailure(call: Call?, e: IOException?) {
272
272
e?.printStackTrace()
273
- callback(Error(ErrorType.Fatal, e?.message ?: "Chamada falhou sem mensagem de erro!"), null)
273
+ Handler(Looper.mainLooper).post {
274
+ callback(Error(ErrorType.Fatal, e?.message ?: "Chamada falhou sem mensagem de erro!"), null)
275
+ }
274
276
}
275
277
276
278
override fun onResponse(call: Call?, response: Response?) {
277
279
if (response == null || response.code() == 502) {
278
- callback(Error(ErrorType.Fatal, "Erro Fatal (502) - Tente novamente"), null)
280
+ Handler(Looper.mainLooper).post {
281
+ callback(Error(ErrorType.Fatal, "Erro Fatal (502) - Tente novamente"), null)
282
+ }
283
+ return
279
284
}
280
285
281
286
var responseBody = try {
282
287
val stringBody = response?.body()?.string()
283
288
JSONObject(stringBody)
284
289
} catch (e: Exception) {
285
- callback(Error(ErrorType.Fatal, "502 - Tente novamente"), null)
290
+ Handler(Looper.mainLooper).post {
291
+ callback(Error(ErrorType.Fatal, "502 - Tente novamente"), null)
292
+ }
286
293
null
287
294
return
288
295
}
@@ -295,15 +302,21 @@ END
295
302
//TODO Fetch correct error type
296
303
val error = Error(ErrorType.valueOf(jsonError.getString("type")), jsonError.getString("message"))
297
304
Log.e("API Error", jsonError.getString("type") + " - " + error.message);
298
- callback(error, null)
305
+ Handler(Looper.mainLooper).post {
306
+ callback(error, null)
307
+ }
299
308
} else {
300
- callback(null, responseBody)
309
+ Handler(Looper.mainLooper).post {
310
+ callback(null, responseBody)
311
+ }
301
312
}
302
313
}
303
314
})
304
315
} catch (e: JSONException) {
305
316
e.printStackTrace()
306
- callback(Error(ErrorType.Fatal, e.message ?: "Erro ao parsear json"), null)
317
+ Handler(Looper.mainLooper).post {
318
+ callback(Error(ErrorType.Fatal, e.message ?: "Erro ao parsear json"), null)
319
+ }
307
320
}
308
321
}
309
322
}
0 commit comments