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,168
  • HttpUtils2.zip
    8.5 KB · Views: 11,493
Last edited:

ArminKH

Well-Known Member
I loaded this project and it will not compile:

B4A version: 5.50
Parsing code. (0.00s)
Compiling code. (0.04s)
Compiling layouts code. (0.00s)
Generating R file. (0.04s)
Compiling debugger engine code. (0.56s)
Compiling generated Java code. Error
B4A line: 16
hc.Initialize(\
javac 1.8.0_66
src\anywheresoftware\b4a\samples\httputils2\httputils2service.java:222: error: cannot access ClientProtocolException
_hc.Initialize("hc");
^
class file for org.apache.http.client.ClientProtocolException not found

What do I need to do to fix this? Thank you.
first of all please start new thread for your question
you should use api 22 or lower to compile your project
please search over forum,you will find some discussion about this issue
 

aedwall

Active Member
Licensed User
Longtime User
I just bought and installed v5.5 - doesn't it come with newer files?

I get this when trying to compile FlickRViewer - more old files?:

B4A version: 5.50
Parsing code. (0.00s)
Compiling code. (0.04s)
Compiling layouts code. (0.01s)
Generating R file. (0.04s)
Compiling debugger engine code. (0.61s)
Compiling generated Java code. Error
B4A line: 54
Log(Response.GetString(\
javac 1.8.0_66
src\anywheresoftware\b4a\samples\flickr\httputils2service.java:163: error: cannot access ParseException
anywheresoftware.b4a.keywords.Common.Log(_response.GetString("UTF8"));
^
class file for org.apache.http.ParseException not found
 

aedwall

Active Member
Licensed User
Longtime User
FlickRViewer now works when I use api 17 instead of 23. Sorry for the bother.
 

aedwall

Active Member
Licensed User
Longtime User
I found this - "Attached is a modified version of HttpUtils2 library (v2.10) that is based on OkHttp instead of Http." I looked at my "HttpUtils2.jar" file and I cannot tell what version it is. So who knows what I have? I also don't know what difference it makes using API23 vs API 22. And I suppose I don't care - too much stuff to keep up with. Seems like if HttpUtils2 is based on OkHttp, then it should be called OkHttp. Confusing to know what is what. Thank you for your help.
 

Oswald

Member
Licensed User
Longtime User
Hi,
I want to take a photo with my mobile device, send that photo to my web server and store it in a folder.
can I do this with HttpUtils2?
does anybody has sample code to do that?
thanks in advance
 

DonManfred

Expert
Licensed User
Longtime User

Oswald

Member
Licensed User
Longtime User
I am totally confused.
with PostFile I can pass the file to the webservice but how can I pass second parameter? the fileneame for example...
and the second question, my webservice is a asp.net platform written in vb.net. How can I get those two parameters from the server side.... :(
sample code for b4a and asp.net would be greatfull...

bravox
 

DonManfred

Expert
Licensed User
Longtime User
the fileneame for example
This will not work with postfile
Use multipartpost instead
I get those two parameters from the server side
using multipartpost you pass the vaues like it was a normal webform.... You just need to do it the same way you get values from a web-form

If i remember correctly i already showed you an example o how to do :D (#368)
 

Oswald

Member
Licensed User
Longtime User
worked!
thanks a lot, I insist to use postfile o_O stupid me...
It works great with multipartpost.
I am sending a png file to the server and I need to parse that stream and get the file name now.

thank again
bravox
 

Oswald

Member
Licensed User
Longtime User
Hi again,
I know this is not the right thread to ask this question but maybe you can help me about the correct one.
My webservice is in asp.net platform and I do not know how to debug it. Because the parameter is coming from the android (file to upload) and I can not pass a file (byte stream) over the browser.
any idea?
 

DonManfred

Expert
Licensed User
Longtime User
I know this is not the right thread to ask this question but maybe you can help me about the correct one.
Create a NEW THread in the B4A-Questions-Forum
My webservice is in asp.net platform and I do not know how to debug it.
I cant help as i dont use asp.net. Search google or look into the documentation of asp.net to find out how to debug.
and I can not pass a file (byte stream) over the browser.
Sure. Create a html-formular with file-uploadfields and then submit the form...
 

Jaime Ramirez

Member
Licensed User
Longtime User
Hi Guys/Erel:

DO you have any sample code to call webservices over HTTPS and Basic Authentication?

Regards

Jaime
 

sule

Member
Licensed User
Longtime User
am I using the latest version ?
'HttpUtils2 version 2.10 (based on OkHttp library)
 
Top