Android Question [Resolved] WebView + WebViewExtras PostUrl

LordZenzo

Well-Known Member
Licensed User
Longtime User
a greeting to the forum
I haven't asked any questions in a while, but now I really need them
I'm doing a small application, you just have to log in, read the HTML and parse it to find some information
I have already done this both using OkHttpUtils and using WebView + WebViewExtras, but today I found a wall, the site I want to access is an RPG, to access I need to POST username and password OkHttpUtils does it well with PostString but the HTML I get is not complete, it lacks all the dynamic parts of the game
WebView + WebViewExtras instead gets a complete HTML but does not perform the POST well, I pass the login only if I have already done it from another browser
for WebView + WebViewExtras I followed @warwound's advice but they don't solve my problem
 

drgottjr

Expert
Licensed User
Longtime User
the posturl method from webviewextras2 should work. exactly how did you implement it? how did it fail? what was the log output? exactly what is the server expecting as posted data? what advice from @warwound are you referring to?
 
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime User
sorry for the delay but I have very little time

B4X:
Sub Class_Globals
    Private mCallBack As Object 'ignore
    Private WebView As WebView
    Private WebExtra As WebViewExtras

    Private fase As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(dd As Object)
    mCallBack=dd
    WebView.Initialize("WebView")

    WebExtra.addWebChromeClient(WebView, "")
    WebExtra.AddJavascriptInterface(WebView,"B4A")

End Sub
Sub INIT(valoreLog As String)
    fase="login"   
'valorelog ="username=user&password=pass"
    WebView.LoadUrl("https://medieval-europe.eu/index.php/user/login?"&valoreLog)
End Sub

this is the code i am using, the result i get is the answer i am not logged in
WebViewExtras ver 1.42
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
your post referred to "WebView + WebViewExtras PostUrl"
where is posturl in your code? webview.loadurl is not the
same as posturl. if you have to post your login
credentials, you need posturl. it will post your login.

whether or not something else may be required, i cannot
say at this time. start with posturl. do you know how to use it?
it's in webviewextras 2. it is not supported in 1.42

it will look something like this:
B4X:
    Dim data() As Byte = "username=user&password=pass".GetBytes("UTF8")
    webviewextras.PostUrl("https://medieval-europe.eu/index.php/user/login", data)

the url you show may or may not be the url which expects posted data. a login usually
is interactive and points to an action which expects posted form data. you need to use
a url which expects posted data. i took a quick look at the url and its source. it is interactive
and it also appears to be the same url which accepts posted data, so it might work.
(i show UTF8, but the server could expect base64. i don't know.) you'll have to work with it.
i've used posturl on servers. if you send them exactly what they want, it works no problem.
 
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime User
thanks for the support
among the various tests I also used OKhttpUtils in this way
B4X:
    Dim htp As HttpJob
    htp.Initialize("fattolog",Me)
    htp.PostString("https://medieval-europe.eu/index.php/user/login",valoreLog)
    Wait For (htp) JobDone(j As HttpJob)
    Process_HTML(j.GetString)
this logs in fine, but the result does not get the complete html code, it lacks the interactive parts

there is a curiosity, if I logged in from the same PC on which I am programming, the WebView on the mobile phone enters the site and returns the right html
 
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime User
now i am trying WebViewExtra2 like this
B4X:
Public Sub Initialize(dd As Object)
    mCallBack=dd
    WebView.Initialize("WebView")
    WebExtra.Initialize(WebView)
    wcc.Initialize("")
    WebExtra.SetWebChromeClient(wcc)

    WebExtra.AddJavascriptInterface(WebView,"B4A")
End Sub
Sub INIT(valoreLog As String)
    fase="login"   

    WebExtra.PostUrl("https://medieval-europe.eu/index.php/user/login",valoreLog.GetBytes("UTF8"))

End Sub

Sub webview_PageFinished (Url As String)
    Dim jsStatement As String

            jsStatement = "B4A.CallSub('Process_HTML', true, document.documentElement.outerHTML)"
            WebExtra.ExecuteJavascript(jsStatement)

    CallSub2(mCallBack, "eseguito",True)
End Sub

the event 'Process_HTML' never triggers
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
yes, i understand - as you do - the differences between
okhttputils and javascript when it comes to capturing
source. different problems, different day. not relevant now

step by step. you're jumping to another problem already.
when you call posturl, exactly what happens? do
you see the expected page in the webview? are you logged
in? yes or no.

(pagefinished does not necessarily behave the way
you are expecting. read google's documentation
carefully. you may have to set a little delay and execute
your javascript without pagefinished. we don't know
that yet. load your page, count to 10 after it looks
loaded and call your javascript snippet.)
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you need to use log entries to tell you what's happening.

when i use posturl with webviewextras 2,
pagefinished fires. in your pagefinished sub,
you need to add log(url & " loaded") so you know
if the event has fired.

then, when you execute your javascript, you simply
assume it worked. where is the captured source
(if any). we see nothing. we need something written
to the log.

as for that callsub2, i don't know what the callback does,
so i can't say if there is a problem there. it doesn't look
right to me the way you have it. any time you have
some sub run following an asynchronous event without
waiting for the result is asking for trouble.

finally, it is almost impossible to debug a webview without
a chrome client. webviewextras 1.42 gives you one.
webviewextras 2 does not exactly give you one. you have
to create one yourself and then set it with webviewextras 2.
you created a dummy chrome client. do you know how to
extend a chrome client with javaobject. not difficult.
why the author chose not to include setting a chrome client
in version 2, i can't say. what i can say is that without a chrome
client you cannot see any errors. this is a big problem.

you did not answer my question regarding posturl.
i will assume it went ok. yes?

again, regarding executing javascript (with a delay), do
a simple test. create a simply javascript alert and
execute that. if you see the alert in the webview, then
you know that executing the js is not the problem.
 
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime User
thank you for your patience, I don't use a web view because I don't need it just read the html the postUrl works I'm almost sure, continuing to search in the forum I found a solution that uses a modified library called webViewExtraArmick unfortunately (so to speak ) I had to leave to go to work, tomorrow I'll do more tests
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…