0

我是android编程的初学者。

我在使用 android 下载文件时遇到了一些问题

我使用了Httpost,Httpget和hhtpurlconnection,第一个根本不工作,第三个无法下载两次

我想要一种将不同的 xml 下载到字符串或输入流(或可转换为它们的东西)来解析这些 XML 的方法。除了该方法应该能够做这样的事情:

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

因为 xmls 是来自 API 的响应

4

2 回答 2

2

在这里,我举一个如何从服务器下载图像文件的示例。我假设在您的本地服务器上有一个图片文件夹,并且您正在从中下载图片..使用以下代码可能会对您有所帮助..

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();
           }
       });  
   }
}
于 2013-07-05T11:17:02.403 回答
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;

  }
于 2013-07-05T11:07:50.823 回答