android - how to upgrade a phonegap plugin from 1.6.x to 2.7? -
the code aim grap content using jsoup, , i'm try upgrade code
here code far:
package com.phonegap.g7.plugin; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import org.jsoup.jsoup; import org.jsoup.nodes.document; import org.jsoup.nodes.element; import org.apache.cordova.api.cordovaplugin; import org.apache.cordova.api.pluginresult; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; public class urlfeedjava extends cordovaplugin { //@override public pluginresult execute(string action, jsonarray data, string callbackid) { pluginresult.status status = pluginresult.status.ok; pluginresult r=null; try { string target = data.getstring(0); string selector = data.getstring(1); document doc = jsoup.connect(target).get(); element masthead = doc.select(selector).first(); string content=masthead.tostring(); string title = doc.title(); r=new pluginresult(status,content); } catch(exception ee) { system.out.print("ee:"+ee.getmessage()); } return r; } }
the phonegapplugin.js file
urlfeed.prototype = { send: function(success, error, url, selector){ cordova.exec(success, error, "urlfeedjava", "send", [url, selector]); } }; phonegap.addconstructor(function() { phonegap.addplugin('urlfeed', new urlfeed()); });
the index.html
<!doctype html> <html> <head> <meta http-equiv='content-type' content='text/html; charset=utf-8' /> <script type='text/javascript' src='jquery-1.8.3.min.js'></script> <script type='text/javascript' src='cordova-2.7.0.js'></script> <script type='text/javascript' src='phonegapplugin.js'></script> <script type='text/javascript'> $(function(){ var onsend = function(){ var success = function(data){ $('#show').html(data); }; var error = function(e){ alert(e); }; var url = 'http://sports.163.com/12/0618/09/8496qlng00051c8v.html'; var selector = $('#selector').val(); window.plugins.urlfeed.send(success, error, url, selector); }; $('#send').bind('click', onsend); }); </script> </head> <body> <div id='messagediv'> <input type='text' id='selector'></input> <div id='show'></div> <button type='button' id='send'>send me</button> </div> </body> </html>
especially don't understand function of constructor.
with newer phonegap releases define constructor in javascript this:
var urlfeed = function(){}; cordova.addconstructor(function() { if (!window.plugins) { window.plugins = {}; } window.plugins.urlfeed = new urlfeed(); });
it makes urlfeed object available entire application, access anywhere like
window.plugins.urlfeed.dosomething();
adding methods plugin:
urlfeed.prototype.dosomething = function(arguments){ //do stuff //send phonegap return phonegap.exec( successcallback, failurecallback, 'urlfeed', null, [arguments] ); };
you have change java plugin this:
public class urlfeedjava extends cordovaplugin { //@override public boolean execute(string action, jsonarray args, final callbackcontext callbackcontext) throws jsonexception { //your code return true; } }
so basic change not return plugin result anymore, simple boolean. remove plugin result code java code.
please have @ simple toast plugin. shows how phonegap plugin developed https://github.com/giver/cordova-android-toast-plugin
Comments
Post a Comment