[WebView 5]: Debugging Web Apps and webviewapps

Source: Internet
Author: User

[WebView 5]: Debugging Web Apps and webviewapps

In the previous article, we learned ([WebView 4]: migrating to the WebView of Android4.4). Today we will continue to learn.

(Blog address: http://blog.csdn.net/developer_jiangqq), reprint please note.

Author: hmjiangqq

Email: jiangqqlmj@163.com

If you are testing your Web Application (Application) in Android4.4 or later, you can use Chrome to remotely debug your Web page, this method also supports earlier Android versions. For more information, see RemoteDebugging on Android.

If you do not have a device of Android 4.4 or a later version, you can use the console JavascriptAPIs to print log messages in logCat to debug Javascript. If you are familiar with using Firebug or Web Inspector, you may also be familiar with using the console (for example, console. log ()). The AndroidWebkit framework also supports many APIs with the same job. When debugging the Android browser or our own WebView, we can retrieve log messages from the Web page. This document describes how to use the APIs console for debugging.

 

(1) Using the Console APIs in the Android Browser (Using Console APIs in theAndroid Browser)

When you call the console method (using the DOM object window. console), logs are printed in logcat. For example, you can execute Javascript code on a webpage:

Console. log ("HelloWorld ");

Then the log information will be printed in logCat:

Console: HelloWorldhttp: // www.example.com/hello.html: 82

The format of the printed message follows the version of Android. In Android2.1, the console information from the Android browser above will contain "browswer ". Under Android1.6, the Android browser information will contain "WebCore ".

The AndroidWebKit framework does not implement all the console information APIs functions of other desktop browsers. However, you can use the following basic log methods:

<span style="font-size:18px;">1.console.log(String)2.console.info(String)3.console.warn(String)        4.console.error(String)</span>

There are also some other console methods that may not work as well as other Web browsers.

 

(2) Using the Console APIs (Using Console APIs in WebView) in WebView)

The console APIs described above also notifies WebView that debugging is supported. If your running device is later than Android, you must provide the WebChromeClient class and implement the onConsoleMessage () method to print logs to the console. then you use the setWebChromeClient () method to bind the WebChromeClient to your WebView.

For example, for Android API 7, the onConsoleMessage (String, int, String) method is implemented:

<span style="font-size:18px;">WebView myWebView = (WebView) findViewById(R.id.webview);myWebView.setWebChromeClient(new WebChromeClient() {  public void onConsoleMessage(String message, int lineNumber, String sourceID) {    Log.d("MyApplication", message + " -- From line "                         + lineNumber + " of "                         + sourceID);  }});</span>

Of course, if your API version is 8 or higher, you can implement the onConsoleMessae (ConcoleMessage) method, for example:

<span style="font-size:18px;">WebView myWebView = (WebView) findViewById(R.id.webview);myWebView.setWebChromeClient(new WebChromeClient() {  public boolean onConsoleMessage(ConsoleMessage cm) {    Log.d("MyApplication", cm.message() + " -- From line "                         + cm.lineNumber() + " of "                         + cm.sourceId() );    return true;  }});</span>

Eaglemanager also provides a MessageLevel object that represents the type of information on the console. You can use the messageLevel () method to determine the emergency severity of log messages and take appropriate measures.

Whether you use onConsoleMessage (String, int, String) or onConsoleMessage (ConsoleManager), Android will call onConsoldeMessage () in a proper place when you execute the console method on the Web page () method so that you can make an error. For example, in the above Code, the printed log is as follows:

HelloWorld -- From line 82 of http://www.example.com/hello.html





How to return button events in ios webview to the app

Use of WebView for IOS Learning

1. Use UIWebView to load webpages
Run XCode 4.3 and create a new Single View Application named WebViewDemo.
2. Load WebView
Add the WebView member variable in ViewController. h and add the implementation in ViewController. m.
The network environment of the mobile phone changes in real time. When the network is slow, how can I prompt that the user's webpage is being opened? How can I prompt users when an error occurs when opening a webpage? At this time, we need to know when the webpage will be opened,
When the loading is complete and when an error occurs. We need to implement the <UIWebViewDelegate> protocol.
3. Modify the implementation protocol in ViewController. h as follows:
Press control + command + up key to switch to the ViewController. m file. Here we enter-(void) webView in the file to see the following implementation method:
Several important functions in UIWebView
1.-(void) webViewDidStartLoad :( UIWebView *) called when the webView page starts loading
2.-(void) webViewDidFinishLoad :( UIWebView *) called when the webView page is loaded
3.-(void) webView :( UIWebView *) webView didFailLoadWithError :( NSError *) Call when error webpage loading error

4. Add NSLog to implement these three methods.
Add
[WebView setDelegate: self]; sets the proxy. In this way, the preceding three methods can be called back.
The three methods are implemented as follows:

Run print:
15:20:29. 728 WebViewDemo [1001: f803] webViewDidStartLoad
15:20:29. 991 WebViewDemo [1001: f803] webViewDidFinishLoad
Let's try the error and turn off wifi and run the following command to print the result:

15:23:58. 939 WebViewDemo [1087: f803] webViewDidStartLoad
15:23:59. 016 WebViewDemo [1087: f803] webViewDidFinishLoad
The request results remain unchanged. Why is network disconnection still successful? Cache? I will try 163.com. This is the real result:
15:24:41. 131 WebViewDemo [1134: f803] webViewDidStartLoad
15:24:41. 149 WebViewDemo [1134: f803] didFailLoadWithError: Error Domain = NSURLErrorDomain Code =-1009 "The Internet connection appears to be offline. "UserInfo = 0x6b41660 {NSErrorFailingURLStringKey = www.163.com/, NSErrorFailingU ...... remaining full text>
 
Use the android webview control to design a browser. If you cannot find a webpage for debugging on your mobile phone, you can use a virtual machine on eclipse. Why?

Virtual machines are more flexible, fast, low-cost, and easy to operate, suitable for automation;
Physical entities are more in line with user scenarios. It is easy to find physical machines with incorrect hardware compatibility to help you find some easy-to-use physical machines. However, physical machines are costly, complex, and difficult to implement and automate;

Baidu is interested in some information, I prefer mobile phone testing, and doing some automation is a great sense of accomplishment.
Real Machine Automation Baidu keyword testing is mainly used for APP testing automation platforms.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.