HttpService - not able to play online videos

371 Views Asked by At

I am using HttpService to send responses to android devices and browsers to share some files. This service works on android device. Everything works fine exclude video files. Listening to online music works perfect. But when I try to watch online video my WebService fails with error which you can see below:

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)

Some code snippet of server side:

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

And I use following code in the client side to watch video or listen to music:

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

What am I doing wrong? Any help will be appreciated!

1

There are 1 best solutions below

0
On

httpcore did not support Content-Range,you can add it.