B4J Question OkHttpUtils2 issue with Non-UI app

magarcan

Active Member
Licensed User
Longtime User
Here is the code:
B4X:
        Dim j As HttpJob
        j.Initialize("", Me)
        j.Download("https://www.google.com")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Log(j.GetString)
        End If
        j.Release

Same code runs without any problem within B4XPages but itdoesn't using Console (Non-UI).

Does it makes any sense?
 

DonManfred

Expert
Licensed User
Longtime User
 
Upvote 0

magarcan

Active Member
Licensed User
Longtime User
Thank you for the anser, but as I understood
A handler is a B4J class that is mapped to a URL. A handler class is responsible for getting the request and providing the response.

The fact is that I'm not working as a server, just a little console program without GUI.
 
Upvote 0

magarcan

Active Member
Licensed User
Longtime User
B4X:
Sub AppStart (Args() As String)
    Dim tr As TextReader
    tr.Initialize(File.OpenInput(File.DirApp, Args(0)))
        
    Dim line As String
    line = tr.ReadLine

    Do While line <> Null
        line = tr.ReadLine
            
        Dim j As HttpJob
        j.Initialize("j", Me)
        j.Download(line)
        Log("Downloading: " & line)
        StartMessageLoop
    Loop
    tr.Close
    ExitApplication2(0)   
End Sub

B4X:
Sub JobDone(Job As HttpJob)
    If Job.Success Then
        Log(Job.GetString)
        Log("ENDDDDDDDDDDDD")
        '...
    Else
        Log(Job.ErrorMessage)
    End If
    Job.Release
    StopMessageLoop
End Sub

But I get an error just before download second file:
Ha ocurrido un error en la línea: 177 (HttpJob)
java.lang.NullPointerException: url == null
at okhttp3.Request$Builder.url(Request.java:132)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest.InitializeGet(OkHttpClientWrapper.java:392)
at b4j.example.httpjob._download(httpjob.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:78)
at b4j.example.main.main(main.java:29)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Two "code smells" ( [B4X] "Code Smells" - common mistakes and other tips ):
1. JobDone sub.
2. TextReader.

B4X:
Sub AppStart (Args() As String)
  Download
  StartMessageLoop
End Sub

Sub Download
 Dim lines As List = File.ReadList(...)
 For Each line As String In lines
       Dim j As HttpJob
        j.Initialize("", Me)
        j.Download(line)
        Wait For (j) JobDone (j As HttpJob)
         If j.Success Then
             ...
         End If
         j.Release
 Next
 ExitApplication
 
Upvote 0
Top