0

该代码旨在通过使用jsoup来获取内容,我正在尝试升级代码

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

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>

特别是我不明白构造函数的功能。

4

1 回答 1

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 代码中删除所有插件结果代码。

请看一下简单的吐司插件。它向您展示了如何开发 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

于 2013-08-20T07:20:29.390 回答