Categories
discuss

Android Kotlin “withTimeout” coroutine how to handle exception

I’m using retrofit2 to call API with kotlin coroutines but the API has status code 200, 400 and 700. When request the API and response status code was 400 or 700, which the “withTimeout” coroutine can be exception crash. I want to handle status code 400 and 700 response message with “withTimeout” coroutine or how to customize “CoroutineScope” thanks.

This is my code

 suspend fun getLogin() {
    try {
        var result = withTimeout(5_000) {  // <----this line can be crashed with http code 700
            accountService.getLogin(
               LoginRequest() // request data
            ) // retrofit
        }
        when (result.state_code) { // api response state code
            200 -> {
                 Timber.i("Create account got data: ${result.source}")
            }
            400 -> {
                 //....... handle this status code error
            }
            700 -> {
               //....... handle this status code error
            }
        }
    } catch (e: ApiError) {
        throw ApiError("Unable to login", e)
    }
}

Error message

2020-04-26 22:55:10.384 28895-28895/com.mgear.mednetapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mgear.mednetapp, PID: 28895
retrofit2.HttpException: HTTP 700  
    at retrofit2.KotlinExtensions$await$2$2.onResponse(KotlinExtensions.kt:53)
    at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:150)
    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:504)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:919)

Answer

Actually, SocketTimeoutException should be handled, but here you can handle the generic Exception here and later check whether your exception is SocketTimeoutException, JsonSyntaxException, UnknownHostException, ConnectException or others.

So, you can try this.

suspend fun getLogin() {
 try {
 //Your api call code here.
 }catch (e: Exception) {
    e.printStacktrace();
    //throw ApiError("Unable to login", e)
 }
}
Categories
discuss

image Asset Studio not available in flutter projects

I want to create a new icon using the assets tool as described here. Surprisingly, the option to create an image set is missing in the context menu:

Screenshot

Answer

to create an Image asset in flutter you need to do the following:

1 – in project view mode, right-click on your root project and choose new directory; and give it a name images

2 – go to pubspec.yaml file and find the assets section. it will be commented so uncomment it then add your directory to the assets. it should look like this:

assets:
  - images/             

be careful with the indentation though. it won’t work if your indentation is not correct.

then you can use all the image assets that you add to that directory in your project.

if you need more clarifications, let me know in the comments!

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..