通过android从Web服务器下载pdf文件、音频和视频文件
我已经创建了一个android应用程序。这需要将pdf、音频和视频文件从我的asp.NETWebAPI下载到我的android应用程序。但是我不知道我的web api代码会是什么样子,我的android下载pdf、音频文件和视频文件的代码会是什么样子。通过android从Web服务器下载pdf文件、音频和视频文件,android,Android,我已经创建了一个android应用程序。这需要将pdf、音频和视频文件从我的asp.NETWebAPI下载到我的android应用程序。但是我不知道我的web api代码会是什么样子,我的android下载pdf、音频文件和视频文件的代码会是什么样子。 感谢您的阅读。您可以自定义并使用此功能,您需要wakelock权限以防止中断 @SuppressLint("Wakelock") public class DownloadTask extends AsyncTask<String, Int
感谢您的阅读。您可以自定义并使用此功能,您需要wakelock权限以防止中断
@SuppressLint("Wakelock")
public class DownloadTask extends AsyncTask<String, Integer, String>
{
private Context context;
public DownloadTask(Context context ,Dialog dialog, ProgressBar progressBar ,TextView progressTextView , String destinationPath ,String fileName , JSONObject jObject )
{
this.context = context;
}
@SuppressWarnings("resource")
@Override
protected String doInBackground(String... sUrl)
{
String directory = sUrl[0];
String fileName = sUrl[1];
//prevent CPU from going off if the user presses the power button during download
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
wl.acquire();
//download
try
{
new File(directory).mkdirs();
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try
{
//connect to url
URL url = new URL(sUrl[2]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// check for http_ok (200)
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
return "Server returned HTTP "
+ connection.getResponseCode() + " "
+ connection.getResponseMessage();
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
output = new FileOutputStream(directory+"/"+fileName+".mp3");//change extension
//copying
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1)
{
// allow canceling
if (isCancelled())
{
new File(directory+"/"+fileName+".mp3").delete();//delete partially downloaded file
return null;
}
total += count;
if (fileLength > 0 ) //publish progress only if total length is known
publishProgress( (int)(total/1024) , fileLength/1024 );//(int) (total * 100 / fileLength));
output.write(data, 0, count);
}
}
catch (Exception e)
{
return e.toString();
}
finally //closing streams and connection
{
try
{
if (output != null)
output.close();
if (input != null)
input.close();
}
catch (IOException ignored)
{
}
if (connection != null)
connection.disconnect();
}
}
finally
{
wl.release(); // release the lock screen
}
return null;
}
@Override // onPreExecute and onProgressUpdate run on ui thread so you can update ui from here
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress)
{
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(String result)
{
if (result != null)
Toast.makeText(context, "Download error: " + result,Toast.LENGTH_SHORT).show();
else
{
Toast.makeText(context, " download complete ",Toast.LENGTH_SHORT).show();
}
}
}
在提问之前,请谷歌/研究更多信息。你的问题就像“嘿,我需要一个Android应用程序和服务器,你能帮我写吗?”非常感谢@Sebastian。注意到的评论。我目前正在做关于上述主题的研究。我只是粘贴了这个,因为我在我的项目的时间线上落后了。我真的很抱歉,如果我违反了规则张贴在反正。我理解每个开发人员都必须愿意进行研究以在行业中脱颖而出。非常感谢。在下一篇帖子中,我已经发布了http下载代码,但正如@Sebastian所说的,你必须搜索更多。是的,谢谢@warlock。如果你觉得有帮助,你可以接受并投票,非常感谢Hanks。我会尝试你的建议和我在研究中得到的,并可能编辑这个问题。如果我遇到任何问题。非常感谢。
new DownloadTask(this).execute( dirPath , fileName , urlToDownload );