1

I am using Retrofit with coroutines (built in support). I have a Retrofit interface, retrofit implementation and the API implementation created by Retrofit, but it doesn't matter as it doesn't get called. It crashes before that, here: In XService.kt

suspend fun test() {
    val mediaType = MediaType.parse("text/plain")
    val requestBody = RequestBody.create(mediaType, "TEST STRING")  // Crashes here
    // Now make some network requests
}

which is called by a viewModel:

    suspend fun test() {
        return withContext(viewModelScope.coroutineContext) {
            service.test()
        }
    }

which is called by a Fragment:

lifecycleScope.launch() { model.test() }

Unhelpful error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: package_name, PID: 9729
I/Process: Sending signal. PID: 9729 SIG: 9

I'm just not sure why creating a RequestBody causes a crash. What is weird is, when debugging and going slowly through the code, it doesn't crash... I have simplified the code so its easier for you to read, but the reason why its scoped to Fragment, not VM is because I want the Fragment to change the UI based on the response.

EDIT: It turns out if I wrap it in a try/ catch block, the line which crashes usually doesn't crash. Instead, the network call afterwards does, giving printing out:

I/System.out: java.net.UnknownHostException: Unable to resolve host "qats.orth.uk": No address associated with hostname
        try {
        val requestBody = RequestBody.create(mediaType, mode.toString())
        return api.createQuiz(requestBody).createResult()
        } catch (e: Exception) {
            println(e)
        }

So I am confused. Why does putting it in a try/catch block change what error message I get. Without try/catch, I got a useless crash with no stacktrace. I guess this may be the challenge with coroutines.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
  • Would you please add your Retrofit service to the question too? I guess the problem is related to the `text/plain`. [See this](https://github.com/square/retrofit/issues/1698). – ʍѳђઽ૯ท Feb 04 '21 at 09:28
  • I tried other types as well, this was the most basic type I can use. The retrofit interface is **not** being called, so its not that relevant here. It will simply confuse readers because it has API specific details like custom types. I am not even calling it now and its crashing. I will add a link to a file on Github though. – Ben Butterworth Feb 04 '21 at 09:39
  • [here](https://github.com/ben-xD/Qats/blob/master/android/common/repository/src/main/java/uk/orth/qats/repository/QuizAPI.kt) it is. The project is still a work in progress. – Ben Butterworth Feb 04 '21 at 09:43
  • 2
    Okay, so after wrapping the `XService` code in a try/ catch, the error is actually printed: `I/System.out: java.net.UnknownHostException: Unable to resolve host "qats.orth.uk": No address associated with hostname`. Yes this is obvious because I haven't deployed the server. I didn't know I needed to try/catch, I thought the `Response` object have an error. Maybe I am being a fool, but I won't take the blame 100% myself . I could not find any authoritative docs on Retrofit. Not a single time did I see coroutines in their docs. – Ben Butterworth Feb 04 '21 at 09:53
  • It's fine. You can deploy it on your server and then try. – ʍѳђઽ૯ท Feb 04 '21 at 10:04
  • 1
    Thanks, I will continue learning about coroutine exception handling as well – Ben Butterworth Feb 04 '21 at 10:15

0 Answers0