如何将phonegap插件从1.6.x升级到2.7?

时间:2013-08-20 05:43:33

标签: android cordova

代码的目标是使用jsoup获取grap内容,我尝试升级代码

到目前为止,这是我的代码:

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;
    }
}

PhonegapPlugin.js文件

UrlFeed.prototype = {
        send: function(success, error, url, selector){
            cordova.exec(success, error, "UrlFeedJava", "send", [url, selector]);
        }
};

PhoneGap.addConstructor(function() {
    PhoneGap.addPlugin('urlfeed', new UrlFeed());
});

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>

特别是我不了解构造函数的功能。

1 个答案:

答案 0 :(得分:0)

使用较新的phonegap版本,您可以在javascript中定义构造函数,如下所示:

var UrlFeed = function(){};

cordova.addConstructor(function() {
    if (!window.plugins) {
        window.plugins = {};
    }
    window.plugins.UrlFeed = new UrlFeed();
});

它使urlfeed对象可用于整个应用程序,您可以在任何地方访问它,如

window.plugins.UrlFeed.doSomething();

为插件添加方法:

UrlFeed.prototype.doSomething = function(arguments){
    //do stuff

    //send to phonegap
    return PhoneGap.exec(
        successCallback,    
        failureCallback,     
        'UrlFeed', 
        null,              
        [arguments]        
    );
};

你必须像这样更改你的java插件:

public class UrlFeedJava extends CordovaPlugin {
    //@Override
    public boolean execute(String action, JSONArray args,
        final CallbackContext callbackContext) throws JSONException {

        //your code
        return true;
    }
}

所以基本的改变是你不再返回一个插件结果,而是一个简单的布尔值。因此,从java代码中删除所有插件结果代码。

请查看简单的Toast插件。它向您展示了如何开发phonegap插件https://github.com/giver/cordova-android-toast-plugin

参考:http://docs.phonegap.com/en/2.8.0/guide_plugin-development_index.md.html#Plugin%20Development%20Guide