2011-01-13 90 views
-1

嘗試下面的代碼下載zip到SD卡,我得到NullpointerException。當試圖進行一些審訊時,我發現zip文件實際上並未下載。那麼你是否需要幫助代碼是否需要一些更改或zip的任何錯誤? M只停留在那一點上。請幫助...我的代碼如下...Android下載Zip到SD卡,zip沒有被讀取

{ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     String from = "http://192.168.1.63/ZipFile/Text.zip" ; 
     String to = Environment.getExternalStorageDirectory() + "/newunzip/"; 

     try { 
      ((TextView) findViewById(R.id.display)).append("\n in try after function :"); 
      downloadFile(from, to); 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      ((TextView) findViewById(R.id.display)).append("\n \n Exception occured :"); 
      ((TextView) findViewById(R.id.display)).append("\n \n Exception message is :"+e.getMessage()); 
      ((TextView) findViewById(R.id.display)).append("\n \n Exception is :"+e.toString()); 
     } 

    } 

    private void downloadFile(String from, String to) throws Exception 
    { 
     ((TextView) findViewById(R.id.display)).append("\n \n in function call :"); 
     HttpURLConnection conn = (HttpURLConnection)new URL(from).openConnection(); 
     conn.setDoInput(true); 
     conn.setConnectTimeout(100000); // timeout 100 secs 
     conn.connect(); 

     ((TextView) findViewById(R.id.display)).append("\n \n Connecting to url :"+ conn); 
     InputStream input = conn.getInputStream(); 

     byte[] b = null; 
     input.read(b); 
     ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b); 

     FileOutputStream fOut = new FileOutputStream(to); 

     byte[] b1 = null; 
     input.read(b1); 
     ((TextView) findViewById(R.id.display)).append("\n \n output method :"+ b1); 

     int byteCount = 0; 
     byte[] buffer = new byte[4096]; 
     int bytesRead = -1; 
     while ((bytesRead = input.read(buffer)) != -1) 
     { 
      ((TextView) findViewById(R.id.display)).append("\n \n reading/writing files :"); 
      fOut.write(buffer, 0, bytesRead); 
      byteCount += bytesRead; 
     } 
     fOut.flush(); 
     ((TextView) findViewById(R.id.display)).append("\n \n flush & close :"); 
     fOut.close(); 
    } 

} 

回答

0

您正在使用空字節數據從流中讀取;

byte[] b = null; 
    input.read(b); 
    ((TextView) findViewById(R.id.display)).append("\n \n input method :"+ b); 

評論這些專輯。