我一直在我们的一个应用程序中使用NanoHTTPD来提供内容,包括从本地SDCard到Webview的音频和视频。已正确配置内容范围和内容长度标头以及HTTP状态。现在,我们有一个用例,我们希望通过NanoHTTPD在服务器上提供内容。
NanoHTTPD方法的问题在于它读取Webview请求的完整内容。如果是本地文件,它仍然是正常的,但您无法等待它从服务器获取如此多的内容并刷新输出流。
我正在寻找一种方法,我可以打开一个连接并继续提供所请求数据的一部分。就像在请求中使用范围标题获取内容时一样。连接保持打开状态,只要有足够的缓冲区,视频播放器就会播放。
请帮忙。
答案 0 :(得分:0)
我通过使用HTTP Client解决了这个问题。我在localhost上使用唯一端口注册了一个模式,并通过添加适当的标头,状态和内容长度来处理大型媒体的响应。这里有三件事要做:
1)将HTTP响应中的标题复制到本地响应
2)设置响应代码。全部内容(200)或部分内容(206)
3)创建一个新的InputStreamEntity并将其添加到响应
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
String range = null;
//Check if there's range in request header
Header rangeHeader = request.getFirstHeader("range");
if (rangeHeader != null) {
range = rangeHeader.getValue();
}
URL url = new URL(mediaURL);
URLConnection urlConn = url.openConnection();
if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException("URL is not an Http URL");
}
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.setRequestMethod("GET");
//If range is present, direct HTTPConnection to fetch data for that range only
if(range!=null){
httpConn.setRequestProperty("Range",range);
}
//Add any custom header to request that you want and then connect.
httpConn.connect();
int statusCode = httpConn.getResponseCode();
//Copy all headers with valid key to response. Exclude content-length as that's something response gets from the entity.
Map<String, List<String>> headersMap = httpConn.getHeaderFields();
for (Map.Entry<String, List<String>> entry : headersMap.entrySet())
{
if(entry.getKey() != null && !entry.getKey().equalsIgnoreCase("content-length")) {
for (int i = 0; i < entry.getValue().size(); i++) {
response.setHeader(entry.getKey(), entry.getValue().get(i));
}
}
}
//Important to set correct status code
response.setStatusCode(statusCode);
//Pass the InputStream to response and that's it.
InputStreamEntity entity = new InputStreamEntity(httpConn.getInputStream(), httpConn.getContentLength());
entity.setContentType(httpConn.getContentType());
response.setEntity(entity);
}