Android Question (Solved)WebView Post

iCAB

Well-Known Member
Licensed User
Hi Guys

I am looking for an example that shows how to invoke a login page in webview. The page requires POST and passing the user name and password in json format.

Thanks in advance
iCAB
 

iCAB

Well-Known Member
Licensed User
Why use WebView for this? You should send POST requests with OkHttpUtils2.
Hi Erel,
The requirements are as follows:
  • we must load an existing web page in the browser
  • pass the user's email address
  • the user enters his password and clicks login
  • if login is successful, the page returns a security token as a cookie
  • we must capture the token for all future communications (which will be using OkHttpUtils)
That's why I was thinking that Webview is the way to go

Thanks
iCAB
 

mc73

Well-Known Member
Licensed User
Though I'm not using it any more (replaced such requests with okHttpUtils2), here's how I did it in the past:
B4X:
Private tempPostUrl As String="yourURL"
Private tempPostString As String="yourPostData after encoding URL"
Private bytesOfPOstRequest() As Byte
bytesOfPostRequest=tempPostString.GetBytes("UTF8")
Private jo As JavaObject=yourWebView
jo.RunMethod("postUrl",Array As Object(tempPostUrl,bytesOfPostRequest))
 

iCAB

Well-Known Member
Licensed User
Though I'm not using it any more (replaced such requests with okHttpUtils2), here's how I did it in the past:
Thanks a lot, sounds good I will give it a try.

One question with regards to the above. The code says "after encoding the URL"

B4X:
Private tempPostUrl As String="yourURL"
Private tempPostString As String="yourPostData after encoding URL"
I see that POST data being encoded
B4X:
Private bytesOfPOstRequest() As Byte
bytesOfPostRequest=tempPostString.GetBytes("UTF8")
At what point tempPostUrl is being encoded?

Thanks
iCAB
 

mc73

Well-Known Member
Licensed User
Thanks a lot, sounds good I will give it a try.

One question with regards to the above. The code says "after encoding the URL"

B4X:
Private tempPostUrl As String="yourURL"
Private tempPostString As String="yourPostData after encoding URL"
I see that POST data being encoded
B4X:
Private bytesOfPOstRequest() As Byte
bytesOfPostRequest=tempPostString.GetBytes("UTF8")
At what point tempPostUrl is being encoded?

Thanks
iCAB
You have to use stringUtils (lib) for this:
B4X:
Private su As StringUtils
Private tempPostString as string=su.EncodeUrl(originalPostString,"UTF8")
 
Top