2011-09-09 32 views
9

Có ai biết cách yêu cầu phạm vi byte cùng với yêu cầu HTTP không? Tôi đang tìm cách tạo điều kiện cho việc tiếp tục tải xuống trong ứng dụng của chúng tôi bằng cách yêu cầu một phạm vi byte nơi tải xuống đã tắt và đọc InputStream của nó từ getContent().Apache HttpClient nhận thêm tiêu đề phạm vi byte?

Tôi đã thử lặp lại các tiêu đề nhưng chúng không có giá trị. Nguồn dưới đây.

import java.io.IOException; 
import java.io.InputStream; 

import org.apache.http.Header; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.util.Log; 

/** 
* @author Kevin Kowalewski 
* 
*/ 
public class DownloadClient { 
DefaultHttpClient httpClient; 
HttpGet httpGet; 
HttpResponse httpResponse; 
HttpEntity httpEntity; 
InputStream httpInputStream; 

private static String LOG_TAG = DownloadClient.class.getName(); 

public DownloadClient(){ 
    httpClient = new DefaultHttpClient(); 
    httpGet = new HttpGet("http://cachefly.cachefly.net/100mb.test"); 
    for (Header header : httpGet.getAllHeaders()){ 
     Log.d(LOG_TAG, "--> Header: " + header); 
    } 

    Log.d(LOG_TAG, "--> Header size is: " + httpGet.getAllHeaders().length); 
    try { 
     httpResponse = httpClient.execute(httpGet); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    httpEntity = httpResponse.getEntity(); 
    try { 
     httpInputStream = httpEntity.getContent(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    Log.d(LOG_TAG, "--> StatusLine: " + httpResponse.getStatusLine()); 
} 

public void read(){ 
    byte[] readBuffer = new byte[32]; 
    try { 
     while (httpInputStream.read(readBuffer) != -1){ 
      try{Thread.sleep(100);}catch(Exception e){} 
      //Log.d(LOG_TAG,"--> Read Bytes: " + new String(readBuffer)); 
     }; 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

public void shutdown(){ 
    httpGet.abort(); 
    httpClient.getConnectionManager().shutdown(); 
} 

}

Kevin

Trả lời

12

tôi đã có thể làm việc này bằng cách thêm dòng sau:

httpGet.addHeader("Range", "bytes=0-0"); 
Các vấn đề liên quan