我正在开发一个需要Android Downloader插件的手机屏幕应用程序:https://github.com/phonegap/phonegap-plugins/tree/master/Android/Downloader
不幸的是,它似乎无法使用最新的cordova(2.2)。我花了几个小时尝试升级代码:
downloader.js
cordova.define(
"cordova/plugin/downloader",
function(require, exports, module)
{
var exec = require("cordova/exec");
var Downloader = function() {};
Downloader.prototype.downloadFile = function(fileUrl, params, successCallback, failureCallback)
{
// Make params hash optional.
if (!failureCallback) win = params;
return exec(
successCallback,
failureCallback,
'Downloader',
'downloadFile',
[fileUrl, params]);
};
var downloader = new Downloader();
module.exports = downloader;
}
);
Downloader.java
package org.apache.cordova.plugins.Downloader;
import org.apache.cordova.api.CordovaInterface;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
[more imports...]
public class Downloader extends Plugin
{
etc.
config.xml中
<plugins>
<plugin name="App" value="org.apache.cordova.App"/>
[...]
<plugin name="Downloader" value="org.apache.cordova.plugins.Downloader"/>
</plugins>
HTML JS调用
downloader = cordova.require("cordova/plugin/downloader");
downloader.downloadFile(...
应用程序在Eclipse中编译正常,但在执行下载程序代码时,我得到:未找到ALERT类。 logcat说:
W / System.err(4035):java.lang.ClassNotFoundException:org.apache.cordova.plugins.Downloader ... 添加插件org.apache.cordova.plugins.Downloader时出错。
你能帮我解决错误吗?
答案 0 :(得分:1)
我在js文件中做了一些更改,而不是由你完成,所有工作都正常。
function Downloader() {}
Downloader.prototype.downloadFile = function(fileUrl, params, win, fail) {
//Make params hash optional.
if (!fail) win = params;
PhoneGap.exec(win, fail, "Downloader", "downloadFile", [fileUrl, params]);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("downloader", new Downloader());
PluginManager.addService("Downloader", "com.phonegap.plugins.downloader.Downloader");
});
if(!window.plugins) {
window.plugins = {};
}
if (!window.plugins.downloader) {
window.plugins.downloader = new Downloader();
}