Android Tutorial HttpUtils - Android web services are now simple!

Status
Not open for further replies.

Inman

Well-Known Member
Licensed User
Longtime User
Sorry, I didn't get you. I am not sure how to initialize the process globals of one module from another module. I see that code modules have a .Process_Globals which is why HttpUtils.Process_Globals works fine. But HttpUtilsService and activity modules do not seem to have one.

Is there another way to initialize a module manually, like how the system handles it when the app is invoked via launcher?
 

Inman

Well-Known Member
Licensed User
Longtime User
This is the Service_Create in HttpUtilsService

B4X:
Sub Service_Create
   If TempFolder = "" Then TempFolder = File.DirInternalCache
   If hcIsInitialized = False Then
      hc.Initialize("hc")
      hcIsInitialized = True
   End If
End Sub

What should I add to this function to initialize the module manually? Sorry I need to keep troubling you and I know you will be busy today with the release of 1.70 and all. But this issue is puzzling me.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try the following:
B4X:
Sub Process_Globals
   Dim hc As HttpClient
   Dim task As Int
   Dim countWorking As Int
   Dim finishTasks As Int
   Dim maxTasks As Int
   maxTasks = 10
   Dim taskToRequest As Map
   Dim TempFolder As String
   Dim Post As Boolean
   Dim PostBytes() As Byte
   Dim PostInputStream As InputStream
   Dim PostLength As Int
   Dim hcIsInitialized As Boolean
   Dim init As Boolean
   init = True
End Sub
Sub Service_Create
   If init = False Then Process_Globals
   If TempFolder = "" Then TempFolder = File.DirInternalCache
   If hcIsInitialized = False Then
      hc.Initialize("hc")
      hcIsInitialized = True
   End If
End Sub
 

Inman

Well-Known Member
Licensed User
Longtime User
Thank you for your patience. Unfortunately that doesn't work. It still freezes the phone. Feels like it is in some never ending loop. But the moment you execute the app with the launcher activity, everything works fine.
 

Inman

Well-Known Member
Licensed User
Longtime User
Yes. Nothing out of the ordinary

B4X:
Starting Job: Job1
** Activity (browser) Resume **
Displayed com.mysite.browserapp/.browser: +320ms
** Service (httputilsservice) Create **
** Service (httputilsservice) Start **
wakelock acquire, uid:10072 at elapsed real time: 8233742
wakelock release, uid:10072 at elapsed real time: 8233778
GC_CONCURRENT freed 374K, 47% free 3483K/6535K, external 0K/0K, paused 6ms+4ms
GC_EXPLICIT freed 164K, 53% free 2713K/5703K, external 0K/0K, paused 49ms
wakelock acquire, uid:1000 at elapsed real time: 8245172
wakelock release, uid:1000 at elapsed real time: 8245223
onUpdate
onUpdate - 103
GC_CONCURRENT freed 435K, 53% free 2796K/5831K, external 151K/512K, paused 4ms+3ms

Like that it goes
 

Inman

Well-Known Member
Licensed User
Longtime User
Just to let you know that I switched to manual implementation of httpclient instead of httputils and everything is working fine now. And fortunately I pass only 1 url at a time so hopefully there won't be a concurrent execution error.

Httputils is very convenient but unfortunately due to some limitation of Android system, it is not suitable for activities that are not invoked via launcher. Thanks for this code module anyway. I will be using it in my next app which is more of a normal one with only 1 major activity that gets invoked via launcher.
 

glouie

Member
Licensed User
Longtime User
PostString - submitting multiple requests as a job?

Thanks for making this helper library. When I first started using httpclient I didn't realize there was a limit to the number of jobs running, I wish Android would queue the jobs.

I was using the httputils to make a series of GET requests submitting them using HttpUtils.DownloadList and this worked great.

Is there a way to submit multiple POST using a job? In a loop if I call a series of Httputils.PostString after the first request the log says "Already working. Request ignored" and the other items never get processed.

Thank you,
Garrick
 

miguelconde

Member
Licensed User
Longtime User
Friends, I am a rookie with B4A, however through the power of this tool I could move my mobile application by 50% in just one week. I have a question about httputils. How do I capture the error message or description when HttpUtils.IsSuccess method is false?. Currently sending a generic message, something like "An error occurred with the server", but I would put the message
that if he could see in the log, in my case I am downloading a file, if not exist on the server, I can see the message in the LOG. "File does not exist", I need to get that message. Thanks for everything and congratulations on eta tool.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Replace the following subs in HttpUtils with

