2021年1月17日星期日

OkHTTP crashes returning result from Coroutine

Im having crush:

01-18 03:51:52.295 31646-31693/com.example.company.test_app E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1      Process: com.example.company.test_app, PID: 31646      android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.          at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556)          at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:907)      at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556)      at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:907)      at android.view.View.requestLayout(View.java:18722)      at android.view.View.requestLayout(View.java:18722)      at android.view.View.requestLayout(View.java:18722)      at android.view.View.requestLayout(View.java:18722)      at android.view.View.requestLayout(View.java:18722)      at android.view.View.requestLayout(View.java:18722)      at androidx.constraintlayout.widget.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)      at android.view.View.requestLayout(View.java:18722)      at android.widget.TextView.checkForRelayout(TextView.java:7172)      at android.widget.TextView.setText(TextView.java:4342)      at android.widget.TextView.setText(TextView.java:4199)      at android.widget.TextView.setText(TextView.java:4174)      at com.example.company.myapplication.MainActivity$onCreate$1$1.invokeSuspend(MainActivity.kt:28)      at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)      at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)      at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)      at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)      at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)      at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)     --------- beginning of system  

01-18 03:51:52.302 1662-5481/system_process W/ActivityManager: Force finishing activity com.example.company.test_app/.MainActivity

with this OkHTTP test application:

import kotlinx.coroutines.CoroutineScope  import kotlinx.coroutines.Dispatchers.IO  import kotlinx.coroutines.launch  import kotlinx.coroutines.withContext  import okhttp3.OkHttpClient  import okhttp3.Request    const val FAILED = "Failed"  const val OK = "Ok"    class MainActivity : AppCompatActivity() {        private val okHttpClient = OkHttpClient()        override fun onCreate(savedInstanceState: Bundle?) {          super.onCreate(savedInstanceState)          setContentView(R.layout.activity_main)            button.setOnClickListener {              CoroutineScope(IO).launch {                  textView.text = editText.text                          ?.let { getResponse(it.toString()) }                          ?: FAILED              }          }      }        private suspend fun getResponse(url: String): String {          return withContext(IO) {              try {                  val request = Request.Builder()                          .url(url)                          .build()                  val response = okHttpClient.newCall(request).execute()                  if (response.isSuccessful) OK else FAILED // crushes here               } catch (ex: Exception) {                  FAILED  // ... or here              }          }      }    }  

The task is to check URL is valid (200) or erroneous (400,500, etc). The request pass-through with Ok but the app crashes when returns from the function. When it fails, the app also crashes, but on exception returns.

Thanx

https://stackoverflow.com/questions/65767628/okhttp-crashes-returning-result-from-coroutine January 18, 2021 at 09:07AM

没有评论:

发表评论