2012-09-06 74 views
0

這是我面對的一個奇怪的錯誤,當試圖下載文件時,我需要打開輸入流,然後將數據寫入輸出流...輸入流不起作用...那裏調試時沒有錯誤..但它似乎只是掛在那裏,鍵盤上的F6似乎不工作....必須終止調試會話...我在做什麼錯?輸入流錯誤

try{ 
     URL url = new URL(pdfurl); 
     URLConnection conexion = url.openConnection(); 
     conexion.connect(); 

     int lenghtOfFile = conexion.getContentLength(); 
     Log.d("k", "Lenght of file: " + lenghtOfFile); 
     //InputStream s=file 
     InputStream s=url.openStream(); 
     InputStream input = new BufferedInputStream(s); 
     OutputStream output = new FileOutputStream(file); 

     byte data[] = new byte[1024]; 

     long total = 0; 

     while ((count = input.read(data)) != -1) { 
      total += count; 
      publishProgress((int)((total*100)/lenghtOfFile)); 
      output.write(data, 0, count); 
     } 

     output.flush(); 
     output.close(); 
     input.close(); 
     } 
     catch (Exception e) 
     { 
     download_flag = true; 
     String s=e.getMessage().toString(); 
     Log.d("k","exception occured"+s); 
     } 
+0

你添加了互聯網的權限在AndroidManifest.xml? –

+0

是的..我有.....否則它不會連接到服務器...正在獲取文件的長度。 –

回答

0

你可以嘗試一個HTTPGET請求:

HttpGet httpGet = new HttpGet(url); 
HttpClient httpclient = new DefaultHttpClient(); 
// Execute HTTP Get Request 
HttpResponse response = httpclient.execute(httpGet); 
content = response.getEntity().getContent();