Do I have an asynchronous error in my code?

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Do I have an asynchronous error in my code?

Can you please check my code? The logic of the code is to go through a list of words and call dictionary.com to see if they are nouns. I run the same code many time and most of the time I get the same number of nouns located but sometimes the number is lower or higher than expected.

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

   Dim hcNewStory As HttpClient
        Dim req As HttpRequest

   Dim intTotalRequestsToComplete As Int

End Sub


Sub Activity_Create(FirstTime As Boolean)

   ' Code using the HTTP library.
   ' Will be used to scrape an internet HTTP page from dictionary.com
   '----------------------------------------------------------------
   hcNewStory.Initialize("hcNewStory")
End Sub


Sub LookupNewStoryWord(strWordToLookup As String, intRequestNumber As Int)

   ' Calls dictionary.com with a word to lookup.
   '--------------------------------------------
   req.InitializeGet("http://dictionary.reference.com/browse/" & strWordToLookup)
   hcNewStory.Execute(req, intRequestNumber)

End Sub


Sub hcNewStory_ResponseSuccess(Response As HttpResponse, TaskId As Int)

   ' This will scrape the web page returned and what part of speach the word is.
   '----------------------------------------------------------------------------
   Dim strEntireWebPage As String
   Dim strSearchWord As String
   Dim blnUseThisWord As Boolean
   Dim strPartOfSpeach As String
            
   strEntireWebPage = Response.GetString("UTF8") 'Get the whole page
   strSearchWord = lstStory.Get(TaskId -1)

   ' The line below finds <div class="pbk"><span class="pg"> 
   ' strEntireWebPage = strEntireWebPage.SubString(strEntireWebPage.IndexOf("<div class=" & QUOTE & "pbk" & QUOTE & "><span class=" & QUOTE & "pg" & QUOTE & ">") + 34) 
            
   ' Replace double quotes with single quotes for easy handling because I am using strings.
   '---------------------------------------------------------------------------------------
   strEntireWebPage = strEntireWebPage.Replace(QUOTE, "'") 

   ' Trim down the entire web page so only the deffinition is left.
   '---------------------------------------------------------------
   strEntireWebPage = strEntireWebPage.SubString(strEntireWebPage.IndexOf("<div class='pbk'><span class='pg'>") + 34) 'Add 34 so we grab the definition after the tag
   strEntireWebPage = strEntireWebPage.SubString2(0, strEntireWebPage.IndexOf("</span>")) 'Grab only the definition enclosed

   ' See if the word is a noun.
   '---------------------------
   If strEntireWebPage.ToUpperCase.Trim = "NOUN" Then

      blnUseThisWord = True
      
      If (strSearchWord.ToUpperCase = "A") _
      OR (strSearchWord.ToUpperCase = "I") _
      OR (strSearchWord.ToUpperCase = "IN") _
      OR (strSearchWord.ToUpperCase = "OFF") _
      OR (strSearchWord.ToUpperCase = "THE") _
      OR (strSearchWord.ToUpperCase = "WAY") _
      OR (strSearchWord.ToUpperCase = "AFTER") _
      OR (strSearchWord.ToUpperCase = "WITH") _
      OR (strSearchWord.IndexOf("ing") > 0) _
      OR (strSearchWord.LastIndexOf("s") > 0) Then
      
         blnUseThisWord = False
      End If
   Else
      blnUseThisWord = False
   End If      
   
   ' Put the nouns into a list.
   '---------------------------
   If blnUseThisWord Then
   
      ToastMessageShow("Processing *** " & lstStory.Get(TaskId -1) & " *** is a: " & strEntireWebPage.ToUpperCase, False)

         lstNouns.Add(strSearchWord)
'      ToastMessageShow("Noun Added", False)
   End If

   intTotalRequestsToComplete = intTotalRequestsToComplete - 1
   
'   ToastMessageShow("Resqests left to complete: " & intTotalRequestsToComplete, False)
   
   If intTotalRequestsToComplete = 0 Then
      
         ProgressBarNewStory.Indeterminate = False
      ProgressBarNewStory.Visible = False
   
      If lstNouns.Size > 0 Then
         GetFunnyReplacementWords
      Else
         Msgbox("Oops. I could not find any nouns. Please try again.", "Make it Funny")
      End If
   End If
End Sub
 

rleiman

Well-Known Member
Licensed User
Longtime User
Here you are.

Thanks.

B4X:
Sub ExtractTheNewStoryWords

   Dim strCurrentWord As String

   ' Show a progress indicator to the user.
   '---------------------------------------
      ProgressBarNewStory.Indeterminate = True
   ProgressBarNewStory.Visible = True
   
   ' Used as a flag to indicate when to proceed to the next parts of code to execute
   ' inside the hc_ResponseSucces sub routine.
   '---------------------------------------------------------------------------------
   intTotalRequestsToComplete = lstStory.Size 

   ' Locate nouns, verbs, etc. from the story.
   '------------------------------------------
   For intLoopCounter = 0 To lstStory.Size - 1
   
      strCurrentWord = lstStory.Get(intLoopCounter)

      LookupNewStoryWord(strCurrentWord, intLoopCounter + 1)
   Next
End Sub

Where is the code that calls: LookupNewStoryWord ?
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Looks like I hit a limit to the amount of requests I can make. If the number of requests I make to .Execute is above 20 then the app crashes.

Is there a way to increase this amount since the user may write a story of maybe 40 or 50 words?
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

Would you be able to upload a sample project that repeatedly calls a web page like I'm trying to do?

I ask because I don't know how to do it correctly.

For example, I'm looking to call a web page maybe 50 times in a loop and proceeding to other coding after the last web page has been called.

Thanks.

You can not increase this limit. HttpUtils take cares of this limit and queues that tasks if there are too much. If you can change your code to use HttpUtils then it will be simpler.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi,

I think that's what I'm looking for.

Do I place my code in the "JobDone" sub routine after the download of the web page?

The HttpUtils example includes a "Job" that sends multiple requests to several web sites. Note that with HttpUtils you need to wait for one job to finish before submitting another one.
 
Upvote 0
Top