Android Question Exception when calling Response.Release in release version but not in debug

Acuario

Active Member
Licensed User
Longtime User
I´m using OkHttpClient to post a file to a webserver (I have the latest OkHttpUtils2 V3.04).
In the ResponseSuccess event I call Response.Release

B4X:
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
    Log("Response:" & Response.StatusCode)
    If Response <> Null Then
        Log("Response.Release done")
        Response.Release
    End If
End Sub

When I run in debug mode everything is ok however when I run in release mode I get an exception when calling Response.Release

Response.Release done
main_hc_responsesuccess (java line: 747)
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1667)
at java.net.SocketInputStream.read(SocketInputStream.java:172)
at java.net.SocketInputStream.read(SocketInputStream.java:143)
at okio.InputStreamSource.read(JvmOkio.kt:90)
at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:129)
at okio.RealBufferedSource.request(RealBufferedSource.kt:206)
at okio.RealBufferedSource.require(RealBufferedSource.kt:199)
at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.kt:381)
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:429)
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408)
at okhttp3.internal.Util.skipAll(Util.kt:337)
at okhttp3.internal.Util.discard(Util.kt:358)
at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.close(Http1ExchangeCodec.kt:450)
at okio.ForwardingSource.close(ForwardingSource.kt:34)
at okhttp3.internal.connection.Exchange$ResponseBodySource.close(Exchange.kt:309)
at okio.RealBufferedSource.close(RealBufferedSource.kt:477)
at okhttp3.internal.Util.closeQuietly(Util.kt:488)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpResponse.Release(OkHttpClientWrapper.java:637)
at b4a.example.main._hc_responsesuccess(main.java:747)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at anywheresoftware.b4a.BA$2.run(BA.java:395)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:8061)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:703)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)

Any ideas why this is happening and how to fix it?

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
Doesn´t look like you are using the library the way which is expected.

You should release the job in the JobDone-Event.
 
Upvote 1

drgottjr

Expert
Licensed User
Longtime User
android.os.NetworkOnMainThreadException
you're doing a network op on the main thread. okhttputils2 is designed to run on a
different thread, so - as donmanfred suggests - you're not using okhttputils2 correctly
(or the error is occurring elsewhere, and okhttputils2 has nothing to do with it.)
 
Upvote 0

Acuario

Active Member
Licensed User
Longtime User
I have sorted it (I think!)
Here is my code for anyone who needs it.
B4X:
Sub sendFile() As ResumableSub
    Private myJob As HttpJob
    Dim mp As MultipartFileData
    mp.Initialize
    mp.Dir = File.DirInternal
    mp.FileName = "plan.gpx"
    mp.KeyName = "file"
    mp.ContentType = "text/plain"
    
    'Add files
    Dim files As List
    files.Initialize
    files.Add(mp)
    
    'Add name / values pairs (parameters)
    Dim NV As Map
    NV.Initialize
    NV.Put("note1", "abc")
    Dim url As String = "http://192.168.0.111/upload"

    myJob.Initialize("", Me)
    
    myJob.PostMultipart (url, NV, files)
    Wait For (myJob) JobDone (myJob As HttpJob)
    Log("Result:" & myJob.Response.StatusCode)
    If myJob.Response.StatusCode = 200 Then
        ToastMessageShow("File uploaded", False)
    Else
        ToastMessageShow("Upload failed code " & myJob.Response.StatusCode, True)
    End If
    myJob.Release
    Return True
End Sub
 
Upvote 0
Top