withContext-ArrayIndexOutOfBoundsException

时间:2019-06-14 12:30:37

标签: android kotlin-coroutines

在Android应用中,我想在后台线程中执行网络操作。 因此,我为此使用Kotlin协程withContext(Dispatchers.IO)

摘要:

@GET("trader/{trader_operation}")
    Deferred<Response<Void>> executeTraderOperation(@Path("trader_operation") String traderOperation,
                                                    @Query("base") String base,
                                                    @Query("quote") String quote,
                                                    @Query("sender") String sender,


import kotlinx.coroutines.*
import retrofit2.Response
import java.util.*
class TransportService {
        companion object {
            private val SERVICE_UNAVAILABLE_CODE = 503

            private val TAG = TransportService::class.java.name@Query("key") String key);

     suspend fun executeTraderOperation(traderOperation: Trader.Operation, base: String, quote: String): NetworkResponse {
                val traderMonitorRestClient = RestClientFactory.createRestClient(TraderMonitorRestClient::class.java)
                val sender = BuildConfig.APPLICATION_ID + "_" + BuildConfig.VERSION_NAME
                val key = DateUtil.getDateAsString(Date(), "mmHHddMMyyyy")
                val operrationDeferred: Deferred<Response<Void>> = traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)
                return executeOperation(operrationDeferred)
            }

            private suspend fun executeOperation(operation: Deferred<Response<*>>): NetworkResponse = withContext(Dispatchers.IO) {
                val networkResponse = NetworkResponse()
                try {
                    val response = operation.await()
                    networkResponse.isSuccess = response.isSuccessful
                    if (response.isSuccessful) {
                        networkResponse.successResponse = response
                    } else {
                        val errorResponse = ErrorUtils.parseError(response)
                        networkResponse.errorResponse = errorResponse
                    }
                    networkResponse
                } catch (e: Throwable) {
                    val errorResponse = ErrorResponse()
                    errorResponse.setCode(SERVICE_UNAVAILABLE_CODE)
                    errorResponse.setMessage("Service unavailable")
                    networkResponse.errorResponse = errorResponse
                    networkResponse
                }
            }
    }
    }

但是在此行中出现运行时错误:

val operrationDeferred: Deferred<Response<Void>> = traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key)

此处错误:

06-14 15:23:45.394 9534-9534/? W/top: type=1400 audit(0.0:39232676): avc: denied { search } for name="7" dev="proc" ino=9011 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:r:kernel:s0 tclass=dir permissive=0



 Throwing new exception 'length=117; index=1902' with unexpected pending exception: java.lang.ArrayIndexOutOfBoundsException: length=117; index=1902
   at java.lang.Object com.myptoject.service.TransportService$Companion.executeTraderOperation2(com.myptoject.model.Trader$Operation, java.lang.String, java.lang.String, kotlin.coroutines.Continuation) (TransportService.kt:37)
   at java.lang.Object com.myptoject.viewmodel.TradersViewModel$doClickExecuteTraderOperation$1.invokeSuspend(java.lang.Object) (TradersViewModel.kt:60)
   at void kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(java.lang.Object) (ContinuationImpl.kt:33)
   at void kotlinx.coroutines.DispatchedTask.run() (Dispatched.kt:238)
   at void android.os.Handler.handleCallback(android.os.Message) (Handler.java:739)
   at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:95)
   at void android.os.Looper.loop() (Looper.java:148)
   at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:5417)
   at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
   at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:726)
   at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:616)

0 个答案:

没有答案