我需要使用多个URL下载zip文件,这是我的 Postman[URL“的 Postman ]
aij0ehis1#
public class downloadfile extends Activity { public static final int DIALOG_DOWNLOAD_PROGRESS = 0; private Button starbtn; private ProgressDialog mProgress; /**Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startbtn = (Button)findViewById(R.id.startbtn); startbtn.setOnClickListener(new OnClickListener(){ public void onClick(View v) { startDown(); } }); } private void startDown() { String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.zip"; new DownloadFileAsync().execute(url); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_DOWNLOAD_PROGRESS: mProgress = new ProgressDialog(this); mProgress.setMessage("Downloading file.."); mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgress.setCancelable(false); mProgress.show(); return mProgress; default: return null; } } class DownloadFileAsync extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); showDialog(DIALOG_DOWNLOAD_PROGRESS); } @Override protected String doInBackground(String... aurl) { int count; try { URL url = new URL(aurl[0]); URLConnection conexion = url.openConnection(); conexion.connect(); int lenghtOfFile = conexion.getContentLength(); Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); inputStreamStream inputStream = new BufferedinputStreamStream(url.openStream()); outputStreamStream outputStream = new FileoutputStreamStream("/sdcard/some_photo_from_gdansk_poland.zip"); byte data[] = new byte[1024]; long total = 0; while ((count = inputStream.read(data)) != -1) { total += count; publishProgress(""+(int)((total*100)/lenghtOfFile)); outputStream.write(data, 0, count); } outputStream.flush(); outputStream.close(); inputStream.close(); } catch (Exception e) {} return null; } protected void onProgressUpdate(String... progress) { Log.d("ANDRO_ASYNC",progress[0]); mProgress.setProgress(Integer.parseInt(progress[0])); } @Override protected void onPostExecute(String unused) { dismissDialog(DIALOG_DOWNLOAD_PROGRESS); } } }
1条答案
按热度按时间aij0ehis1#