2013-03-22 53 views
0

我的代码如下,从url下载xml内容,这需要花费更多时间在wifi网络下载,我的xml只有29.2kb。我使用AsyncTask来做到这一点。从URL下载xml作为Inputstream的最佳方式

InputStream getInputStreamForUrl(String url) { 
     BufferedHttpEntity bufferedEntity = null; 
     InputStream is = null; 
     try { 
      bufferedEntity = download(url); 
      if (bufferedEntity != null) { 
       is = bufferedEntity.getContent(); 
       if (is != null) { 
        BufferedReader feedReader = new BufferedReader(new InputStreamReader(is, Utility.UTF_ENCODING), 
          16 * 1024); 
        Utility.cacheFeed(feedReader, url); 
       } 
      } 
     } catch (NetworkNotAccessable e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (bufferedEntity != null) { 
        bufferedEntity.consumeContent(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return (url != null) ? Utility.getInputStreamForCache(url) : null; 
    } 

下载(网址)方法即时通讯使用HTTPGET请求如下:

public BufferedHttpEntity download(String url) 
      throws ClientProtocolException, IOException, 
        IllegalStateException, NetworkNotAccessable { 
     HttpGet get = new HttpGet(url); 
     HttpResponse response = mDefaultHttpClient.execute(get); 
     int status = response.getStatusLine().getStatusCode(); 
     if (status != 200) { 
      throw new NetworkNotAccessable(url + "error code:" + status); 
     } 
     HttpEntity entity = response.getEntity();   
     BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);  

     while (bufHttpEntity.isStreaming()) { 
      try { 
       bufHttpEntity.wait(500); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
     return bufHttpEntity; 
    } 

请让我知道有没有拉上拉链整个网址并下载任何最好的方式。

+0

请地方Utility.cacheFeed和Utility.getInputStreamForCache(URL)的代码 – 2013-03-22 10:33:07

+0

如果您能够更改服务器代码,请[gzip](http://en.wikipedia.org/wiki/Gzip)xml响应,并在您的客户端使用[AndroidHttpClient](http://developer.android .com/reference/android/net/http/AndroidHttpClient.html)。这是我如何尽量减少下载时间。 – hgoz 2013-03-22 10:38:42

+0

@user_CC Utility.cacheFeed和Utility.getInputStreamForCache(url)是用于缓存Bufferreader并将url作为键的方法,并通过提供url来获取InputStream。我将在下载Inputstream之后更新本地chche。下载(String url)是正在发生下载的方法。 – Adi 2013-03-22 12:03:09

回答

2

如果您在'download(url)'方法中发生实际下载,那么恐怕我看不到发生这种情况,同时也会从getInputStream方法调用下载方法,您将返回输入流不能看到任何原因...

此外,为什么你使用bufHttpEntity.wait(500);这是一个阻塞状态(可造成重大延误)

使用下面的代码在你的下载方法中检索的XML:

URL url = new URL(url); 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
     try { 
     InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
     byte buffer[] = new byte[4096]; 

     int count; 
     String xmlData = ""; 
     while((count = in.read(buffer)) != -1){ 
      xmlData += new String(buffer, 0, count); 
     } finally { 
     urlConnection.disconnect(); 
     } 


      Log.d(TAG, " Data: " + xmlData); 
+0

非常感谢,它帮助我缩短了下载时间 – Adi 2013-03-27 05:37:34

+0

@Adi请您接受答案..很多谢谢 – 2013-03-27 08:54:00