Android Tutorial HttpUtils2 - Web services are now even simpler

HttpUtils2 was replaced with OkHttpUtils2: https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/
Both libraries are included in the IDE.


HttpUtils2 is a small framework that helps with communicating with web services (Http servers).

HttpUtils2 is an improved version of HttpUtils.

The advantages of HttpUtils2 over HttpUtils are:
  • Any number of jobs can run at the same time (each job is made of a single task)
  • Simpler to use
  • Simpler to modify
  • Supports credentials
  • GetString2 for encodings other than UTF8
  • Download2 encodes illegal parameters characters (like spaces)

HttpUtils2 requires Basic4android v2.00 or above.
It is made of two code modules: HttpUtils2Service and HttpJob (class module).
The two code modules are included in HttpUtils2 (attached project).
It depends on the following libraries: Http and StringUtils

How to use
- Dim a HttpJob object
- Initialize the Job and set the module that will handle the JobDone event.
The JobDone event is raised when a job completes.
The module can be an Activity, Service or class instance. You can use the Me keyword to reference the current module.
Note that CallSubDelayed is used to call the event.
- Call one of the following methods:
Download, Download2, PostString, PostBytes or PostFile. See HttpJob comments for more information.
- Handle the JobDone event and call Job.Release when done.
Note that the completion order may be different than the submission order.

To send credentials you should set Job.UserName and Job.Password fields before sending the request.

For example the following code sends three request. Two of the responses will be printed to the logs and the third will be set as the activity background:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim job1, job2, job3 As HttpJob
   job1.Initialize("Job1", Me)

   'Send a GET request
   job1.Download2("http://www.b4x.com/print.php", _
      Array As String("first key", "first value :)", "second key", "value 2"))

   'Send a POST request
   job2.Initialize("Job2", Me)
   job2.PostString("http://www.b4x.com/print.php", "first key=first value&key2=value2")

   'Send a GET request
   job3.Initialize("Job3", Me)
   job3.Download("http://www.b4x.com/forum/images/categories/android.png")
End Sub

Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
      Select Job.JobName
         Case "Job1", "Job2"
            'print the result to the logs
            Log(Job.GetString)
         Case "Job3"
            'show the downloaded image
            Activity.SetBackgroundImage(Job.GetBitmap)
      End Select
   Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
   Job.Release
End Sub

This example and an example of downloading several images from Flickr are attached:

flickr_viewer1.png


Starting from B4A v2.70, HttpUtils2 is included as a library in the IDE.

Relevant links

ImageDownloader - Service that makes it simple to efficiently download multiple images. Note that a simpler "FlickrViewer example" is available there.
DownloadService - Download files of any size with DownloadService. Includes progress monitoring and the ability to cancel a download.
 

Attachments

  • FlickrViewer.zip
    10.7 KB · Views: 9,188
  • HttpUtils2.zip
    8.5 KB · Views: 11,520
Last edited:

metrick

Active Member
Licensed User
Longtime User
How can I save the downloaded images from Job.GetBitmap to File.DirInternal folder on Job3 done? I had tried several methods from the forum and not successful. Thanks in advance.

Case "Job3"
'show the downloaded image
Activity.SetBackgroundImage(Job.GetBitmap)
'save image to File.DirInternal folder
 

RJB

Active Member
Licensed User
Longtime User
Hi,
is there any way of sending an IF-MODIFIED-SINCE get request and checking for the 304 response?
 

javiers

Active Member
Licensed User
Longtime User
Get information website ...

Hello, I am new to B4A.

Would like to obtain data from a web page table (Bizkaia.Net - Salidas Finalizadas) , And failed.

Could you help me with the code? Attached file.

Thank you for your invaluable help.
 

Attachments

  • Salidas.zip
    8.3 KB · Views: 464

RJB

Active Member
Licensed User
Longtime User
I tried the search box at the top of the page, which didn't seem to work so I googled and got back to your quiz #1. This seems to say that you have to make a download request before you can set the header to send an IF-MODIFIED-SINCE request. Is that right?
Can you tell me where I can find this documented? Then I can find the answers myself without making more work for you!
Thanks
 

RJB

Active Member
Licensed User
Longtime User
Thanks, I'll take a look.
In the mean time I've been trying it out. The 304 response comes back as an error in "hc_ResponseError" rather than "hc_ResponseSuccess" though I wouldn't really call it an error.

The line "Log("hc_response: " & Response.GetString("UTF8"))" in "hc_ResponseError" doesn't work as I'm using Android 4 but I assume it does no harm?
 

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
some help is required...

hi,

i am new to here.
i use vb.net and web services
all works perfect
now, i want to use the same web service i use in my .net code
i just use the reference, and then call the functions with parameters - no post etc...

is there a way to do this with b4a ?
i found this quite harder...

i need to access the web service and send & receive data to & from it

thanks
:sign0104:
 

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
here is my problem

i have a full working project that was created with the older version of b4a
now, when installed the new version and trying to run it in emulator i get an error on the web service respond

is there any change between the versions ?
should i do any code modifications ?
i used net addon and keep using the same net file - is there a newer one ?

thanks
 

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Yes.

HttpUtils2 is quite new. There were no major updates to it.

Which error do you get?

strange - running on real phone running like charm
it's only the emulator...
i will do some drilling and then come back...

thanks for your good will
:BangHead:
 
Top