Android Question Webview LoadHtml include images and files

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Dealing with this website:
https://qrisk.org/three/

I need to run this calculator to compare it to my own calculator (using the provided algorithm).
I can set all the values in code and run the calculation with:
WVExtras1.executeJavascript(WebView1, strJavaScript)
However this appears slow and somewhat unreliable, do I thought a better way might be to download
the webpage source, alter it to set the values, load the http to a webview and then run the calculation, eg:

B4X:
Sub TestWebPageDownLoad
 
 Dim strHttp As String
 
 Dim rs As ResumableSub = GetHttpFromURL(strQR3WebCalculator)
 Wait For (rs) Complete (strHttp As String)

 strHttp = strHttp.Replace($"<IMG width=100 border="0" src="clinriskLogoTransparent.png">"$, "") 'don't need this image
 strHttp = strHttp.Replace("selected>non-smoker", ">non-smoker")
 strHttp = strHttp.Replace(">moderate smoker (10 to 19)", "selected>moderate smoker (10 to 19)")
 
 If pnlWebView.IsInitialized = False Then
  Activity.LoadLayout("ShowWebView")
  arrPanels(Enums.ePanelType.ShowWebview) = pnlWebView
 End If
 
 WebView1.LoadHtml(strHttp)
 
 GotoPanel(Enums.ePanelType.ShowWebview, False, False)

'etc. etc.
 
End Sub

Sub GetHttpFromURL(strURL As String) As ResumableSub
 
 Dim j As HttpJob
 Dim strHttp As String
 
 j.Initialize("", Me)
 j.Download(strURL)
 
 Wait For (j) JobDone(j As HttpJob)
 If j.Success Then
  strHttp = j.GetString
 End If
 
 j.Release
 
 Return strHttp
 
End Sub

Setting the controls works fine, but the calculation can't run:
The web page at: file:///three/index.php could not be loaded because:
net::ERR_FILE_NOT_FOUND

Not sure what file this refers and how to fix this.
I am completely new to http and net stuff in general.
Not sure if downloading all the images (not many) and showing these in the webview
would make a difference.
Any idea how to tackle this?
Could I avoid loading to a webview and submitting the altered html directly?

RBS
 

mc73

Well-Known Member
Licensed User
Longtime User
There are some ways of dealing with such situation, I usually go as shown in the example I attach. It's not the best, this I can be sure of, but it can offer a solution.
It uses okHttpUtils2 for sending the request.

Note: I've just noticed that I should set the "sex" param value to 0 for the female option, instead of 2.
 

Attachments

  • b4aQRiskThree.zip
    10.9 KB · Views: 190
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
There are some ways of dealing with such situation, I usually go as shown in the example I attach. It's not the best, this I can be sure of, but it can offer a solution.
It uses okHttpUtils2 for sending the request.

Note: I've just noticed that I should set the "sex" param value to 0 for the female option, instead of 2.

That looks very good indeed and thanks for that!

I wasn't aware that you could post something like this:
Private postData As String=$"age=${age}&sex=${sex}&ethnicity=${ethnicity}&postcode&smoke_cat=0&diabetes_cat=0&rati&sbp&sbps5&height&weight&calculate=Calculate risk"$

I can provide all the values from my app, so I don't have to set any web controls and just post the complete string of values.
Only need to get the final value and I can do that with JavaScript and WebExtras.

Will try it out now.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
There are some ways of dealing with such situation, I usually go as shown in the example I attach. It's not the best, this I can be sure of, but it can offer a solution.
It uses okHttpUtils2 for sending the request.

Note: I've just noticed that I should set the "sex" param value to 0 for the female option, instead of 2.

Some problems with the postdata string.
There are no errors but I get too low a score, for example:

B4X:
  strPostData = "&age=60&sex=0&ethnicity=1&postcode=WV6 8JW&smoke_cat=0&diabetes_cat=0&" & _
       "fh_cvd=1&b_renal=0&b_AF=0&b_treatedhyp=1&b_migraine=0&b_ra=0&b_sle=0&b_semi=0&" & _
       "b_atypicalantipsy=0&b_corticosteroids=0&rati=5.0&sbp=130&sbps5=6.41&" & _
       "height=171&weight=103&calculate=Calculate risk"

This gives score of 8.2 and should be 13.2.

Above is the log of postdata, the actual code is like this:

