3

I have seen answers to detecting when a URL has finished loading, which I need to know.. but I also need to know if the load was successful.

I need to know this as I am loading an HTML interstitial advert and if it is not loaded then I don't want to display it. I can make my own WebClient and detect onPageFinished, but I noticed that if I switched my internet off, I would still see the onPageFinished being called. So how do I detect that the URL if both finished AND successful?

EDIT: Looking at ObAt's suggestion, and combining that with the most popular answer to this SO question, I'm still a bit stuck. It seems that when I have my internet switched off, the contents of the WebView after onPageFinished appears to be (with my section_id number changed to 9999):

<head><head><script type="text/javascript" src="http://ad.leadboltads.net/show_app_ad.js?section_id=9999"></script></head><body></body></head>

This does not show any obvious errors.

EDIT: Just noticed that I'm doing a LoadData rather than a LoadUrl, I don't know if this makes any difference.

EDIT: If my internet is on and the ad loads successfully, then an example contents of the webview is this:

<iframe name="ap_iframe" width="300" height="250" frameborder="0" src="http://ad.leadboltads.net/show_app_ad?section_id=9999&amp;lang=en-GB&amp;scr_w=480&amp;scr_h=800&amp;
url=data%3Atext%2Fhtml%2C%3Cscript%20type%3D%22text%2Fjavascript%22%20src%3D%22http%3A%2F%2Fad.leadboltads.net%2Fshow_app_ad.js%3Fsection_id%__HIDDEN__script%253E&amp;
referer=" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no"></iframe></body></head>

So, I can now see differences between the two states... and perhaps I could just do a search for "iframe" or "width" or anything I might expect in the successfully loaded page that isn't in the failed page... but it feels like a very non-general and unsatisfactory answer.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mick
  • 8,284
  • 22
  • 81
  • 173

2 Answers2

2

You can check if the data you downloaded doesn't contain any bad HTTP Status Error (A HTTP 200 error mean everything is OK). You can also check if the the downloaded data is not null.

Edit: The successful response is much longer than the unsuccessful request so you can check the length of your response.

if(serverRespons.length > 50){
  // do something
}

Or you can search your response if it contains a attribute you only get when your request is succesfull

String myCheck = "width="
if(serverRespons.toLowerCase().contains(myCheck.toLowerCase()){
  //do something
}

I did not test the code myself, but hopefully it works for you!

Note: I prefer the second way, or a combination :)

ObAt
  • 2,337
  • 3
  • 24
  • 44
  • Looks interesting... presumably I need to use the methods desribed in this SO question http://stackoverflow.com/questions/2376471/how-do-i-get-the-web-page-contents-from-a-webview – Mick Dec 12 '12 at 17:36
  • Can you post an example when your webpage is succesfull loaded? Because I don't know why "http://ad.leadboltads.net/show_app_ad.js?section_id=9999" appears in the source – ObAt Dec 12 '12 at 18:42
  • I've given you an upvote for effort... but I won't mark it as correct because it feels rather dangerous. I suspect that the ad server may change some of its characteristics without notice and the suddenly I will have lots of customers with a broken app. – Mick Dec 12 '12 at 19:07
  • Thank you, if I know a better solution I'll let you know! – ObAt Dec 12 '12 at 19:39
-3

Maybe by setting a webchromeclient and overriding the onprogresschanged method.

There you can check when the progress is 100 to check if it's finished loading.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
ePeace
  • 1,987
  • 1
  • 17
  • 23