xmlファイルを解析することができました。xmlによってそのURLが指定されたファイルをダウンロードしたいと考えています。私は次のコードを持っています:Android:100 KB以上のファイルが1 KBになる
try{
/* Create a URL we want to load some xml-data from. */
URL url = new URL("http://dev2.eacomm.com/tabletcms/tablets/sync_login/jayem30/jayem");
url.openConnection();
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(url.openStream()));
/* Parsing has finished. */
/* Our ExampleHandler now provides the parsed data to us. */
List<ParsedExampleDataSet> parsedExampleDataSet = myExampleHandler.getParsedData();
Iterator i;
i = parsedExampleDataSet.iterator();
ParsedExampleDataSet dataItem;
while(i.hasNext()){
dataItem = (ParsedExampleDataSet) i.next();
String folder = dataItem.getParentTag();
if(folder == "Videos" ){
String [] videoName = dataItem.getName().split("/");
String currentFile = videoName[0] + "." + videoName[1];
String currentFileURL = dataItem.getUrl() + videoName[0] + "." + videoName[1];
tv.append("\nURL: " + currentFileURL);
new DownloadFileAsync().execute(currentFile , currentFileURL, "Videos");
this.videoCount++;
tv.append("\nVideo Count: " + this.videoCount);
}
if(folder == "Slideshows"){
//processSlideshows(dataItem, folder);
}
}
}catch(Exception e){
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
ダウンロードコードは非同期タスクにあります。しかし、私がこれを実行すると、私がダウンロードしたと思われる2つのファイル(126kbと98kb)がダウンロードされます。ファイルはそこにありますが、そのサイズは1kbのみです。ファイルは再生されません。
私はファイルサイズが細かいですが、それだけで一つのファイルを返す
**new DownloadFileAsync().execute(currentFile , currentFileURL, "Videos");**
to
**new DownloadFileAsync().execute("hehe.flv", "http://dev2.eacomm.com/tabletcms/app/webroot/files/000002/videos/27.flv", "Videos");**
行を変更します。
EDIT:
//---------------------------- START DownloadFileAsync -----------------------//
class DownloadFileAsync extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... strings) {
try {
String currentFile = strings[0];
String currentFileURL = strings[1];
String folder = strings[2];
File root = Environment.getExternalStorageDirectory();
URL u = new URL(currentFileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lenghtOfFile = c.getContentLength();
FileOutputStream f = new FileOutputStream(new File(root + "/Engagia/Downloads/" + folder, currentFile));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
long total = 0;
while ((len1 = in.read(buffer)) > 0) {
total += len1; //total = total + len1
publishProgress("" + (int)((total*100)/lenghtOfFile));
f.write(buffer, 0, len1);
}
f.close();
}catch (Exception e){
Log.d("Downloader", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
//---------------------------- END DownloadFileAsync -----------------------//
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading files...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
EDIT:(行き当たりばったりのおかげで)
私の悪い、私は私のURLを再チェックし、私は私のXMLストリームがそれほどダウンロードのために正しいURLを返さないことが判明ダウンロード用のURLを再構築する必要がありました。私は次のようなことをしました:
tv.append("\nCurrent File URL: " + currentFileURL);
String downloadFileURL = currentFileURL.replace("tablets/tablet_content", "app/webroot/files");
あなたは 'currentFile'と' currentFileURL'の両方が正しいことを確認したことがありますか?コードをデバッグして、それらが正しく形成されているかどうか確認してください。 – Haphazard
あなたの応答のためにこんにちは、ありがとう、はい、彼らは両方正しいです、私は実際に他のURLを試しました – Kris
あなたのダウンローダーからエラーが発生しますか? 'Log.d(" Downloader "、e.getMessage());'出力は非常に面白いでしょう。また、 "error"の場合は 'Log.e'でなければなりません。 'Log.d'は" debug "文のためのものです。 – Haphazard