2012-05-21 54 views
-2

任何人都知道如何使用Android中的DefaultHttpClient從網上下載文件?
我試過最可能的選項,是的OpenURL連接,但我需要使用DefaultHttpClient提前使用DefaultHttpClient從網上下載文件

+0

這張圖片來自你的服務器。如果是,那麼請問,然後發送它作爲base64字符串值,並將該base64字符串轉換爲您的應用程序中的圖像 –

回答

0

謝謝,我可以舉一個例子可以幫助ü肯定 的HttpClient HttpClient的=新DefaultHttpClient();

// Prepare a request object 
HttpGet httpget = new HttpGet("http://www.apache.org/"); 

// Execute the request 
HttpResponse response = httpclient.execute(httpget); 

// Examine the response status 
System.out.println(response.getStatusLine()); 

// Get hold of the response entity 
HttpEntity entity = response.getEntity(); 

// If the response does not enclose an entity, there is no need 
// to worry about connection release 
if (entity != null) { 
    InputStream instream = entity.getContent(); 
    try { 

     BufferedReader reader = new BufferedReader(
       new InputStreamReader(instream)); 
     // do something useful with the response 
     System.out.println(reader.readLine()); 

    } catch (IOException ex) { 

     // In case of an IOException the connection will be released 
     // back to the connection manager automatically 
     throw ex; 

    } catch (RuntimeException ex) { 

     // In case of an unexpected exception you may want to abort 
     // the HTTP request in order to shut down the underlying 
     // connection and release it back to the connection manager. 
     httpget.abort(); 
     throw ex; 

    } finally { 

     // Closing the input stream will trigger connection release 
     instream.close(); 

    } 

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure 
    // immediate deallocation of all system resources 
    httpclient.getConnectionManager().shutdown(); 
}