2013-03-22 49 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