2016-03-28 119 views
0

我試圖從API下載一個zip文件。爲此我使用這個下面的代碼:Android下載一個Zip文件

public class Download_Activity extends Activity { 

public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
private Button startBtn; 
private ProgressDialog mProgressDialog; 

/** 
* Called when the activity is first created. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.download); 
    startBtn = (Button) findViewById(R.id.downloadButton); 

    startBtn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      startDownload(); 
     } 
    }); 
} 

private void startDownload() { 
    String url = downloadURL; 
    new DownloadFileAsync().execute(url); 
} 

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
     case DIALOG_DOWNLOAD_PROGRESS: 
      mProgressDialog = new ProgressDialog(this); 
      mProgressDialog.setMessage("Downloading file.."); 
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      mProgressDialog.setCancelable(false); 
      mProgressDialog.show(); 
      return mProgressDialog; 
     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 urlConnection = url.openConnection(); 
      urlConnection.setDoOutput(true); 
      urlConnection.connect(); 

      Log.i("1111", "1111"); 
      File SDCardRoot = Environment.getExternalStorageDirectory(); 
      File file = new File(SDCardRoot, "hello1.zip"); 
      Log.i("2222", "2222"); 
      FileOutputStream fileOutput = new FileOutputStream(file); 
      InputStream inputStream = urlConnection.getInputStream(); 
      int totalSize = urlConnection.getContentLength(); 
      int downloadedSize = 0; 
      byte[] buffer = new byte[1024]; 
      int bufferLength = 0; 
      Log.i("3333", "3333"); 
      Log.i("befferLength", "bufferLength: " + bufferLength); 
      Log.i("is read buffer", "is read buffer: " + inputStream.read(buffer)); 
      while ((bufferLength = inputStream.read(buffer)) > 0) { 
       Log.i("inside while", "inside while "); 
       fileOutput.write(buffer, 0, bufferLength); 
       downloadedSize += bufferLength; 
       updateProgress(downloadedSize, totalSize); 
      } 
      Log.i("4444", "4444"); 
      fileOutput.close(); 

     } catch (Exception e) { 
     } 
     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); 
    } 
} 

public void updateProgress(int currentSize, int totalSize) { 
    Toast.makeText(getApplicationContext(), "Loading Files...", 
      Toast.LENGTH_SHORT).show(); 
} 

在這段代碼,zip文件作爲「hello1.zip」創建的,但該文件是在我的手機是空的。在這裏我有各種日誌語句來查找代碼的執行情況。令我驚訝的只有「Log.i(」2222「,」2222「);」在其他日誌不打印時打印。你能告訴我什麼是問題嗎?

在此先感謝..!

+0

在'catch'中添加一些日誌,看看是否有異常 – Lino

回答