Android Question httpjob problem

Status
Not open for further replies.

alireza939

New Member
Hi
i cant download any text file with httpjob.
file download but don't show any things.
when i log from my text file (here is a json file from host) show this Error.
full Error is:
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("d9d83f038e98bf4ef56ef499fd4568c1");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://(example location)";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html> >>>>>Error on line 42.
Capture.PNG
 

JohnC

Expert
Licensed User
Longtime User
It appears the page you are trying to download requires javascript to run, but since you are downloading the page using httpjob and not displaying it in a webview, the javascript in the page can not run.

What I would recommend is to load the page into a webview, then inject/execute a javascript command to extract the HTML from the webview control, then work with that.
 
Last edited:
Upvote 0

alireza939

New Member
It appears the page you are trying to download require javascript to run, but since you downloading the page using httpjob and not displaying it in a webview, the javascript can not run.

What I would recommend is to load the page into a webview, then inject/execute a javascript command to extract the HTML from the webview control, then work with that.


My file is one number in txt document. this one need java script to download?
How extract HTML from webview?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
yeah, all you're doing is downloading a text file. you can't expect anything else to happen after that. put it in a webview:
B4X:
dim webview as webview
activity.addview(webview, 0%x,0%y,100%x,100%y)
webview.loadhtml( res )
and see what you get. (or as johnc says, work with that).

a webview can deal with javascript on its own, so, depending on what's in the rest of the file you downloaded, you might actually get a functioning webpage.

whether or not that page has anything to do with what you think your jobdone sub does is another story. i don't think it does, but first things first.

what you think you downloaded is not json. there is nothing to parse. and whether or not you can actually download anything from the webview will depend on whether you can modify the html text. you will probably need to insert javascript in the webview. that javascript will send data back to b4a which you can parse. but that data is not going to come back as httpjob download. and it will probably not be json. everything depends on what that downloaded html does once you get it into a webview.

another possible option is to understand what the html file does and to query the server directly via httputils. you did that, but you did it with the wrong request. requesting the underlying html has nothing to do with what you are ultimately after. it's what's in the html file that tells you what you need to download.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Ok, if you are trying to download a webpage (that has javascript in it) and then parse the html, this is what I would do.

1) Configure the Webview control to add the chrome client using WebViewExtras lib:
B4X:
wve.addWebChromeClient(wv,  "WVE")    'wve2
2) Enable JavaScript in the Webview:
B4X:
wv.JavaScriptEnabled = True
3) Enable the B4A/JavaScript interface using WebViewExtras:
B4X:
wve.addJavascriptInterface(nwv, "B4A")
4) Instead of using HTTPJob to download the page at the URL, load the page into a webview using wv.LoadURL
B4X:
wv.LoadURL (URL)
5) Once the page has been loaded, run a javascript to extract the HTML:
B4X:
Sub wv_PageFinished (URL As String)

    'run javascript
    Dim Javascript As String
   
    Javascript="B4A.CallSub('GetHTML_Event', false, document.getElementsByTagName('html')[0].innerHTML)"
    wve.executeJavascript(wv, Javascript)
   
End Sub
6) Then capture the HTML:
B4X:
Sub GetHTML_Event(HTML As String)
    'Msgbox(HTML,"HTML")
    Log("HTML=" & HTML)
End Sub
 
Upvote 0

alireza939

New Member
Ok, if you are trying to download a webpage (that has javascript in it) and then parse the html, this is what I would do.

1) Configure the Webview control to add the chrome client using WebViewExtras lib:
B4X:
wve.addWebChromeClient(wv,  "WVE")    'wve2
2) Enable JavaScript in the Webview:
B4X:
wv.JavaScriptEnabled = True
3) Enable the B4A/JavaScript interface using WebViewExtras:
B4X:
wve.addJavascriptInterface(nwv, "B4A")
4) Instead of using HTTPJob to download the page at the URL, load the page into a webview using wv.LoadURL
B4X:
wv.LoadURL (URL)
5) Once the page has been loaded, run a javascript to extract the HTML:
B4X:
Sub wv_PageFinished (URL As String)

    'run javascript
    Dim Javascript As String
  
    Javascript="B4A.CallSub('GetHTML_Event', false, document.getElementsByTagName('html')[0].innerHTML)"
    wve.executeJavascript(wv, Javascript)
  
End Sub
6) Then capture the HTML:
B4X:
Sub GetHTML_Event(HTML As String)
    'Msgbox(HTML,"HTML")
    Log("HTML=" & HTML)
End Sub

Webview show my txt file but httpjob can't get this Value and when log it show this page need javascript
As you see Webview show the number 1 but when i ge
Untitled.png
t job.getstring can't get anything and tell me this page need javascript.
I want to build a program that if my txt file = 1 dont need update and when its 2 download button active.
 
Upvote 0

supriono

Member
Licensed User
Longtime User
i have same error with HttpJob

log:
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("a6130332cd330cda1ae9dca07ce41e7e");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://kelulusansmkn5.phpnet.us/json.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

link : http://kelulusansmkn5.phpnet.us/json.php

everything is oke when use webview
webview:
Sub load_url
    WebViewExtras1.addWebChromeClient(WebView3,"")
    WebViewExtras1.addJavascriptInterface(WebView3, "B4A")
    WebView3.Visible=True
    WebView3.LoadUrl("http://kelulusansmkn5.phpnet.us/json.php")
End Sub