B4X:
  If RS1.GetInt2(2) = 1 Then
   strPostData = $"&age=${RS1.GetInt2(1)}&sex=${RS1.GetInt2(2)}&ethnicity=${(RS1.GetInt2(22)+1)}&postcode=${RS1.GetString2(3)}&smoke_cat=${RS1.GetInt2(22)}&diabetes_cat=${RS1.GetInt2(21)}&fh_cvd=${RS1.GetInt2(4)}&b_renal=${RS1.GetInt2(10)}&b_AF=${RS1.GetInt2(6)}&b_treatedhyp=${RS1.GetInt2(7)}&b_migraine=${RS1.GetInt2(8)}&b_ra=${RS1.GetInt2(9)}&b_sle=${RS1.GetInt2(5)}&b_semi=${RS1.GetInt2(11)}&b_atypicalantipsy=${RS1.GetInt2(12)}&b_corticosteroids=${RS1.GetInt2(13)}&rati=${RS1.GetDouble2(17)}&sbp=${RS1.GetInt2(15)}&sbps5=${RS1.GetDouble2(16)}&height=${iHt}&weight=${iWt}}&b_impotence2=${RS1.GetInt2(14)}&calculate=Calculate risk"$
  Else
   strPostData = $"&age=${RS1.GetInt2(1)}&sex=${RS1.GetInt2(2)}&ethnicity=${(RS1.GetInt2(22)+1)}&postcode=${RS1.GetString2(3)}&smoke_cat=${RS1.GetInt2(22)}&diabetes_cat=${RS1.GetInt2(21)}&fh_cvd=${RS1.GetInt2(4)}&b_renal=${RS1.GetInt2(10)}&b_AF=${RS1.GetInt2(6)}&b_treatedhyp=${RS1.GetInt2(7)}&b_migraine=${RS1.GetInt2(8)}&b_ra=${RS1.GetInt2(9)}&b_sle=${RS1.GetInt2(5)}&b_semi=${RS1.GetInt2(11)}&b_atypicalantipsy=${RS1.GetInt2(12)}&b_corticosteroids=${RS1.GetInt2(13)}&rati=${RS1.GetDouble2(17)}&sbp=${RS1.GetInt2(15)}&sbps5=${RS1.GetDouble2(16)}&height=${iHt}&weight=${iWt}&calculate=Calculate risk"$
  End If

strPostData is all on one line as been unable sofar to make it multi-line.

Not sure if the postcode needs single quotes.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Try to remove & in age parameter since its first parameter.

I started without the initial & as suggested by mc73. Taken it off again, but it makes no difference, still gives me the same 8.2 for the
mentioned parameters. It works with or without the initial & as the provided age alters the score either
Problem is with the checkbox values as the 1 or 0 make no difference. Have tried with true or false and with checked, but still not
seeing them making any difference.

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
I started without the initial & as suggested by mc73. Taken it off again, but it makes no difference, still gives me the same 8.2 for the
mentioned parameters. It works with or without the initial & as the provided age alters the score either
Problem is with the checkbox values as the 1 or 0 make no difference. Have tried with true or false and with checked, but still not
seeing them making any difference.

RBS

Fixed this now. Checkbox values should only be posted if they are checked and the value should be on.
Got the right score now.

B4X:
  strPostData = "age=60&sex=0&ethnicity=1&postcode=WV6 8JW&smoke_cat=0&diabetes_cat=0" & _
       "&fh_cvd=on&b_treatedhyp=on" & _
       "&rati=5.0&sbp=130&sbps5=6.41" & _
       "&height=171&weight=103&calculate=Calculate risk"

RBS
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
This is always the case with html checkboxes. Unchecked are not in the post params, this is why in PHP we check for
B4X:
isset($_POST['checkbox']
.
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
This is always the case with html checkboxes. Unchecked are not in the post params, this is why in PHP we check for
B4X:
isset($_POST['checkbox']
.

OK, not sure what that does or if I have to use it, but in any case, thanks for getting me on the right path.
The approach with HttpJob and PostString is much better than loading the data in a Webview, setting the controls with JavaScript etc.
I don't need the Webview for this now, only for the disclaimer.

RBS
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
OK, not sure what that does or if I have to use it, but in any case, thanks for getting me on the right path.
The approach with HttpJob and PostString is much better than loading the data in a Webview, setting the controls with JavaScript etc.
I don't need the Webview for this now, only for the disclaimer.

RBS
This was just a piece of php code, nothing to do with B4X, it was given purely to show what to expect when posting (or not) a checkbox.
 
Upvote 0
Top