Example usage for org.apache.http.impl.conn SingleClientConnManager shutdown

List of usage examples for org.apache.http.impl.conn SingleClientConnManager shutdown

Introduction

In this page you can find the example usage for org.apache.http.impl.conn SingleClientConnManager shutdown.

Prototype

public void shutdown() 

Source Link

Usage

From source file:fm.last.android.player.StreamProxy.java

private void processRequest(HttpRequest request, Socket client) throws IllegalStateException, IOException {
    if (request == null) {
        return;//from ww w  . j a va2 s .c o  m
    }
    Log.d(LOG_TAG, "processing");
    String url = request.getRequestLine().getUri();

    DefaultHttpClient seed = new DefaultHttpClient();
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    SingleClientConnManager mgr = new MyClientConnManager(seed.getParams(), registry);
    DefaultHttpClient http = new DefaultHttpClient(mgr, seed.getParams());
    HttpGet method = new HttpGet(url);
    for (Header h : request.getAllHeaders()) {
        method.addHeader(h);
    }
    HttpResponse realResponse = null;
    try {
        Log.d(LOG_TAG, "starting download");
        realResponse = http.execute(method);
        Log.d(LOG_TAG, "downloaded");
    } catch (ClientProtocolException e) {
        Log.e(LOG_TAG, "Error downloading", e);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error downloading", e);
    }

    if (realResponse == null) {
        return;
    }

    if (!isRunning)
        return;

    Log.d(LOG_TAG, "downloading...");

    InputStream data = realResponse.getEntity().getContent();
    StatusLine line = realResponse.getStatusLine();
    HttpResponse response = new BasicHttpResponse(line);
    response.setHeaders(realResponse.getAllHeaders());

    Log.d(LOG_TAG, "reading headers");
    StringBuilder httpString = new StringBuilder();
    httpString.append(response.getStatusLine().toString());

    httpString.append("\n");
    for (Header h : response.getAllHeaders()) {
        httpString.append(h.getName()).append(": ").append(h.getValue()).append("\n");
    }
    httpString.append("\n");
    Log.d(LOG_TAG, "headers done");

    try {
        byte[] buffer = httpString.toString().getBytes();
        int readBytes;
        Log.d(LOG_TAG, "writing to client");
        client.getOutputStream().write(buffer, 0, buffer.length);

        // Start streaming content.
        byte[] buff = new byte[8192];
        while (isRunning && (readBytes = data.read(buff, 0, buff.length)) != -1) {
            client.getOutputStream().write(buff, 0, readBytes);
        }
    } catch (Exception e) {
        Log.e("", e.getMessage(), e);
    } finally {
        mgr.shutdown();
        client.close();
        Log.d(LOG_TAG, "streaming complete");
    }
}