Sub WebView3_PageFinished (URL As String)
    Log("jalankan javascript")
    Dim Javascript As String
    Javascript="B4A.CallSub('ProcessHTML', false, document.getElementsByTagName('html')[0].innerHTML)"
    Log("PageFinished: "&Javascript)
    WebViewExtras1.executeJavascript(WebView3, Javascript)
    Do While( WebView3.Zoom( False ) = True)
    Loop
End Sub

Sub ProcessHTML(Html As String)
    Log("ProcessHTML: "&Html)
    Dim kk As String=Html
    kk=kk.Replace("<html>","")
    kk=kk.Replace("<head>","")
    kk=kk.Replace("<body>","")
    kk=kk.Replace("</head>","")
    kk=kk.Replace("</html>","")   
    kk=kk.Replace("</body>","")
    Log(kk)
    Try
        DateTime.DateFormat = "dd-MM-yyyy"
        Dim TGL As String = DateTime.Date(DateTime.Now)
        Log(TGL)
        Dim parser As JSONParser
        parser.Initialize(kk)
        Dim root As Map = parser.NextObject
        Dim data As List = root.Get("data")
        Log(data)
        For Each coldata As Map In data
            Dim kelas2 As String = coldata.Get("kelas")
            Dim link2 As String = coldata.Get("link")
            Dim waktu2 As String = coldata.Get("waktu")
            Dim jurusan2 As String = coldata.Get("jurusan")
            Dim id2 As String = coldata.Get("id")
            Dim tanggal2 As String = coldata.Get("tanggal")
            If jurusan2="SEMUA 1" And TGL=tanggal2 Then
                ToastMessageShow("SIMULASI 1",True):URL2=link2
            else If jurusan2="SEMUA 2" And TGL=tanggal2 Then
                ToastMessageShow("SIMULASI 2",True):URL2=link2
            Else
                If kelas=kelas2 And jurusan=jurusan2 And TGL=tanggal2 Then
                    Log("INI LINK UJIANYA: "&link2):URL2=link2
                End If
            End If
        Next
    Catch
        Log(LastException)
    End Try
End Sub
 
Upvote 0

supriono

Member
Licensed User
Longtime User
i have same error with HttpJob

log:
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("a6130332cd330cda1ae9dca07ce41e7e");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://kelulusansmkn5.phpnet.us/json.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

link : http://kelulusansmkn5.phpnet.us/json.php

everything is oke when use webview
webview:
Sub load_url
    WebViewExtras1.addWebChromeClient(WebView3,"")
    WebViewExtras1.addJavascriptInterface(WebView3, "B4A")
    WebView3.Visible=True
    WebView3.LoadUrl("http://kelulusansmkn5.phpnet.us/json.php")
End Sub

Sub WebView3_PageFinished (URL As String)
    Log("jalankan javascript")
    Dim Javascript As String
    Javascript="B4A.CallSub('ProcessHTML', false, document.getElementsByTagName('html')[0].innerHTML)"
    Log("PageFinished: "&Javascript)
    WebViewExtras1.executeJavascript(WebView3, Javascript)
    Do While( WebView3.Zoom( False ) = True)
    Loop
End Sub

Sub ProcessHTML(Html As String)
    Log("ProcessHTML: "&Html)
    Dim kk As String=Html
    kk=kk.Replace("<html>","")
    kk=kk.Replace("<head>","")
    kk=kk.Replace("<body>","")
    kk=kk.Replace("</head>","")
    kk=kk.Replace("</html>","")  
    kk=kk.Replace("</body>","")
    Log(kk)
    Try
        DateTime.DateFormat = "dd-MM-yyyy"
        Dim TGL As String = DateTime.Date(DateTime.Now)
        Log(TGL)
        Dim parser As JSONParser
        parser.Initialize(kk)
        Dim root As Map = parser.NextObject
        Dim data As List = root.Get("data")
        Log(data)
        For Each coldata As Map In data
            Dim kelas2 As String = coldata.Get("kelas")
            Dim link2 As String = coldata.Get("link")
            Dim waktu2 As String = coldata.Get("waktu")
            Dim jurusan2 As String = coldata.Get("jurusan")
            Dim id2 As String = coldata.Get("id")
            Dim tanggal2 As String = coldata.Get("tanggal")
            If jurusan2="SEMUA 1" And TGL=tanggal2 Then
                ToastMessageShow("SIMULASI 1",True):URL2=link2
            else If jurusan2="SEMUA 2" And TGL=tanggal2 Then
                ToastMessageShow("SIMULASI 2",True):URL2=link2
            Else
                If kelas=kelas2 And jurusan=jurusan2 And TGL=tanggal2 Then
                    Log("INI LINK UJIANYA: "&link2):URL2=link2
                End If
            End If
        Next
    Catch
        Log(LastException)
    End Try
End Sub
if use HTTPJOB error

httpjob:
Sub GetPage As ResumableSub
    For i = 1 To 10
        Dim j As HttpJob
        j.Initialize("", Me)
        j.Download("http://kelulusansmkn5.phpnet.us/json.php")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Dim res As String = j.GetString
            Log("res:"&res)
            j.Release
            Return res
        Else
            Log("error: " & i)
        End If
        j.Release
        Sleep(3000)
    Next
    Return ""
End Sub
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
As you can see, you can't use HTTPjob to download pages that contain JavaScript.
 
Upvote 0
Status
Not open for further replies.
Top