B4X:
Sub IsSuccess(URL As String) As Boolean
   Return SuccessfulUrls.ContainsKey(URL.Replace(" ", "%20"))
End Sub

'Get methods should be called only after the JobDone event or the UrlDone event.
Sub GetString(URL As String) As String
   If IsSuccess(URL) = False Then
      Log("Task not completed successfully.")
      Return ""
   End If
   Return File.GetText(HttpUtilsService.TempFolder, SuccessfulUrls.Get(URL.Replace(" ", "%20")))
End Sub

Sub GetBitmap(URL As String) As Bitmap
   Dim b As Bitmap
   If IsSuccess(URL) = False Then
      Log("Task not completed successfully.")
      Return b
   End If
   b = LoadBitmap(HttpUtilsService.TempFolder, SuccessfulUrls.Get(URL.Replace(" ", "%20")))
   Return b
End Sub

Sub GetInputStream(URL As String) As InputStream
   Dim in As InputStream
   If IsSuccess(URL) = False Then
      Log("Task not completed successfully.")
      Return in
   End If
   in = File.OpenInput(HttpUtilsService.TempFolder, SuccessfulUrls.Get(URL.Replace(" ", "%20")))
   Return in
End Sub

ProcessNextTask in HttpUtilsService

After

B4X:
Dim link As String
   link = HttpUtils.Tasks.Get(task)

Put

B4X:
link=link.Replace(" ", "%20")

This fixes the bug with URLs with spaces in them
 
Last edited:

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'm having trouble queuing large amounts of files. It seems like it only lets you do 2 at a time and won't pull any ones after that off the stack
 

Cableguy

Expert
Licensed User
Longtime User
What would be the correct way to read a page's html sting (source-code)?

I tried to use directly the GetString, but it errors in the device (it compiles fine) complaining that a Map was not initialized...
After having a look at the httputils module, I think the map is never initialized when calling the GetString method!
What am I missing?
 

Cableguy

Expert
Licensed User
Longtime User
That is another problem I have...I cann LOG my phone (SGS)
 

ChrShe

Member
Licensed User
Longtime User
UttpUtils.GetInputStream woes...

1st off, lemme say that I'm super new to Android development and b4a is making my learning experience fantastic...and HttpUtils is making it easier yet!

However, all of the sudden, I'm having trouble with HttpUtils.GetInputStream.

It is failing with "Object should first be initialized (InputStream)". The strange thing is that yesterday, it worked fine. Today, all I did was change the size of some controls.

Here is a code snippet:
B4X:
Sub Globals
    ...
    Dim AnimalsPage As String
    ...
End Sub
Sub Activity_Create(FirstTime As Boolean)
   HttpUtils.CallbackActivity = "Main" 'Current activity name.
    HttpUtils.CallbackJobDoneSub = "ParsePetangoPage"
    ...
End Sub
Sub btnDog_Click
   ProgressDialogShow("Loading Currently Available Dogs...")
   AnimalsPage = "http://www.petango.com/webservices/adoptablesearch/wsAdoptableAnimals.aspx?species=Dog&sex=A&agegroup=All&onhold=A&orderby=ID&colnum=1&AuthKey=5refef2jv1piy179v350n58k5m1l4if77u466z4fy567wv622l&css=http://petango.com/WebServices/adoptablesearch/css/styles.css"
   HttpUtils.Download("ParsePetangoPage", AnimalsPage)
   ...
End Sub
Sub ParsePetangoPage
   ...
   Dim txtrdr As TextReader
   [B]txtrdr.Initialize(HttpUtils.GetInputStream(AnimalsPage))[/B] 'Fails here with exception ("Object should first be initialized (InputStream)") 
   ...
End Sub

Again...this worked yesterday, and if you post that giant url into a browser it works, so I'm at a total loss of what's happening.

Thanks in advance
Chris:BangHead:
 

ChrShe

Member
Licensed User
Longtime User
Never mind...I seem to have lost internet connectivity to my emulator.
Running on a real device works great!!!

Now on to adding in error handling...LOL
 

KashMalaga

Member
Licensed User
Longtime User
Im trying to do a post reply and after that read the source of webpage.

Httputils is really confuse with all those options. would be better create a simple project using every example?

Here is an example about what im talking, and isnt running
 

Attachments

  • kj.zip
    7.7 KB · Views: 388
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…