언어 설정

Menu
Sites
Language
WebView Page Load Errors

Hello,I am an Android dev & new here so please pardon my ignorance if any :-)

How can I listen to page load errors like say 404s, 500s and so on.. on a webview and possibly take appropriate action like show a local html file instead of the default error message shown by a WebView? 

Something like on_web_url_load_error Using the sample WebView available at the forums/code samples.

Thank you!

답변 바로가기

Responses

13 댓글
Iqbal Hossain

Hi~

You can try this example. I tested, it works fine!

It is with source code and documentation. 

link: https://developer.tizen.org/community/tip-tech/webview-native-application

 

-Thanks

Karanja Kiniaru

Hi Iqbal,

Thanks for the response.

The referenced sample is the one I have been using but the Page Load Errors(how to listen to & catch) are not there in .

Maybe to explain more what I mean is For instance we have the domain URL as developer.tizen.org then instead of that say for some reason I input d.tizen.org which is presumably non exsistent.

Expected: Throws an error say a 404, Instead of showing the default 'page not found error' on the webview I need to be able to catch that case and do something else like show some custom 404 page from my apps local html files (This part I have covered).

I hope I have explained it exhaustively.

Iqbal Hossain

For d.tizen.org i got "This Web page is not available" in emulator. I am using Tizen IDE SDK 2.4.0_Rev8

Thanks

Karanja Kiniaru

Yes that's correct.

But I need to get that error in code and take appropritate action.

In short looking for an equivalent of what would be in Android -

onReceivedError
webView.setWebViewClient(new WebViewClient(){

@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
   // Do something
   }
});
Iqbal Hossain

Hi~

If you can't find the status code from WebView you can check it using Curl.  Based on that status you can perform your further actions. 

http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html

CURLINFO_RESPONSE_CODE

Pass a pointer to a long to receive the last received HTTP or FTP code. This
option was known as CURLINFO_HTTP_CODE in libcurl 7.10.7 and earlier. This 
will be zero if no server response code has been received. Note that a 
proxy's CONNECT response should be read with CURLINFO_HTTP_CONNECTCODE 
and not this. 
curl_code = curl_easy_perform (session);
long http_code = 0;
curl_easy_getinfo (session, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code == 200 && curl_code != CURLE_ABORTED_BY_CALLBACK)
{
         //Succeeded
}
else
{
         //Failed
}

 

Let me know if it works for you. 

Thanks

Karanja Kiniaru

Hi thanks was finally able to get the errors after your suggestions above.

I will paste what worked for me here incase someone is looking to do this in future.
 

CURL *curl;
CURLcode res;

	curl = curl_easy_init();
	if (curl) {
		curl_easy_setopt(curl, CURLOPT_URL, YOUR_URL_HERE);
//		curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L); --for GET requests
		curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
		/* Perform the request, res will get the return code */
		res = curl_easy_perform(curl);
		long http_code = 0;
		curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
		/* Check for errors */
		if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK) {
			//Succeeded
			dlog_print(DLOG_ERROR, "ERROR-->", "app_main() SUCCESS. msg = %d",
					http_code);
		} else {
			//Failed
			dlog_print(DLOG_ERROR, "ERROR-->", "app_main() FAIL. err = %d",
					http_code);
		}

That said I have now ran into new problems, how can I load a local html file on the Webview Sample provided i.e using C the sample here is in C++

Iqbal Hossain

Glad to know that !

But if my answer solved your problem, please mark it as best anwer incase someone is looking to do this in future. Thanks

Mark as answer
Yasin Ali

Hi~,

From this link https://developer.tizen.org/community/tip-tech/webview-native-application within the code portion of

"static void create_base_gui(appdata_s *ad)", change/edit with this code fragment

    		/* WebView */
			Evas *evas = evas_object_evas_get(ad->win);
			ad->web_view = ewk_view_add(evas);
			// ewk_view_url_set(ad->web_view, elm_object_text_get(ad->entry) );

			CURL *curl;
			CURLcode res;
			curl = curl_easy_init();
			if (curl) {

				//curl_easy_setopt(curl, CURLOPT_URL, "www.tizen.org/ghj");
				curl_easy_setopt(curl, CURLOPT_URL,
						elm_object_text_get(ad->entry));

				//		curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L); --for GET requests
				curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
				/* Perform the request, res will get the return code */
				res = curl_easy_perform(curl);
				long http_code = 0;
				curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
				/* Check for errors */
				if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK) {
					//Succeeded
					dlog_print(DLOG_VERBOSE, "ERROR-->",
							"app_main() SUCCESS. msg = %d", http_code);
					ewk_view_url_set(ad->web_view,
							elm_object_text_get(ad->entry));

				} else {
					//Failed
					dlog_print(DLOG_VERBOSE, "ERROR-->",
							"app_main() FAIL. err = %d", http_code);
					char html[99999] = "<!DOCTYPE html><html>"
							"<body>\" Page Not Found \""
							"</body></html>";
					ewk_view_html_string_load(ad->web_view, html, NULL, NULL);

				}

			}

			my_table_pack(table, ad->web_view, 0, 2, 3, 8);

After that give a wrong entry like "elm_object_text_set(ad->entry, "http://www.tizen67.org");" in "static void create_base_gui(appdata_s *ad)".

Check to see "Page Not Found" error.

If you find it helpful please mark it as best answer.

Thanks

 

 

 

 

 

Karanja Kiniaru

Thanks Bro! 

You are the real MVP! :-D.

As I mark this as the Best answer, one last quick one...what if I want to load an image(in the resources) in the static html  c string format, 

How can I achieve this?

Currently using this

                           "<!DOCTYPE html><html>"
                            "<body>"
                            "<div ><img src=\"/opt/usr/apps/org.example.basicui/res/error.png\" /></div>"
                            "</body></html>"

 but it wont work :(

Yasin Ali

Hi~,

Thanks. But I think we should discuss this topic in another thread,
so that other people may find it easily if they face similar problem
like you. So, after selecting useful answer please open a new thread,
and post your current problem there.

 

Karanja Kiniaru

Hi Yasin Ali,

Thanks for that, I believe that last matter should be tackled here  if possible in the interest of maintaining context as it would call for posting this whole back story in a new thread to try and bring potential help upto speed with the foregoing matter.

Thanks again for your help.

Yasin Ali

Hi~,

You may try

ewk_view_html_string_load(ad->web_view, "<!doctype html><body>Hello <img src=\"./res/logo.png\"></body>", "file:///workspace/project/../", NULL);

Images referenced in the HTML document are located relative to a baseUrl.

Thanks.

Karanja Kiniaru

AWESOME! Works like a charm.

This is what worked for me incase someone needs this in future
 

ewk_view_html_string_load(ad->web_view, html,"file:///opt/usr/apps/PACKAGE_NAME_HERE/", NULL);



Many thanks Yasin Ali!