APP webview and inline web interaction implementation

Source: Internet
Author: User

The logic of implementation is generally the case, the APP's webview can intercept the requested link address, and then request the content by agreeing to the request prefix (e.g., webjs2app://) with the inline interface.

The requested content is as follows:

{"functionname": "SayHello", "args": ["haha"], "success": "Onsuccess", "Error": "OnError"}

is a Json string , including the message has a call to the application interface method name, pass parameters, call the successful callback after the JS method name, call failed callback after the JS method name. Abstract very in place, can do universal.

final Web request interface address such as:webjs2app://{"functionname": "SayHello", "args": ["haha"], "success": "Onsuccess", " Error ":" OnError "},the APP WebView received the request address from the webjs2app:// , it will parse out the following request content ... On the code.

just link inside already have The code for IOS and the Web is up, and the instructions are clear. Let me add the corresponding implementation of the Android side.

The first step is to rewrite the shouldoverrideurlloadingto intercept the agreed request.

Private String Protocolprefix = "webjs2app://"; This prefix should be lowercase, because WebView will automatically turn the request protocol type into lowercase.

Mwebview.setwebviewclient (New Webviewclient () {

@Override

public boolean shouldoverrideurlloading (WebView view, String URL) {

return Processurl (URL);

}

....

}

The second step is to parse the request interface data

Private Boolean processurl (String URL) {

int i = Url.indexof (Protocolprefix);

System.out.println (URL);

if (Url.indexof (protocolprefix) = = 0) {

Strip protocol from the URL. We'll get input to call a native method

url = url.substring (Protocolprefix.length ());

Decode the URL string

HashMap callinfo = jsonutil.read (URL, hashmap.class);

if (Callinfo = = null) {

TODO: Prompt Call resolution failed

return false;

}

Get function name. It is a required input

Object functionname = Callinfo.get ("functionname");

if (functionname = = null) {

TODO: prompt not found calling method

return false;

}

Object success = Callinfo.get ("Success");

Object error = Callinfo.get ("error");

Object args = Callinfo.get ("args");

Callnativefunction (String) functionname, args, success, error);

return false;

}

return true;

}

The third step is to use Java reflection to invoke the interface.

/**

* Method Interface Invocation

*

* @param functionname

* @param args

* @param success

* @param error

*/

private void Callnativefunction (String functionname, Object args, Object success, object error) {

try {

using reflection, note that the jsfunctions class cannot be confused

method = JsFunctions.class.getMethod (functionname, Webview.class, Object.class, Object.class, Object.class);

Object invoke = Method.invoke (Jsfunctions.getinstance (), Mwebview, args, success, error);

} catch (Nosuchmethodexception e) {

TODO: prompt not found calling method

} catch (InvocationTargetException e) {

E.printstacktrace ();

} catch (Illegalaccessexception e) {

TODO: Prompt permission Access

E.printstacktrace ();

}

}

Fourth step, interface processing class

public class Jsfunctions {

/**

* Single Case

*/

private static Jsfunctions instance = new Jsfunctions ();

/**

* SayHello Interface

* @param WebView

* @param args

* @param successfunc

* @param errorfunc

*/

public void SayHello (WebView WebView, Object args, Object Successfunc, Object Errorfunc) {

if (args! = null) {

Object name = ((ArrayList) args). Get (0);

LOG.D (Name.tostring ());

if (successfunc! = null)

Calljsfunction (WebView, successfunc.tostring (), args);

} else {

if (errorfunc! = null)

Calljsfunction (WebView, errorfunc.tostring (), args);

}

}

/**

* Callback processing

* @param WebView

* @param functionname

* @param args

*/

public void Calljsfunction (WebView WebView, String functionname, Object args) {

String argsjsonstr = null;

if (args! = null) {

Argsjsonstr = jsonutil.write2string (args);

}

if (argsjsonstr! = null)

Webview.loadurl ("javascript:" + functionname + "('" + Argsjsonstr + "')");

Else

Webview.loadurl ("javascript:" + functionname + "()");

}

public static Jsfunctions getinstance () {

return instance;

}

}

All right, here we go, please correct me if you have any shortcomings. Of course, the development of the APP is also required to conduct a full range of testing:www.ineice.com


APP webview and inline web interaction implementation

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.