HttpService - 无法播放在线视频

时间:2013-08-07 14:17:34

标签: android servlets webserver video-streaming httpservice

我正在使用HttpService向Android设备和浏览器发送回复以共享一些文件。此服务适用于Android设备。一切正常,排除视频文件。聆听在线音乐作品非常完美。但是,当我尝试观看在线视频时,WebService失败并出现错误,您可以在下面看到:

java.net.SocketException: sendto failed: EPIPE (Broken pipe)
at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506)
at libcore.io.IoBridge.sendto(IoBridge.java:475)
at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)
at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
at org.apache.http.impl.io.AbstractSessionOutputBuffer.write(AbstractSessionOutputBuffer.java:109)
at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:113)
at org.apache.http.entity.FileEntity.writeTo(FileEntity.java:83)
at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:97)
at org.apache.http.impl.AbstractHttpServerConnection.sendResponseEntity(AbstractHttpServerConnection.java:182)
at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:209)
at com.test.communication.server.WebServer$1.run(WebServer.java:94)
at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)
at libcore.io.Posix.sendtoBytes(Native Method)
at libcore.io.Posix.sendto(Posix.java:146)
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
at libcore.io.IoBridge.sendto(IoBridge.java:473)

服务器端的一些代码片段:

WebServer.java

private BasicHttpProcessor httpproc = null;
private BasicHttpContext httpContext = null;
private HttpService httpService = null;
private HttpRequestHandlerRegistry registry = null;

public WebServer(Context context) {
    this.setContext(context);
    httpproc = new BasicHttpProcessor();
    httpContext = new BasicHttpContext();       
    httpproc.addInterceptor(new ResponseDate());
    httpproc.addInterceptor(new ResponseServer());
    httpproc.addInterceptor(new ResponseContent());
    httpproc.addInterceptor(new ResponseConnControl());
    httpService = new HttpService(httpproc,
        new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory());        
    registry = new HttpRequestHandlerRegistry(); 
    registry.register(MOBILE_BASE_PATTERN, new MobileFileBrowseHandler(context));

    httpService.setHandlerResolver(registry);
}

public void runServer() {
    new Thread(new Runnable() {         
        public void run() {
            try {                   
                serverSocket = new ServerSocket(Constants.DEFAULT_SERVER_PORT);     
                serverSocket.setReuseAddress(true);                     
                        while (RUNNING) {                               
                            try {                                   
                                final Socket socket = serverSocket.accept();                                    
                                DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();                                                                       
                                serverConnection.bind(socket, new BasicHttpParams());

                                httpService.handleRequest(serverConnection, httpContext); //ERROR UCCURS!!!     
                                serverConnection.shutdown();
                            } catch (IOException e) {
                                e.printStackTrace();
                            } catch (HttpException e) {
                                e.printStackTrace();
                            }
                        }           
                serverSocket.close();                   
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }       
            RUNNING = false;
        }
    }).start();
}

MobileFileBrowseHandler.java

public void handle(HttpRequest request, HttpResponse response,
        HttpContext context) throws HttpException, IOException {
    HttpEntity entity = null;       
    String uriString = request.getRequestLine().getUri().substring(13);      

contentType = URLConnection.guessContentTypeFromName(file.getAbsolutePath()); 

                HttpEntity entity = new FileEntity(file, contentType);          
                response.setHeader("Content-Type", contentType);            
    response.setEntity(entity);
}

我在客户端使用以下代码来观看视频或听音乐:

public static void IntentPlayer(Context context, Uri uri, int mode){
    String type;
    switch(mode){
        case 1: type = "video/*"; break;
        case 2: type = "audio/*"; break;            
        default: return;
    }
    Intent intent = new Intent();   
    intent.setAction(android.content.Intent.ACTION_VIEW); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);             
    intent.setDataAndType(uri, type);   
    context.startActivity(intent);
}

我做错了什么?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

httpcore不支持Content-Range,您可以添加它。