该代码旨在通过使用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>
特别是我不明白构造函数的功能。