Android Tutorial How do I read the content of a basic web page into a text variable?

I've been experimenting with GPS tracking using the api at Tiny Geo-coder | The fastest way to find latitude and longitude

I make a url and send a request to:
http://tinygeocoder.com/create-api.php?g=33.790348,-117.226085

and the site returns a simple web page which is literally actually just the address of the coordinates it received.

I have tried all sorts of things from various posts and examples but can't seem to figure out how to, instead of showing this page in a webview, put it into a text string.

I have been trying to do so with the response from the http server which I now realise is wrong, what I need to do is read all the text from the resulting webpage (which contains only the text I want to display) but am not sure how to.

I know it is possible but I also know I am over complicating this when I am sure it is more simple than I have been thinking. Could anyone point out what I'm missing?

I realise I have asked a few questions in the past few days (I am just getting into the language and jumping right in with testing things) so excuse me if it's a dumb question.

While it would be great if someone could make it really obvious for me if someone can give me maybe a hint or push in the right direction it would be very helpful :)

Dave
Dave
 

susu

Well-Known Member
Licensed User
Longtime User
It's easy. You just use Http lib to request the url with longitude & latitude you want then you read the response, put to edit text... (you can save it to file if you want). You should read Currency example, that may help.
 

mistermentality

Active Member
Licensed User
Longtime User
That's what I tried first but the response was reading as "anywhere software" which is nothing to do with the site I was contacting so have put that idea on hold until I figure it out.

Dave
 

susu

Well-Known Member
Licensed User
Longtime User
You should check the url. I'll write an example for you later. I don't have my PC now.
 

Brad

Active Member
Licensed User
Longtime User
Here's a short example

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
        Dim Url As String
   Dim HttpClient1 As HttpClient
   
   Url = "http://tinygeocoder.com/create-api.php?g="
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim btnFind As Button
   Dim lblResults As Label
   Dim txtLongLat As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        HttpClient1.Initialize("http")
    End If

   Activity.LoadLayout("mainscrn")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub btnFind_Click
   Dim HttpResponse1 As HttpResponse
   Dim request As HttpRequest
 If txtLongLat.Text="" Then
    Msgbox("Please enter long,lat","Error")
   Return
 End If
   LblResults.Text=""
   request.InitializeGet(Url & txtLongLat.Text)
   request.Timeout=30000
   If HttpClient1.Execute(request, 1) = False Then Return
   ProgressDialogShow("Please wait...Searching")
End Sub
Sub Http_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   ProgressDialogHide
   lblResults.Text = Response.GetString("UTF8") 
End Sub
 
Last edited:

mistermentality

Active Member
Licensed User
Longtime User
Thank you for the example. I still had some problems but I tried the code, which was flawless, and adapted it to work with my existing code (took me a while being a newb as I had various errors caused by existing lines of code I had tried but I got there by comparing it with what I had and seeing how it did what it did).

Thanks to the help I now have a nice looking and simple app (only one button) that gets the current gps co-ordinates and shows them on a map in a framed display area with the postal address shown underneath.

I did have to miss out the progress message as when my code was not working I disabled it thinking my code was interfering with it somehow, so I have to recheck it tomorrow as I hope to enable that because it was a nice touch (thank you again :) ) but if any one is curious (as I always am) to see the code I've attached the files as a zip.

I am new to B4A but with the help I have received on the forums I am reasonably pleased with my first app using it, I will be improving it now it's working of course.

It looks best on 320x480 screens as I have not yet designed it for larger resolutions but will be doing that soon :)

Thanks again for everyones help :sign0060:
 

Attachments

  • FindMe.zip
    139.2 KB · Views: 1,063

Brad

Active Member
Licensed User
Longtime User
Great to hear! I downloaded and installed it on my Motorola droid and it works great. One suggestion is to indent your code. Makes it easier to read. Here's a website describing various styles. :)
 

mistermentality

Active Member
Licensed User
Longtime User
Great to hear! I downloaded and installed it on my Motorola droid and it works great. One suggestion is to indent your code. Makes it easier to read. Here's a website describing various styles. :)

Thanks :)

I have made some improvements and added some things, will post my code in the post your examples thread when it's ready and will indent the code to make it easier to read but at moment am having an error with it I cannot pin down the cause of but I will figure it out sooner or later.

Never knew there were so many ways of indenting, that was an eye opener.

Dave
 

GMan

Well-Known Member
Licensed User
Longtime User
:sign0087:
 
Top