2011-10-04 72 views
0

我正在嘗試使用我的下載代碼,該代碼在我的其他應用程序中運行得非常好,但在我嘗試製作的這個新應用程序中,它的行爲非常奇怪。不包含數據的變量?

public class DownloadFile extends AsyncTask<Void, Integer, String> {               
    String SDCardRoot; 
    String mixtapeurl = "http://xxxxxx.com/xxxxxxxxxxxx/Mixtapes/NCredible/J.%20Cole%20-%20Cole%20World.zip"; 
    NotificationManager notificationManager; 
    Notification notification2; 
    ProgressBar progressBar; 

    @Override 
    protected void onPreExecute() {   
     // configure the intent 
     Intent intent = new Intent(); 
     final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); 

     // configure the notification 
     notification2 = new Notification(R.drawable.download, "DOWNLOADING: " + mixtapeurl, System 
       .currentTimeMillis()); 
     notification2.flags = notification2.flags | Notification.FLAG_ONGOING_EVENT; 
     notification2.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress); 
     notification2.contentIntent = pendingIntent; 
     notification2.contentView.setTextViewText(R.id.percentage, 0 + "%"); 
     notification2.contentView.setTextViewText(R.id.status_text, "DOWNLOADING: " + mixtapeurl); 
     notification2.contentView.setProgressBar(R.id.status_progress, 100, 0, false); 

     getApplicationContext(); 
     notificationManager = (NotificationManager) getApplicationContext().getSystemService(
       Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(2, notification2);     
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     try { 
      //set the download URL, a url that points to a file on the internet 
      //this is the file to be downloaded 
      URL url = new URL(mixtapeurl); 

      //create the new connection 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

      //set up some things on the connection 
      urlConnection.setRequestMethod("GET"); 
      urlConnection.setDoOutput(true); 

      //and connect! 
      urlConnection.connect(); 

      //set the path where we want to save the file 
      //in this case, going to save it on the root directory of the 
      //sd card. 
      SDCardRoot = Environment.getExternalStorageDirectory() + "/download/"; 
      //create a new file, specifying the path, and the filename 
      //which we want to save the file as. 
      File file = new File(SDCardRoot,mixtapeurl); 

      //this will be used to write the downloaded data into the file we created 
      FileOutputStream fileOutput = new FileOutputStream(file); 

      //this will be used in reading the data from the internet 
      InputStream inputStream = urlConnection.getInputStream(); 

      //this is the total size of the file 
      int totalSize = urlConnection.getContentLength(); 
      //variable to store total downloaded bytes 
      int downloadedSize = 0; 
      int myProgress; 

      //create a buffer... 
      byte[] buffer = new byte[1024]; 
      int bufferLength = 0; //used to store a temporary size of the buffer 
      int previousProgress = 0; 

      //now, read through the input buffer and write the contents to the file 
      while ((bufferLength = inputStream.read(buffer)) > 0) { 
        //add the data in the buffer to the file in the file output stream (the file on the sd card 
        fileOutput.write(buffer, 0, bufferLength); 
        //add up the size so we know how much is downloaded 
        downloadedSize += bufferLength; 
        //this is where you would do something to report the prgress, like this maybe 
        //updateProgress(downloadedSize, totalSize); 
        //publishProgress((int)(total*100/lenghtOfFile)); 
        myProgress = (int) ((downloadedSize/(double)totalSize) * (double)100); 


        if ((myProgress) > 1 && (myProgress) > ((previousProgress))) { 
        previousProgress= myProgress; 

        publishProgress(myProgress); 
        }      
      } 
      //close the output stream when done 
      fileOutput.close();            

    //catch some possible errors... 
    } catch (MalformedURLException e) {         
      e.printStackTrace(); 
    } catch (IOException e) {     
      e.printStackTrace(); 
    } 
     return "success";} 


    @Override 
    protected void onProgressUpdate(Integer... progression) {   
     String percent; 
     percent = progression[0].toString(); 

     notification2.contentView.setProgressBar(R.id.status_progress, 100, progression[0], false);      

     notification2.contentView.setTextViewText(R.id.percentage, percent + "%"); 
     notificationManager.notify(2, notification2);  

    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub     
     Toast toast = Toast.makeText(getApplicationContext(), mixtapeurl + " downloaded to: " + SDCardRoot, Toast.LENGTH_LONG); 
     toast.show(); 
    } 
} 

這是我如何實現它:

new DownloadFile().execute(); 

而我得到的是「http://xxxxxx.com/xxxxxxxxxxxx/Mixtapes/NCredible/J.%20Cole%20-%20Cole %20World.zip下載到null「,我點擊按鈕後,我的土司就沒有下載了。

+0

什麼代碼看起來像你點擊按鈕的地方在發生什麼? –

回答

0

這聽起來像是一個非常普遍的問題:忘記設置清單權限。

<uses-permission android:name="android.permission.INTERNET" /> 
+0

然後會拋出異常,不是嗎?編輯:改變了我的想法,你可能是對的,他沒有記錄他們。 – dmon

+0

只是在黑暗中拍攝;-) *代碼以前工作,但不是在新的應用程序*和代碼使用網絡連接。這似乎很常見。在開始時,每個人都在爲此奮鬥... – Knickedi

+0

AHH我告訴自己我永遠不會忘記那個...... –