phonegap アプリケーション (Android) があります。プラグインDownloaderを使用してxml ファイルをダウンロードします。ファイルをダウンロードすると、ダウンロードが完了するまでアプリケーションが「一時停止」します。何もクリックできない、ローダーが機能しないなど。
以前は正常に動作していましたが、アプリを cordova 1.8.0 から新しいバージョン (2.7.0) にアップグレードする必要がありました。
また、新しいコルドバで動作するようにプラグイン自体も変更しました。
何が原因なのかわかりません。何か案は?。
----------編集: 追加されたコード---------
これが私のダウンローダープラグインクラスです
public class Downloader extends CordovaPlugin {
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
if (!action.equals("downloadFile")){
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
return true;
}
try {
String fileUrl = args.getString(0);
JSONObject params = args.getJSONObject(1);
String fileName = params.has("fileName") ?
params.getString("fileName"):
fileUrl.substring(fileUrl.lastIndexOf("/")+1);
String dirName = params.has("dirName") ?
params.getString("dirName"):
Environment.getExternalStorageDirectory().toString();
Boolean overwrite = params.has("overwrite") ? params.getBoolean("overwrite") : false;
callbackContext.sendPluginResult(this.downloadUrl(fileUrl, dirName, fileName, overwrite, callbackContext));
return true;
} catch (JSONException e) {
e.printStackTrace();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()));
return false;
} catch (InterruptedException e) {
e.printStackTrace();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, e.getMessage()));
return false;
}
}
private PluginResult downloadUrl(String fileUrl, String dirName, String fileName, Boolean overwrite, CallbackContext callbackContext) throws InterruptedException, JSONException {
try {
Log.d("PhoneGapLog", "Downloading "+fileUrl + " into " + dirName + "/" + fileName);
File dir = new File(dirName);
if (!dir.exists()) {
Log.d("PhoneGapLog", "directory " + dirName + " created");
dir.mkdirs();
}
File file = new File(dirName, fileName);
if (!overwrite && file.exists()) {
Log.d("DownloaderPlugin", "File already exist");
JSONObject obj = new JSONObject();
obj.put("status", 1);
obj.put("total", 0);
obj.put("file", fileName);
obj.put("dir", dirName);
obj.put("progress", 100);
return new PluginResult(PluginResult.Status.OK, obj);
}
URL url = new URL(fileUrl);
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setRequestMethod("GET");
ucon.connect();
Log.d("PhoneGapLog", "Download start");
InputStream is = ucon.getInputStream();
byte[] buffer = new byte[1024];
int readed = 0,
progress = 0,
// totalReaded = 0,
fileSize = ucon.getContentLength();
FileOutputStream fos = new FileOutputStream(file);
while ((readed = is.read(buffer)) > 0) {
fos.write(buffer, 0, readed);
//totalReaded += readed;
//int newProgress = (int) (totalReaded*100/fileSize);
//if (newProgress != progress)
// progress = informProgress(fileSize, newProgress, dirName, fileName, callbackId);
}
fos.close();
Log.d("PhoneGapLog", "Download finished");
JSONObject obj = new JSONObject();
obj.put("status", 1);
obj.put("total", fileSize);
obj.put("file", fileName);
obj.put("dir", dirName);
obj.put("progress", progress);
return new PluginResult(PluginResult.Status.OK, obj);
}
catch (FileNotFoundException e) {
Log.d("PhoneGapLog", "File Not Found: " + e);
return new PluginResult(PluginResult.Status.ERROR, 404);
}
catch (IOException e) {
Log.d("PhoneGapLog", "Error: " + e);
return new PluginResult(PluginResult.Status.ERROR, e.getMessage());
}
}
}
これが私のダウンローダープラグインjavascriptです
function Downloader() {}
Downloader.prototype.downloadFile = function(fileUrl, params, win, fail) {
//Make params hash optional.
if (!fail) win = params;
return cordova.exec(win, fail, "Downloader", "downloadFile", [fileUrl, params]);
};
if(!window.plugins) {
window.plugins = {};
}
if (!window.plugins.downloader) {
window.plugins.downloader = new Downloader();
}
そして、私がそれを呼び出すとき
$.mobile.showPageLoadingMsg();
window.plugins.downloader.downloadFile("URL",
{overwrite: true,
dirName: dir, fileName: "File.xml"},
function() {
alert("finished");
}, function(error) {
alert("fail");
);
showPageLoadingMsg によって表示されるローダーは、ダウンロードが完了するまでフリーズするか、ダウンロードが完了すると表示されます。