Android Question Get body even with a response with error. okHttp

Douglas Farias

Expert
Licensed User
Longtime User
Hello everyone.
I'm building an application and unfortunately I do not have server side access.

I need to get the response from the server even if it returns me an error in okHttp

example
B4X:
    If j.Success Then
        Log("success")
        Log(j.GetString)
    Else
        Log("error")
        Log(j.ErrorMessage)
        Log(j.GetString)
    End If


Sometimes i will get log success and sometimes i got error.
but when i get the error, the j.GetString is the same of the success response.

B4X:
    If j.Success Then
        Log(j.GetString) 'WORKS FINE, I GOT THE SOAP XML RESPONSE, ALL WORKING FINE
    Else
        Log(j.GetString) 'WORKS FINE, I GOT THE SOAP XML RESPONSE, ALL WORKING FINE (ON MY DEVICE)
    End If


this code above works fine on my device, MI A2 Lite with Android 9
but on old devices it crash on j.GetString

Obs: runing the code above on my device i got this logs

(error case example)
- error
- javax.net.ssl.SSLException: Read error: ssl=0x75f489c188: I/O error during system call, Connection reset by peer
- the response in xml

(success case example)
- success
- the response in xml


On the old Android Versions, or another devices i got a crash in j.GetString when the case is Error.
Error occurred on line: 232 (HttpJob)
java.io.FileNotFoundException: /data/user/0/my.app.here/cache/20 (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:200)
at java.io.FileInputStream.<init>(FileInputStream.java:150)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:214)
at teste.consulta.placa.httpjob._getstring2(httpjob.java:400)
at teste.consulta.placa.httpjob._getstring(httpjob.java:120)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:250)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
at anywheresoftware.b4a.BA$2.run(BA.java:370)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy (ignored)**

Crash on this line on httpJob Class
B4X:
tr.Initialize2(File.OpenInput(HttpUtils2Service.TempFolder, taskId), Encoding)


the question is, how can I get the body of the error, regardless if the response from httpjob is successful or not ..

what modifications should I make in the HttpUtils2Service class?

remembering: I do not want to get the error log, or know what error occurred, I need is the return of the job, job.getstring ....


Thx All
 

DonManfred

Expert
Licensed User
Longtime User
job.getstring ....
It is not available when job.success is false. At least in some situations.
I don´t think that there is something you can do.
 
Upvote 0
Top