Android Question Possible bug in B4A 9

hatzisn

Expert
Licensed User
Longtime User
Hi everyone,

I have this code:

B4X:
    Dim jp As JSONParser
    jp.Initialize(sRes)
    Dim mU As Map
    mU.Initialize
    mU = jp.NextObject
  
    Dim su As StringUtils
    For Each k As String In mU.Keys
        If k.StartsWith("file") Then
            Dim sFile As String = su.DecodeUrl(mU.Get(k), "UTF8")
            If File.Exists(rp.GetSafeDirDefaultExternal("Dnlds"), sFile.SubString(sFile.indexof("/") + 1)) Then
                '
            Else
                Dim hj As HttpJob
                hj.Initialize("", Me)
                hj.Download(su.DecodeUrl(mU.Get("URL"), "UTF8") & sFile)
                Log(su.DecodeUrl(mU.Get("URL"), "UTF8") & sFile)
                wait for (hj) JobDone(job As HttpJob)
                If job.Success Then
                    Dim b() As Byte = CommonFunctions2.GetByteArrayFromInputStream(job.GetInputStream)
                    CommonFunctions2.BytesToFile(rp.GetSafeDirDefaultExternal("Dnlds"), sFile.SubString(sFile.indexof("/") + 1), b)
                Else
                    Dim hj2 As HttpJob
                    hj2.Initialize("", Me)
                    hj2.Download("http://www.server2.com/images/" & sFile.SubString(sFile.indexof("/") + 1))
                    wait for (hj2) JobDone(job2 As HttpJob)
                    If job2.Success Then
                        Dim b() As Byte = CommonFunctions2.GetByteArrayFromInputStream(job.GetInputStream)
                        CommonFunctions2.BytesToFile(rp.GetSafeDirDefaultExternal("Guide"), sFile.SubString(sFile.indexof("/") + 1), b)
                    Else
                        '
                    End If                 
                    job2.Release
                    hj2.Release
                End If
                job.Release
                hj.Release
            End If
        End If
    Next

The sFile variable is in the form "subfolder/filename.jpg".
The mU.Get("URL") is in the form "https://www.server1.com/"
The line [ wait for (hj) JobDone(job As HttpJob) ] is in line 879.
The functions in CommonFunctions2 are the following:

B4X:
Sub BytesToFile (Dir As String, FileName As String, Data() As Byte)
    Dim out As OutputStream = File.OpenOutput(Dir, FileName.ToLowerCase, False)
    out.WriteBytes(Data, 0, Data.Length)
    out.Close
End Sub

Sub GetByteArrayFromInputStream(ins As InputStream) As Byte()
    Return Bit.InputStreamToBytes(ins)
End Sub

The code starts at line 862 and ends in line 901. I add a breakpoint in line
B4X:
Dim sFile As String = su.DecodeUrl(mU.Get(k), "UTF8")
and then when I continue running the code I get this error:

ResponseError. Reason: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found., Response:
Error occurred on line: 273 (HttpJob)
java.io.FileNotFoundException: /data/user/0/dhqi.trial2.project/cache/3 (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:214)
at dhqi.trial2.project.httpjob._getinputstream(httpjob.java:167)
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:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
java.lang.RuntimeException: Unable to create service dhqi.trial2.project.starter: java.lang.RuntimeException: java.net.SocketException: Socket closed
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3544)
at android.app.ActivityThread.-wrap6(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1732)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.RuntimeException: java.net.SocketException: Socket closed
at anywheresoftware.b4a.shell.Shell.virtualAssets(Shell.java:164)
at anywheresoftware.b4a.shell.Shell.start(Shell.java:102)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:92)
at dhqi.trial2.project.starter.onCreate(starter.java:34)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3534)
... 8 more
Caused by: java.net.SocketException: Socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:151)
at java.net.SocketInputStream.read(SocketInputStream.java:120)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
at java.io.DataInputStream.readInt(DataInputStream.java:387)
at anywheresoftware.b4a.shell.Shell.virtualAssets(Shell.java:136)
... 12 more

The application runs in debug mode...
As you can see the error appears in line 273 where there is no httpjob at all. Any suggestions?
Furthermore when I click in the line where it says error in line 273 it doesn't get me to this line.
My previous version was 7.80 and just today I upgraded to version 9. Am I missing something important here?
 

MarkusR

Well-Known Member
Licensed User
Longtime User
As you can see the error appears in line 273 where there is no httpjob at all. Any suggestions?
what is line 273?

from erros:
i think there is a problem in the starter service with tcp Sockets, then the download did not work and the file was not found.
and at top i see SSLHandshakeException but u not use https only http.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Line 273 is an empty line between two other lines which have nothing to do with internet access...
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Do you get the error if you run the code without the breakpoint?

- Colin.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
if u insert some rows at top did this error move too?
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
When I switched to release obfuscated no error occurred at all. I will try again with debug tomorrow.

@Computersmith64 If I add the breakpoint and step into the [wait for - line] with F8 the job response does not fire at all, but if I press F5 it "explodes" in my face.
@MarcusR I will try that tomorrow Markus. Thanks for the tip.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
what is line 273?

from erros:
i think there is a problem in the starter service with tcp Sockets, then the download did not work and the file was not found.
and at top i see SSLHandshakeException but u not use https only http.

The server1 and server2 values are "masked" values. The first one corresponds to an https URL and the second one corresponds to an http URL.
I was trying with Fiddler to see the posts and responds of some services and in Fiddler capture data mode the https links do not work in any B4A app (that I have tried) in my device. Thus the SSLHandshakeException. But should there was an error on the connection shouldn't the code execution be diverted in job.Success = false brunch of the if statement?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
As you can see the error appears in line 273 where there is no httpjob at all
It's line 273 in the HttpJob module. So in your source directory look for the generated httpjob.java file and see what line 273 is.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
@Erel, you were correct once again. The error was very obvious for me to see. In the Job.Success = False of the first HttpJob this code:

B4X:
Dim b() As Byte = CommonFunctions2.GetByteArrayFromInputStream(job.GetInputStream)

should have been

B4X:
Dim b() As Byte = CommonFunctions2.GetByteArrayFromInputStream(job2.GetInputStream)
.

THAT IS WHY one has to write the code line by line and do not copy any text.
 
Upvote 0
Top