2013-08-20 93 views
0

該代碼是通過使用jsoup瞄準虎視眈眈的內容,我嘗試升級代碼如何將手機插件從1.6.x升級到2.7?

這裏是我到目前爲止的代碼:

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> 

尤其是我不明白的F構造函數的聯合。

回答

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

+0

謝謝,我的另一個問題http://stackoverflow.com/questions/18348751/how-to-return-a-array-or - 其他的集合元素型從-的PhoneGap-機器人 - 對 – hkguile