Android Question HttpUtils2 and job.download2

Hagen Schloemer

Member
Licensed User
Longtime User
Hi!

I have tried the httputils2 and the b4azxing modules to scan ean's and resolve the names of the products by web-database.

the job starts with this:

Job1.Download2("http://www.ean-search.org/perl/ean-search.pl", Array As String("q", lblEAN.Text))

the label "lblEAN" contains the valid ean-code. If i run this i get a "not found" error, but if i open the complete link in a normal browser, i get the rigth answer.

The jobdone looks in moment like this:

Sub JobDone(Job As HttpJob)
ProgressDialogHide

If Job.Success=True Then
Dim result As String
result=Job.Getstring
Dim i1 As Int
i1=result.IndexOf(lblEAN.Text)
Msgbox(i1,"")
Else
Msgbox(Job.ErrorMessage,"FEHLER")
End If

End Sub

What i am doing wrong? Is that the right way to get the result-page into a string?

best regards
Hagen
 

Hagen Schloemer

Member
Licensed User
Longtime User
My own answer.

The reason is the website itself. If i use the deskop (firefox, explorer or crome) everthing is ok, the page runs as expected and delivers the correct answer. Only on the device (Android 4.x) the result is everytime "404-page not found. (the browser on the device is ok, i can see the page correctly). If i use google als url i get the correct html-code. Has anyone a idea why this is the result? I have tested it also whit the http client, same result. The page above makes no redirect, i have set the httpclient parameters to supress it.

best regards
Hagen
 
Upvote 0

Hagen Schloemer

Member
Licensed User
Longtime User
Thank you Erel,

your tip was right, but the header needs more information to return a correct stream:

job1.Download2(link and paramter to site....))
job1.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0")
job1.Getrequest.SetHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
job1.GetRequest.SetHeader("Accept-Language","de-de,de;q=0.8,en-us;q=0.5,en;q=0.3")
job1.GetRequest.SetHeader("Accept-Encoding","utf8")
job1.GetRequest.SetHeader("DNT","1")

So the correct data arrives.

Best regards
Hagen
 
Upvote 0

Harmlos

Member
Licensed User
Longtime User
Hy Hagen,

is It possible to show me all of the Code which is required for this?
I am working at the same problem, but i dont understand how the "HttpUtils2" libary works...
maybe i shut learn a bit HTML first?

Kind regards Harmlos
 
Upvote 0

Harmlos

Member
Licensed User
Longtime User
thanks Erel and Hagen
finaly i have solved my Problem. Maybe its interesting to other people (this code searches for an EAN in the Barcoo-Database and gives back the productname and the best price)
B4X:
Sub btnStart_Click
    Dim job1 As HttpJob
    job1.Initialize("Job1", Me)
    job1.Download("http://www.barcoo.com/" & lblEAN.Text)
End Sub
B4X:
Sub JobDone(Job As HttpJob)
If Job.Success=True Then
    'Nach Klartexbeschreibung und nidrigster Preis des Produkts im runtergeladenen HTML Text suchen
    Dim result As String
    Dim result2, result3, result4 As String
    result=Job.Getstring 'HTML code an result übergeben
    Dim i1, i2 As Int
    'Beschreibung
    i1=result.IndexOf("og:title")'nach "og:title" im Code suchen (dahinter steht die Produktbeschreibung
    result2 = result.SubString(i1+19) 'alles vor der beschreibung abscheiden"
    i2 = result2.IndexOf(">")    'nach ">" im Restcode suchen (steht nach der Produktbeschreibung)
    result3 = result2.SubString2(0,i2-3)'alles hinter der beschreibung abschneiden (nur noch die Beschreibung ist als Text vorhanden
    'Preis
    i1 = result.IndexOf("priceSum")
    If i1 = -1 Then 'wenn kein Preis eingetragen ist dann:
        result4 = "kein Preis"
    Else ' sonst:
        result2 = result.SubString(i1+10)
        i2 = result2.IndexOf("€")
        result4 = result2.SubString2(0,i2+1)
    End If
Msgbox(result3 & ". Niedrigster Preis: " & result4,"")
Else
    Msgbox(Job.ErrorMessage,"FEHLER")
End If
End Sub

Kind regards
Harmlos
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Try to add a User-Agent header: (add a reference to Http library):

I'm kind of confused now :)

I only had Core & HTTPUtils2 (the modded) tagged under libs and the downloads worked fine.

As you notified somewhere that the mod relies on okHTTP I guess it was calling the okHTTP lib as reference from in the library so it worked without having okHTTP tagged.

When I add your setHeader code it refuses to compile unless I tag okHTTP aswell?

When doing that the user agent is being passed fine tho, thanks for that.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
This is an old thread. start a new one. YOU should know that :)
 
Upvote 0
Top