android 网络程序下载,Android之网络文件下载
环境搭建:本地服务器(下载wamp【Windows, Apache, MySQL, PHP 】,安装即可,然后将网页或文件放进 www directory)虚拟机(访问本地服务器的ip:10.0.2.2)注意事项:权限相关网络访问 存储卡访问 文件权限 如:chmod 777 /data源代码:package com.example.net_ex04;import java.io.File;im.
环境搭建:
本地服务器(下载wamp【Windows, Apache, MySQL, PHP 】,安装即可,然后将网页或文件放进 www directory)
虚拟机(访问本地服务器的ip:10.0.2.2)
注意事项:
权限相关
网络访问 存储卡访问
文件权限 如:chmod 777 /data
源代码:
package com.example.net_ex04;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
ProgressBar progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar)this.findViewById(R.id.progressBar1);
Button button = (Button)this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
String url = "http://10.0.2.2/Music.flv";
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
new DownloadTask().execute(url);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class DownloadTask extends AsyncTask{
public DownloadTask() {
// TODO Auto-generated constructor stub
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
HttpGet httpRequest = new HttpGet(params[0]);
HttpClient httpClient = new DefaultHttpClient();
try{
HttpResponse httpResponse = httpClient.execute(httpRequest);
HttpEntity entity = httpResponse.getEntity();
long length = entity.getContentLength();
progressBar.setMax((int)length);
InputStream inputStream = entity.getContent();
byte[] b = new byte[1024];
int readedLength = -1;
//File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "Music.flv");
File file = new File("/data/Music.flv");
if(!file.exists())
{
file.createNewFile();
}
OutputStream outputStream = new FileOutputStream(file);
int count = 0;
while((readedLength = inputStream.read(b)) != -1){
outputStream.write(b, 0, readedLength);
count += readedLength;
publishProgress(count);
}
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Integer... values){
progressBar.setProgress(values[0]);
super.onProgressUpdate(values);
}
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)