how to use libraries within B4A

moster67

Expert
Licensed User
Longtime User
I am already playing around with B4A and I wanted to test retrieving the content from a web-page using the included HTTP-library.

How do we use libraries within B4A? I had a look at the tutorials posted so far but I couldn't see this mentioned. Would it be possible to post a short code-snippet showing how to access the HTTP-library in the current version of B4A? Or perhaps, in this moment we can only see the HTTP-library in the IDE but since it is a very early beta, we are not yet able to use them?
 

moster67

Expert
Licensed User
Longtime User
Hmm...I think I figured out how to initialize the HTTP-library in B4A. Seems to be similar to how you do it in VB.Net. I noted that the HTTP-library has 3 classes/objects, HttpClient, HttpRequest and HttpResponse whereas in B4PPC we had two (WebRequest and WebResponse).

The test-program compiled so I guess I managed to initialize the libraries but I got an error executing it. That's probably because I don't know yet how to use them but that's another story.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Log("My Test")
   
   Dim myRequest As HttpRequest
   Dim myResponse As HttpResponse
   Dim myClient As HttpClient
   URL = "www.google.com"
   myValue = ""

   myRequest.InitializeGet(URL)
   myclient.Execute(myrequest,1) 'what is TaskID for?
   myvalue=myresponse.GetString("utf-8")   'error NullPointerException
 

   Msgbox(myValue,"myHTTPTest")
   
End Sub
 
Last edited:
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
Hi there,

Just tried playing around with the HTTP library.

For those who are not aware (as I was until about 30 minutes ago), at the bottom right of the IDE, there is a tab for "Libraries".

Click that tab and select the HTTP library there. This will then give us access to the 3 classes as mentioned by the thread starter.

I managed to work this one out.

Following is my code:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   Dim hc As HttpClient
   Dim hrp As HttpResponse
   Dim hrq As HttpRequest
   'In the designer also add the following 3
   Dim Button1 As Button 
   Dim EditText1 As EditText 'this is the url, it MUST start with http://
   Dim EditText2 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layout")
    hc.Initialize("hcstart") 'hcstart will be the prefix of our response, see below
   
End Sub

Sub Button1_Click
   hrq.InitializeGet(EditText1.Text) 
   hc.Execute(hrq, 2) 'fetches the url, remember the http://
   
End Sub

Sub hcstart_ResponseError (Reason As String, StatusCode As Int, TaskId As Int)
   Msgbox("fail:" & reason, StatusCode)
End Sub

Sub hcstart_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Msgbox(Response, "success")
   EditText2.Text = Response.GetString("utf-8")
   
End Sub

Hope the above helps... and let me know if i am doing it wrong somewhere...

Woohoo! One of the best reasons to be up at 2.15 am!!
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
@ssg

Many thanks! Works very well. I appreciate it alot. :icon_clap:

Now, I am in a position to start porting, at least partially, a project of mine from B4PPC to B4A. This will surely help to get used to the new syntax.

PS: It also took me a while to find the library-tag :)
 
Upvote 0

addi

Member
good work

thanks,nice example!must i put the url as edittext1?also does it get and display all the content or do it just est an http connection to the url?
 
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
thanks,nice example!must i put the url as edittext1?also does it get and display all the content or do it just est an http connection to the url?

Hi addi,

No, you do not need to put the url as edittext1, you can put it any way you want it. My example just used the default naming convention and to provide a way to key in the url manually.

The result displays all the content in the 2nd edittext field. It actually returns the entire html code.

Refer to the example given by Erel... much better documented :sign0089:
 
Upvote 0

addi

Member
works well

Hi addi,

No, you do not need to put the url as edittext1, you can put it any way you want it. My example just used the default naming convention and to provide a way to key in the url manually.

The result displays all the content in the 2nd edittext field. It actually returns the entire html code.

Refer to the example given by Erel... much better documented :sign0089:

Thanks works well..Html code gets displayed in EditText2.So I wonder if you could let is displayed as if you would at it via a browser?:sign0142:
 
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
LOL... i guess you'd need to launch the browser to show the html perhaps? :)

I guess the main idea of the http library is more to read XML replies and formatting it to your needs. Example of usage would be for a weather application, or an RSS reader.
 
Upvote 0

addi

Member
LOL... i guess you'd need to launch the browser to show the html perhaps? :)

I guess the main idea of the http library is more to read XML replies and formatting it to your needs. Example of usage would be for a weather application, or an RSS reader.

Ohk so i would need then to invoke default browser.I want to read content from a clasifed website.should i then need to read content via Rss feeds?
 
Upvote 0
Top