Android Question Need Help With HttpUtils

androit

Member
Licensed User
Longtime User
Hi, I'm trying to download a webpage to save as string using HttpUtils. I'm using 2 modules that I've downloaded: HttpUtils.bas and HttpUtilsService.bas. When I run it I get this error:

An error occurred:
(Line: xx) End Sub
java.lang.ArrayIndexOutOfBoundsException: 0

... and the cursor ends up at the End Sub of Activity_Resume, prompting me to think the problem has something to do with that subroutine.

Excuse me, this problem might be a simple bug but I'm new to B4A, so here's my simplified code:

B4X:
Sub Process_Globals
    Dim sServerFile As String
    sServerFile="http://www.MyUrl.com"
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime=True Then
        Activity.LoadLayout("View1")
        HttpUtils.CallbackActivity="Main"
        HttpUtils.CallbackJobDoneSub="JobDone"
        PingWebsite
    End If
End Sub

Sub PingWebsite
    HttpUtils.Download("ping",sServerFile)  
End Sub

Sub JobDone(Job As String)
    Dim bSuccess As Boolean
    bSuccess=HttpUtils.IsSuccess(sServerFile)
    If bSuccess=True Then      
        Msgbox("Success!","")      
    End If
End Sub

Sub Activity_Resume
    If HttpUtils.Complete = True Then JobDone(HttpUtils.Job)
End Sub

Thanks in advance for your help.
 
Last edited:

qsrtech

Active Member
Licensed User
Longtime User
First you should use the httputils2 library which i think is part of the b4a package now, if not find it and download it. here's how to start it:

B4X:
    Dim http As HttpJob
    http.Initialize("jobname",Me) 'change jobname to anything you want to isolate it in the jobdone event later
    http.Download(myserver & "/" & myfile) 'include full web address plus the file name
 
Upvote 0

androit

Member
Licensed User
Longtime User
Thanks for your reply qsrtech.

What I'm trying to do is sort of follow Erel's example of how to use the httputils2 module: http://www.b4x.com/android/forum/threads/httputils-android-web-services-are-now-simple.9176/

I'm not sure where your example of http:Initialize("jobname",Me) would be located? Also I should mention that the "webpage" I'm trying to download is actually a cgi script that sends the html code. Does that make a difference on how this is done?


 
Upvote 0

qsrtech

Active Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Dim sServerFile As String
    sServerFile="http://www.MyUrl.com"
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime=True Then
        Activity.LoadLayout("View1")
        PingWebsite 'id probably move this outside of the "firsttime" block so everytime the app is (re)started it pings
    End If
End Sub

Sub PingWebsite
    Dim http As HttpJob
    http.Initialize("Ping",Me)
    http.Download(sServerFile & "/" & sServerFile) 'make sure the sserverfile var is set to the file name of the script page
End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success Then
        Select Case Job.JobName.ToLowerCase
            Case "ping"
                Msgbox("Success!","")      
            Case Else
        End Select
    Else
    End If
    Job.Release
End Sub

Sub Activity_Resume

End Sub
 

Attachments

  • httptest.zip
    5.9 KB · Views: 254
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…