2011-10-04 73 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我告诉自己我永远不会忘记那个...... –