0

xml コンテンツを URL としてダウンロードするための私のコードは次のとおりです。これは、wifi ネットワークでのダウンロードに時間がかかります。私の xml はわずか 29.2kb です。これに AsyncTask を使用しています。

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;
    }

download(url) メソッドは、次のように 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;
    }

URL全体を圧縮してダウンロードする最善の方法があれば教えてください。

4

1 に答える 1

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 );
于 2013-03-22T13:59:10.470 に答える