Search in sources :

Example 1 with AsyncHttpClient

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

the class SSLTests method disabled__testClientCertificateIssue163.

public void disabled__testClientCertificateIssue163() throws Exception {
    // https://security.springthroughtest.com/hello.json
    AsyncServer server = new AsyncServer();
    try {
        AsyncHttpClient client = new AsyncHttpClient(server);
        JSONObject json = client.executeJSONObject(new AsyncHttpGet("https://security.springthroughtest.com/hello.json"), null).get();
    } finally {
        server.stop();
    }
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) JSONObject(org.json.JSONObject) AsyncServer(com.koushikdutta.async.AsyncServer) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

Example 2 with AsyncHttpClient

use of com.koushikdutta.async.http.AsyncHttpClient in project K6nele by Kaljurand.

the class WebSocketRecognitionService method startSocket.

/**
     * Opens the socket and starts recording/sending.
     *
     * @param url Webservice URL
     */
void startSocket(String url) {
    mIsEosSent = false;
    Log.i(url);
    AsyncHttpClient client = AsyncHttpClient.getDefaultInstance();
    if (false) {
        //http://stackoverflow.com/questions/37804816/androidasync-how-to-create-ssl-client-in-websocket-connection
        AsyncSSLSocketMiddleware sslSocketMiddleware = new AsyncSSLSocketMiddleware(client);
        SSLContext sslContext = null;
        try {
            sslContext = getSSLContext();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        sslSocketMiddleware.setSSLContext(sslContext);
        client.insertMiddleware(sslSocketMiddleware);
    }
    client.websocket(url, PROTOCOL, new AsyncHttpClient.WebSocketConnectCallback() {

        @Override
        public void onCompleted(Exception ex, final WebSocket webSocket) {
            mWebSocket = webSocket;
            if (ex != null) {
                handleException(ex);
                return;
            }
            webSocket.setStringCallback(new WebSocket.StringCallback() {

                public void onStringAvailable(String s) {
                    Log.i(s);
                    handleResult(s);
                }
            });
            webSocket.setClosedCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    if (ex == null) {
                        Log.e("ClosedCallback");
                        handleFinish(mIsEosSent);
                    } else {
                        Log.e("ClosedCallback: ", ex);
                        handleException(ex);
                    }
                }
            });
            webSocket.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    if (ex == null) {
                        Log.e("EndCallback");
                        handleFinish(mIsEosSent);
                    } else {
                        Log.e("EndCallback: ", ex);
                        handleException(ex);
                    }
                }
            });
            startSending(webSocket);
        }
    });
}
Also used : AsyncSSLSocketMiddleware(com.koushikdutta.async.http.AsyncSSLSocketMiddleware) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient) WebSocket(com.koushikdutta.async.http.WebSocket)

Example 3 with AsyncHttpClient

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

the class CacheTests method testMaxAgePrivate.

public void testMaxAgePrivate() throws Exception {
    AsyncHttpClient client = new AsyncHttpClient(AsyncServer.getDefault());
    ResponseCacheMiddleware cache = ResponseCacheMiddleware.addCache(client, new File(getContext().getFilesDir(), "AndroidAsyncTest"), 1024 * 1024 * 10);
    AsyncHttpServer httpServer = new AsyncHttpServer();
    try {
        httpServer.get("/uname/(.*)", new HttpServerRequestCallback() {

            @Override
            public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
                response.getHeaders().set("Date", HttpDate.format(new Date()));
                response.getHeaders().set("Cache-Control", "private, max-age=10000");
                response.send(request.getMatcher().group(1));
            }
        });
        AsyncServerSocket socket = httpServer.listen(AsyncServer.getDefault(), 0);
        int port = socket.getLocalPort();
        // clear the old cache
        cache.clear();
        client.executeString(new AsyncHttpGet("http://localhost:" + port + "/uname/43434"), null).get();
        client.executeString(new AsyncHttpGet("http://localhost:" + port + "/uname/43434"), null).get();
        assertEquals(cache.getCacheHitCount(), 1);
        assertEquals(cache.getNetworkCount(), 1);
    } finally {
        AsyncServer.getDefault().stop();
        client.getMiddleware().remove(cache);
    }
}
Also used : AsyncServerSocket(com.koushikdutta.async.AsyncServerSocket) AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) HttpServerRequestCallback(com.koushikdutta.async.http.server.HttpServerRequestCallback) AsyncHttpServerRequest(com.koushikdutta.async.http.server.AsyncHttpServerRequest) ResponseCacheMiddleware(com.koushikdutta.async.http.cache.ResponseCacheMiddleware) AsyncHttpServer(com.koushikdutta.async.http.server.AsyncHttpServer) AsyncHttpServerResponse(com.koushikdutta.async.http.server.AsyncHttpServerResponse) File(java.io.File) Date(java.util.Date) HttpDate(com.koushikdutta.async.http.HttpDate) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient)

Aggregations

AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)3 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)2 AsyncServer (com.koushikdutta.async.AsyncServer)1 AsyncServerSocket (com.koushikdutta.async.AsyncServerSocket)1 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 AsyncSSLSocketMiddleware (com.koushikdutta.async.http.AsyncSSLSocketMiddleware)1 HttpDate (com.koushikdutta.async.http.HttpDate)1 WebSocket (com.koushikdutta.async.http.WebSocket)1 ResponseCacheMiddleware (com.koushikdutta.async.http.cache.ResponseCacheMiddleware)1 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)1 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)1 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)1 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)1 File (java.io.File)1 IOException (java.io.IOException)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 Date (java.util.Date)1 TimeoutException (java.util.concurrent.TimeoutException)1