Android Question Accessing the Web multiple times

SteveAuckland

Member
Licensed User
Longtime User
Hi,

I have played with NetChickens first example in his manual, (see below) and tried to use HttpUtils2. The idea is to type in a word, hit the lookup button and the ap looks up dictionary.com and downloads the definition page.

My problem is this, the first time I use the ap it works as expected, if however I type in a new word it doesn't work at all.
From checking the logs it seems on subsequent trys the job success is false?
If I change Job.Release to Activity.Complete, it still wont work twice.

Getting a bit lost and discouraged here as I'm new to this, any help would be Great

Steve


#Region Project Attributes
#ApplicationLabel: HttpDictionary
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals
Dim DictLookup As HttpJob
Dim SearchButton As Button
Dim WordBox As EditText
Dim StrDict As String
StrDict = "http://dictionary.reference.com/browse/"
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main")
DictLookup.Initialize("HttpUtils",Me)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub SearchButton_Click


DictLookup.Download(StrDict & WordBox.Text)

End Sub
Sub JobDone(Job As HttpJob)
Dim SearchWord As String
If Job.Success Then

SearchWord = Job.GetString
SearchWord = SearchWord.Replace(QUOTE, "'")
SearchWord = SearchWord.SubString(SearchWord.IndexOf("<meta name='description' content=")+34)

SearchWord=SearchWord.SubString2(0, SearchWord.IndexOf("See more"))

Msgbox(WordBox.Text & SearchWord, "")


End If
Job.Release
End Sub
 

SteveAuckland

Member
Licensed User
Longtime User
Please use [ code ] [ /code ] tags (without spaces) when posting code.

What is the error message? It should appear in the logs.

Hi,
There was no error message. When you try the ap a second time, it doesn't fail it just does nothing. What the logs showed was: for the first time it showed the full downloaded file, the information you would see on a webpage if you View Source. The second time it just shows the first few lines of the same information.

From stepping through the code it seems like the Job.success code doesn't get done.

Sorry about the code tags, is there a post that gives an example how to do this?
 
Upvote 0

SteveAuckland

Member
Licensed User
Longtime User
Note that it is better to make the job a process global variable. Though this is not the cause of this error.

Can you upload your project (File - Export as zip)?


Thanks for you time Erel here is the file,
 

Attachments

  • DictLookup.zip
    7.2 KB · Views: 204
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
They reject your request because there is no User-Agent header. Here is an improved version of your code:
B4X:
Sub Globals
   Dim SearchButton As Button
   Dim WordBox As EditText
   Dim StrDict As String
   StrDict = "http://dictionary.reference.com/browse/"
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
End Sub

Sub SearchButton_Click
   Dim DictLookup As HttpJob
   DictLookup.Initialize("HttpUtils",Me)
   DictLookup.Download(StrDict & WordBox.Text)
   DictLookup.GetRequest.SetHeader("User-Agent", "   Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0")
End Sub

Sub JobDone(Job As HttpJob)
   Dim SearchWord As String
   If Job.Success Then
       SearchWord = Job.GetString
     SearchWord = SearchWord.Replace(QUOTE, "'")
     SearchWord = SearchWord.SubString(SearchWord.IndexOf("<meta name='description' content=")+34)
     SearchWord=SearchWord.SubString2(0, SearchWord.IndexOf("See more"))
     Msgbox(WordBox.Text & SearchWord, "")
   Else
     Log(Job.ErrorMessage)
   End If
   Job.Release
End Sub
 
Upvote 0

SteveAuckland

Member
Licensed User
Longtime User
Thank You Erel,

I'm learning all the time, I love the product, but the customer service makes all the difference.

Now to study up on User-Agent headers.

Steve
 
Upvote 0
Top