我正在使用android项目的phonegap。我想从url下载一个文件到我的sdk卡。
这是我的Downloader.java
package com.example.pgplugins.downloaderPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
import com.phonegap.DroidGap;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Downloader extends Plugin{
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
if (action.equals("downloadFile")) {
try {
return this.downloadUrl(args.getString(0),args.getString(1),args.getString(2),args.getString(3));
} catch (JSONException e) {
return new PluginResult(PluginResult.Status.ERROR, "Param errrors");
}
}
else {
return new PluginResult(PluginResult.Status.INVALID_ACTION);
}
}
private PluginResult downloadUrl(String fileUrl, String dirName, String fileName, String overwrite){
try{
Log.d("DownloaderPlugin", "DIRECTORY CALLED /sdcard/"+dirName+" created");
File dir = new File("/sdcard/"+dirName);
if(!dir.exists()){
Log.d("DownloaderPlugin", "directory /sdcard/"+dirName+" created");
dir.mkdirs();
}
File file = new File("/sdcard/"+dirName+fileName);
if(overwrite.equals("false") && file.exists()){
Log.d("DownloaderPlugin", "File already exist");
return new PluginResult(PluginResult.Status.OK, "exist");
}
URL url = new URL(fileUrl);
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setRequestMethod("GET");
ucon.setDoOutput(true);
ucon.connect();
Log.d("DownloaderPlugin", "download begining");
Log.d("DownloaderPlugin", "download url:" + url);
InputStream is = ucon.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
FileOutputStream fos = new FileOutputStream(file);
while ( (len1 = is.read(buffer)) > 0 ) {
fos.write(buffer,0, len1);
}
fos.close();
Log.d("DownloaderPlugin", "Download complete in" + fileName);
} catch (IOException e) {
Log.d("DownloaderPlugin", "Error: " + e);
return new PluginResult(PluginResult.Status.ERROR, "Error: " + e);
}
return new PluginResult(PluginResult.Status.OK, fileName);
}
}
这是我的Downloader.js
function Downloader() {
}
Downloader.prototype.downloadFile = function(fileUrl,dirName,fileName,overwrite,win,fail) {
if(overwrite==false) overwrite="false";
else overwrite="true";
PhoneGap.exec(win, fail, "Downloader", "downloadFile", [fileUrl,dirName,fileName,overwrite]);
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin("downloader", new Downloader());
PluginManager.addService("Downloader","com.example.pgplugins.downloaderPlugin.Downloader");
});
这是我的index.html
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>PhoneGap Demo With JQuery Mobile</title>
<link rel="stylesheet" href="jquery.mobile/jquery.mobile-1.0b2.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="pgandjqm-style-override.css" type="text/css" charset="utf-8" />
<script type="text/javascript" src="jquery.mobile/jquery-1.6.2.min"></script>
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script src="jquery.mobile/jquery.mobile-1.0b2.js"></script>
<script type="text/javascript" charset="utf-8" src="main.js"></script>
<!-- CDN Respositories: For production, replace lines above with these uncommented minified versions -->
<!-- <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.css" />-->
<!-- <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>-->
<!-- <script src="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.js"></script>-->
script type="text/javascript" charset="utf-8" src="downloader.js"></script>
<script type="text/javascript">
window.plugins.downloader.downloadFile("http://www.toforge.com/archive.zip","sdcard/cache/","archive.zip", false,
function(data){
if(data=="exist"){
alert("File already exist");
}
else{
alert("File saved on sd card")
}
},function(data){ alert("error: "+data); });
</script>
</head>
<body onload="init();">
</body>
</html>
每当我运行该程序时,发生此错误,意味着我没有得到任何结果。
错误: - 强制关闭程序。
我的程序没有运行。在开始运行之前意味着停止。请检查我的代码并帮助我找出错误。