Search in sources :

Example 11 with AsyncHttpGet

use of com.koushikdutta.async.http.AsyncHttpGet in project AndroidAsync by koush.

the class HttpClientTests method testGithubHelloWithFuture.

public void testGithubHelloWithFuture() throws Exception {
    Future<String> string = client.executeString(new AsyncHttpGet("https://" + githubPath + "hello.txt"), null);
    assertEquals(string.get(TIMEOUT, TimeUnit.MILLISECONDS), "hello world");
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet)

Example 12 with AsyncHttpGet

use of com.koushikdutta.async.http.AsyncHttpGet in project AndroidAsync by koush.

the class HttpClientTests method testProxy.

public void testProxy() throws Exception {
    wasProxied = false;
    final AsyncServer proxyServer = new AsyncServer();
    try {
        AsyncProxyServer httpServer = new AsyncProxyServer(proxyServer) {

            @Override
            protected boolean onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
                wasProxied = true;
                return super.onRequest(request, response);
            }
        };
        AsyncServerSocket socket = httpServer.listen(proxyServer, 0);
        //            client.getSocketMiddleware().enableProxy("localhost", 5555);
        AsyncHttpGet get = new AsyncHttpGet("http://www.clockworkmod.com");
        get.enableProxy("localhost", socket.getLocalPort());
        Future<String> ret = client.executeString(get, null);
        String data;
        assertNotNull(data = ret.get(TIMEOUT, TimeUnit.MILLISECONDS));
        assertTrue(data.contains("ClockworkMod"));
        assertTrue(wasProxied);
    } finally {
        proxyServer.stop();
    }
}
Also used : AsyncServerSocket(com.koushikdutta.async.AsyncServerSocket) AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncServer(com.koushikdutta.async.AsyncServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) AsyncProxyServer(com.koushikdutta.async.http.server.AsyncProxyServer)

Example 13 with AsyncHttpGet

use of com.koushikdutta.async.http.AsyncHttpGet in project AndroidAsync by koush.

the class Issue59 method testIssue.

public void testIssue() throws Exception {
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.get("/", new HttpServerRequestCallback() {

            @Override
            public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
                // setting this to empty is a hacky way of telling the framework not to use
                // transfer-encoding. It will get removed.
                response.getHeaders().set("Transfer-Encoding", "");
                response.code(200);
                Util.writeAll(response, "foobarbeepboop".getBytes(), new CompletedCallback() {

                    @Override
                    public void onCompleted(Exception ex) {
                        response.end();
                    }
                });
            }
        });
        httpServer.listen(5959);
        AsyncHttpGet get = new AsyncHttpGet("http://localhost:5959/");
        get.setLogging("issue59", Log.VERBOSE);
        get.getHeaders().removeAll("Connection");
        get.getHeaders().removeAll("Accept-Encoding");
        assertEquals("foobarbeepboop", AsyncHttpClient.getDefaultInstance().executeString(get, null).get(1000, TimeUnit.MILLISECONDS));
    } finally {
        httpServer.stop();
        AsyncServer.getDefault().stop();
    }
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse)

Example 14 with AsyncHttpGet

use of com.koushikdutta.async.http.AsyncHttpGet in project simperium-android by Simperium.

the class AsyncWebSocketProvider method connect.

@Override
public void connect(final WebSocketManager.ConnectionListener listener) {
    Uri uri = Uri.parse(String.format(AndroidClient.WEBSOCKET_URL, mAppId));
    AsyncHttpRequest request = new AsyncHttpGet(uri);
    request.setHeader(AndroidClient.USER_AGENT_HEADER, mSessionId);
    // Protocol is null
    mAsyncClient.websocket(request, null, new WebSocketConnectCallback() {

        @Override
        public void onCompleted(Exception ex, final WebSocket webSocket) {
            if (ex != null) {
                listener.onError(ex);
                return;
            }
            if (webSocket == null) {
                listener.onError(new IOException("WebSocket could not be opened"));
                return;
            }
            final WebSocketManager.Connection connection = new WebSocketManager.Connection() {

                @Override
                public void close() {
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            webSocket.close();
                        }
                    });
                }

                @Override
                public void send(final String message) {
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            webSocket.send(message);
                        }
                    });
                }
            };
            webSocket.setStringCallback(new WebSocket.StringCallback() {

                @Override
                public void onStringAvailable(String s) {
                    listener.onMessage(s);
                }
            });
            webSocket.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    listener.onDisconnect(ex);
                }
            });
            webSocket.setClosedCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    listener.onDisconnect(ex);
                }
            });
            listener.onConnect(connection);
        }
    });
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) IOException(java.io.IOException) Uri(android.net.Uri) IOException(java.io.IOException) WebSocket(com.koushikdutta.async.http.WebSocket) WebSocketConnectCallback(com.koushikdutta.async.http.AsyncHttpClient.WebSocketConnectCallback) AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Aggregations

AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)14 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)4 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)4 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)3 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)3 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)3 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)3 CancellationException (java.util.concurrent.CancellationException)3 ExecutionException (java.util.concurrent.ExecutionException)3 Semaphore (java.util.concurrent.Semaphore)3 TimeoutException (java.util.concurrent.TimeoutException)3 AsyncServer (com.koushikdutta.async.AsyncServer)2 AsyncServerSocket (com.koushikdutta.async.AsyncServerSocket)2 ByteBufferList (com.koushikdutta.async.ByteBufferList)2 AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)2 File (java.io.File)2 Uri (android.net.Uri)1 DataEmitter (com.koushikdutta.async.DataEmitter)1 DataCallback (com.koushikdutta.async.callback.DataCallback)1 FutureCallback (com.koushikdutta.async.future.FutureCallback)1