I need to retreive various JSONObjects from an API using Volley. The API requires header authentication using Basic. It works if I don't add the headers but when I do I keep getting a org.jetbrains.kotlin.codegen.CompilationException
. Here are the functions I wrote:
private suspend fun readData(url: String): JSONObject = suspendCancellableCoroutine { continuation ->
val request: JsonObjectRequest = object: JsonObjectRequest(Request.Method.POST, url, null, Response.Listener<JSONObject> {
continuation.resume(it)
}, Response.ErrorListener {
continuation.resumeWithException(Exception(it.cause))
}) {
override fun getHeaders(): Map<String, String> = mapOf("Content-Type" to "application/json", "charset" to "utf-8", "Authorization" to "Basic ".plus(android.util.Base64.encode(String.format("%s:%s", getString(R.string.api_login_username), getString(R.string.api_login_password)).toByteArray(), android.util.Base64.DEFAULT)))
}
request.setShouldCache(false)
this.queue?.add(request)
}
fun data(url: String) = async(CommonPool) { readData(url) }
And them implement them like so:
val brands = data(getString(R.string.api_url).plus("/api/brand"))
val models = data(getString(R.string.api_url).plus("/api/model"))
launch(UI) {
val brandsJsonArray = brands.await().getJSONArray("recordset")
val modelsJsonArray = models.await().getJSONArray("recordset")
}
But this is the exception I keep getting once I try to compile the project:
org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Couldn't inline method call 'suspendCoroutine' into
public final suspend fun readData(queue: com.android.volley.RequestQueue?, url: kotlin.String, continuation: kotlin.coroutines.experimental.Continuation<org.json.JSONObject>): kotlin.Any? defined in com.universales.inspeccion.activities.InspectionCreateActivity
suspend fun readData(url: String): JSONObject = suspendCancellableCoroutine { continuation ->
val request = object : JsonObjectRequest(Request.Method.GET, url, JSONObject(), Response.Listener {
continuation.resume(it)
}, Response.ErrorListener {
continuation.resumeWithException(Exception(it.cause))
}) {
override fun getHeaders(): Map<String, String> = mapOf("Content-Type" to "application/json", "charset" to "utf-8", "Authorization" to "Basic ".plus(android.util.Base64.encode(String.format("%s:%s", getString(R.string.api_login_username), getString(R.string.api_login_password)).toByteArray(), android.util.Base64.DEFAULT)))
}
queue?.add(request)
continuation.invokeOnCompletion {
request.cancel()
}
}
Cause: suspendCancellableCoroutine$default (ZLkotlin/jvm/functions/Function1;Lkotlin/coroutines/experimental/Continuation;ILjava/lang/Object;)Ljava/lang/Object;:
L0
L1
LINENUMBER 167 L1
ICONST_0
ISTORE 0
L2
LINENUMBER 297 L2
ICONST_0
INVOKESTATIC kotlin/jvm/internal/InlineMarker.mark (I)V
ALOAD 2
INVOKESTATIC kotlin/coroutines/experimental/jvm/internal/CoroutineIntrinsics.normalizeContinuation (Lkotlin/coroutines/experimental/Continuation;)Lkotlin/coroutines/experimental/Continuation;
ASTORE 4
L3
LINENUMBER 298 L3
NEW kotlinx/coroutines/experimental/CancellableContinuationImpl
DUP
ALOAD 4
ICONST_1
INVOKESPECIAL kotlinx/coroutines/experimental/CancellableContinuationImpl.<init> (Lkotlin/coroutines/experimental/Continuation;I)V
ASTORE 5
L4
LINENUMBER 299 L4
ILOAD 0
IFNE L5
ALOAD 5
INVOKEVIRTUAL kotlinx/coroutines/experimental/CancellableContinuationImpl.initCancellability ()V
L5
LINENUMBER 300 L5
ALOAD 1
ALOAD 5
INVOKEINTERFACE kotlin/jvm/functions/Function1.invoke (Ljava/lang/Object;)Ljava/lang/Object;
POP
L6
LINENUMBER 301 L6
ALOAD 5
INVOKEVIRTUAL kotlinx/coroutines/experimental/CancellableContinuationImpl.getResult ()Ljava/lang/Object;
L7
NOP
L8
LINENUMBER 297 L8
ICONST_1
INVOKESTATIC kotlin/jvm/internal/InlineMarker.mark (I)V
L9
LINENUMBER 302 L9
ARETURN
L10
LOCALVARIABLE cancellable Lkotlinx/coroutines/experimental/CancellableContinuationImpl; L4 L7 5
LOCALVARIABLE cont Lkotlin/coroutines/experimental/Continuation; L3 L8 4
LOCALVARIABLE $i$a$2$suspendCoroutineOrReturn I L3 L8 6
LOCALVARIABLE holdCancellability Z L2 L10 0
LOCALVARIABLE block Lkotlin/jvm/functions/Function1; L0 L10 1
LOCALVARIABLE $i$f$suspendCancellableCoroutine I L2 L10 3
MAXSTACK = 4
MAXLOCALS = 7
Cause: kotlin.KotlinNullPointerException
File being compiled at position: (124,75) in C:/Users/jcardama/Projects/android/inspeccion/app/src/main/java/com/universales/inspeccion/activities/InspectionCreateActivity.kt
The root cause was thrown at: MethodInliner.kt:280
at org.jetbrains.kotlin.codegen.inline.InlineCodegen.throwCompilationException(InlineCodegen.kt:124)
at org.jetbrains.kotlin.codegen.inline.InlineCodegen.performInline(InlineCodegen.kt:167)
at org.jetbrains.kotlin.codegen.inline.PsiInlineCodegen.genCallInner(InlineCodegen.kt:661)
at org.jetbrains.kotlin.codegen.CallGenerator$DefaultImpls.genCall(CallGenerator.kt:95)
at org.jetbrains.kotlin.codegen.inline.PsiInlineCodegen.genCall(InlineCodegen.kt:642)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2398)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.invokeMethodWithArguments(ExpressionCodegen.java:2347)
at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments$1.invoke(Callable.kt:41)
at org.jetbrains.kotlin.codegen.Callable$invokeMethodWithArguments$1.invoke(Callable.kt:13)
at org.jetbrains.kotlin.codegen.OperationStackValue.putSelector(StackValue.kt:79)
at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:109)
at org.jetbrains.kotlin.codegen.StackValue.put(StackValue.java:98)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.putStackValue(ExpressionCodegen.java:349)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:334)
at org.jetbrains.kotlin.codegen.ExpressionCodegen.returnExpression(ExpressionCodegen.java:1621)
at org.jetbrains.kotlin.codegen.coroutines.SuspendFunctionGenerationStrategy.doGenerateBody(SuspendFunctionGenerationStrategy.kt:88)
at org.jetbrains.kotlin.codegen.FunctionGenerationStrategy$CodegenBased.generateBody(FunctionGenerationStrategy.java:80)
at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:620)
at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethodBody(FunctionCodegen.java:391)
at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:244)
at org.jetbrains.kotlin.codegen.FunctionCodegen.generateMethod(FunctionCodegen.java:168)
at org.jetbrains.kotlin.codegen.FunctionCodegen.gen(FunctionCodegen.java:139)
at org.jetbrains.kotlin.codegen.MemberCodegen.genSimpleMember(MemberCodegen.java:199)
at org.jetbrains.kotlin.codegen.ClassBodyCodegen.generateDeclaration(ClassBodyCodegen.java:161)
at org.jetbrains.kotlin.codegen.ClassBodyCodegen.generateBody(ClassBodyCodegen.java:75)
at org.jetbrains.kotlin.codegen.MemberCodegen.generate(MemberCodegen.java:131)
at org.jetbrains.kotlin.codegen.MemberCodegen.genClassOrObject(MemberCodegen.java:305)
at org.jetbrains.kotlin.codegen.MemberCodegen.genClassOrObject(MemberCodegen.java:289)
at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generateClassOrObject(PackageCodegenImpl.java:161)
at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generateClassesAndObjectsInFile(PackageCodegenImpl.java:86)
at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generateFile(PackageCodegenImpl.java:119)
at org.jetbrains.kotlin.codegen.PackageCodegenImpl.generate(PackageCodegenImpl.java:66)
at org.jetbrains.kotlin.codegen.DefaultCodegenFactory.generatePackage(CodegenFactory.kt:97)
at org.jetbrains.kotlin.codegen.DefaultCodegenFactory.generateModule(CodegenFactory.kt:68)
at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.doGenerateFiles(KotlinCodegenFacade.java:47)
at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:39)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.kt:454)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:150)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:161)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:63)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:107)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:51)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:96)
at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:463)
at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:98)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally(IncrementalCompilerRunner.kt:228)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.access$compileIncrementally(IncrementalCompilerRunner.kt:38)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner$compile$2.invoke(IncrementalCompilerRunner.kt:90)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:102)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.execIncrementalCompiler(CompileServiceImpl.kt:538)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.access$execIncrementalCompiler(CompileServiceImpl.kt:98)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:418)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:98)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:920)
at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$$inlined$ifAlive$lambda$2.invoke(CompileServiceImpl.kt:98)
at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:950)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:919)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:417)
at sun.reflect.GeneratedMethodAccessor102.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: kotlin.KotlinNullPointerException
at org.jetbrains.kotlin.codegen.inline.MethodInliner$doInline$lambdaInliner$1.visitMethodInsn(MethodInliner.kt:280)
at org.jetbrains.org.objectweb.asm.tree.MethodInsnNode.accept(MethodInsnNode.java:133)
at org.jetbrains.org.objectweb.asm.tree.InsnList.accept(InsnList.java:162)
at org.jetbrains.org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:817)
at org.jetbrains.kotlin.codegen.inline.MethodInliner.doInline(MethodInliner.kt:351)
at org.jetbrains.kotlin.codegen.inline.MethodInliner.doInline(MethodInliner.kt:93)
at org.jetbrains.kotlin.codegen.inline.MethodInliner.access$doInline(MethodInliner.kt:39)
at org.jetbrains.kotlin.codegen.inline.MethodInliner$doInline$lambdaInliner$1.visitMethodInsn(MethodInliner.kt:259)
at org.jetbrains.org.objectweb.asm.tree.MethodInsnNode.accept(MethodInsnNode.java:133)
at org.jetbrains.org.objectweb.asm.tree.InsnList.accept(InsnList.java:162)
at org.jetbrains.org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:817)
at org.jetbrains.kotlin.codegen.inline.MethodInliner.doInline(MethodInliner.kt:351)
at org.jetbrains.kotlin.codegen.inline.MethodInliner.doInline(MethodInliner.kt:93)
at org.jetbrains.kotlin.codegen.inline.MethodInliner.doInline(MethodInliner.kt:66)
at org.jetbrains.kotlin.codegen.inline.InlineCodegen.inlineCall(InlineCodegen.kt:278)
at org.jetbrains.kotlin.codegen.inline.InlineCodegen.performInline(InlineCodegen.kt:158)
... 73 more
Push out the definition of object: JsonObjectRequest
to a named class that takes the continuation as a constructor parameter. This way you'll make the code inside suspendCancellableCoroutine
simpler and avoid this compiler bug.
suspend fun readData(url: String): JSONObject = suspendCancellableCoroutine { continuation ->
RequestWithHeaders(url, continuation).also {
it.setShouldCache(false)
queue.add(it)
}
}
class RequestWithHeaders(
url: String,
continuation: Continuation<JSONObject>
) : JsonObjectRequest(Request.Method.POST, url, null, Response.Listener {
continuation.resume(it)
}, Response.ErrorListener {
continuation.resumeWithException(Exception(it.cause))
}) {
override fun getHeaders(): Map<String, String> = mapOf(
"Content-Type" to "application/json; charset=utf-8",
"Authorization" to "Basic")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With