2013-07-05 35 views
0

我是一名初學者,使用android編程。下載Android版

我有一些問題,下載文件與Android

我用Httpost,HTTPGET和hhtpurlconnection 兩個第一是不是在所有 工作和第三不能下載拖時間

我想要一種方式將不同的xmls下載到字符串或輸入流(或者可轉換爲它們的東西)來解析這些XML。 除了方法應該能夠做這樣的事情:

conn.setRequestProperty("Authorization", "Basic " + encodedStr); 

因爲個XML是從API

+0

看到這個回答問題http://stackoverflow.com/問題/ 13481027/i-want-in-download-xml-file-for-parsrsing – Chucky

+0

但是在第二次使用它時,它顯示錯誤並退出 –

+0

@EbrahimTahernejad然後閱讀錯誤 – keyser

回答

0

下面,你可以用它來下載文件的示例響應。當然,你將不得不使用正確的URL。

public InputStream downloadXmlFileStreamUsingUrl(final String url) { 

    log.info(String.format("downloadXmlFileStreamUsingUrl: %s", url)); 


    final HttpGet getRequest = new HttpGet(url); 
    HttpClient client; 
    try { 
     client = new DefaultHttpClient(); 

     final HttpResponse getResponse = client.execute(getRequest); 
     final int statusCode = getResponse.getStatusLine().getStatusCode(); 

     if (statusCode != HttpStatus.SC_OK) { 
     log.warn("Error " + statusCode + " for URL " + url); 
     return null; 
     } 

     final HttpEntity getResponseEntity = getResponse.getEntity(); 
     final InputStream content = getResponseEntity.getContent(); 
     return content; 

    } catch (final IOException e) { 
     getRequest.abort(); 
     log.warn("Exception in downloadXmlFileStreamUsingUrl, error for URL " + url + e, e); 
    } 
    finally { 
     // When HttpClient instance is no longer needed, 
     // shut down the connection manager to ensure 
     // immediate deallocation of all system resources 
     client.getConnectionManager().shutdown(); 
    } 

    return null; 

    } 
+0

第10行的問題:| –

+0

確實,忘了刪除它,我編輯了代碼,它應該是現在。 –

2

這裏我舉一個例子如何從服務器下載圖像文件。我asuming您的本地服務器上有一個圖片文件夾,您是從下載PIC .. 使用下面的代碼,它可以幫助你..

public class DownloadType1 extends Activity{ 

    String dwnload_file_path = "http://10.0.2.2/pictures/webicon.PNG"; 
    String dest_file_path = Environment.getRootDirectory()+"/pictures/img1.png"; 
    ProgressDialog dialog = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.download1); 
    } 


    public void onClick(View v) { 

     dialog = ProgressDialog.show(DownloadType1.this, "", "Downloading file...", true); 
      new Thread(new Runnable() { 
       public void run() { 
         downloadFile(dwnload_file_path, dest_file_path); 
       } 
       }).start();    
    } 

    public void downloadFile(String url, String dest_file_path) { 
     try { 
      File dest_file = new File(dest_file_path); 
      URL u = new URL(url); 
      URLConnection conn = u.openConnection(); 
      int contentLength = conn.getContentLength(); 

      DataInputStream stream = new DataInputStream(u.openStream()); 

      byte[] buffer = new byte[contentLength]; 
      stream.readFully(buffer); 
      stream.close(); 

      DataOutputStream fos = new DataOutputStream(new FileOutputStream(dest_file)); 
      fos.write(buffer); 
      fos.flush(); 
      fos.close(); 
      hideProgressIndicator(); 

     } catch(FileNotFoundException e) { 
      hideProgressIndicator(); 
      return; 
     } catch (IOException e) { 
      hideProgressIndicator(); 
      return; 
     } 
    } 

    void hideProgressIndicator(){ 
     runOnUiThread(new Runnable() { 
      public void run() { 
       dialog.dismiss(); 
      } 
     }); 
    } 
} 
+0

感謝:D這不是我想要的,但它是我的項目的另一部分:D –