当前位置: 首页>>代码示例>>Java>>正文


Java AsyncHttpClient类代码示例

本文整理汇总了Java中com.koushikdutta.async.http.AsyncHttpClient的典型用法代码示例。如果您正苦于以下问题:Java AsyncHttpClient类的具体用法?Java AsyncHttpClient怎么用?Java AsyncHttpClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


AsyncHttpClient类属于com.koushikdutta.async.http包,在下文中一共展示了AsyncHttpClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: uploadFile

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
public static Future<?> uploadFile(Request request, final ResponseCallback responseCallback) {
    StringBuilder stringBuffer = new StringBuilder(request.getURL());
    if (request.getURLParams() != null) {
        String params = HttpUtils.getRequestParams(request.getURLParams());
        if (!TextUtils.isEmpty(params)) {
            stringBuffer.append("?").append(params);
        }
    }
    if (BuildConfig.DEBUG) {
        LogUtils.d(stringBuffer.toString());
    }
    AsyncHttpPost asyncHttpPost = new AsyncHttpPost(stringBuffer.toString());
    HashMap<String, File> file = request.getFileBody();
    if (file != null && !file.isEmpty()) {
        asyncHttpPost.setBody(new FileBody(file.entrySet().iterator().next().getValue()));
    }
    addHead(request.getHeader(), asyncHttpPost);
    return asyncHttpClient.executeString(asyncHttpPost, new AsyncHttpClient.StringCallback() {
        @Override
        public void onCompleted(Exception e, AsyncHttpResponse asyncHttpResponse, String s) {
            if (responseCallback != null) {
                responseCallback.onCompleted(e, asyncHttpResponse, s);
            }
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:27,代码来源:HttpApi.java

示例2: send

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
@Override
public void send(String message) {
    if (message.startsWith("5")) {
        postMessage(message);
        return;
    }

    AsyncHttpRequest request = new AsyncHttpPost(computedRequestUrl());
    request.setBody(new StringBody(message));

    client.executeString(request, new AsyncHttpClient.StringCallback() {
        @Override
        public void onCompleted(Exception e, AsyncHttpResponse source, String result) {
            if (e != null) {
                close(e);
                return;
            }

            sendResult(result);
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:23,代码来源:XHRPollingTransport.java

示例3: testSendAcknowledge

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
public void testSendAcknowledge() throws Exception {
    final TriggerFuture trigger = new TriggerFuture();
    SocketIOClient client = SocketIOClient.connect(AsyncHttpClient.getDefaultInstance(), "http://koush.clockworkmod.com:8080/", null).get();

    client.setStringCallback(new StringCallback() {
        boolean isEcho = true;
        @Override
        public void onString(String string, Acknowledge acknowledge) {
            if (!isEcho) {
                trigger.trigger("hello".equals(string));
                return;
            }
            assertNotNull(acknowledge);
            isEcho = false;
            acknowledge.acknowledge(new JSONArray().put(string));
        }
    });

    client.emit("hello");

    assertTrue(trigger.get(TIMEOUT, TimeUnit.MILLISECONDS));
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:23,代码来源:SocketIOTests.java

示例4: testEndpoint

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
public void testEndpoint() throws Exception {
    final TriggerFuture trigger = new TriggerFuture();
    SocketIOClient.connect(AsyncHttpClient.getDefaultInstance(), new SocketIORequest("http://koush.clockworkmod.com:8080/", "/chat"), new ConnectCallback() {
        @Override
        public void onConnectCompleted(Exception ex, SocketIOClient client) {
            assertNull(ex);
            client.setStringCallback(new StringCallback() {
                @Override
                public void onString(String string, Acknowledge acknowledge) {
                    trigger.trigger("hello".equals(string));
                }
            });
            client.emit("hello");
        }
    });
    assertTrue(trigger.get(TIMEOUT, TimeUnit.MILLISECONDS));
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:18,代码来源:SocketIOTests.java

示例5: testMaxAgePrivate

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
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);
    }
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:33,代码来源:CacheTests.java

示例6: testRelativeRedirect

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
public void testRelativeRedirect() throws Exception {
    String ret = AsyncHttpClient.getDefaultInstance()
    .executeString(new AsyncHttpGet("http://localhost:6003/foo/bar"), null)
    .get();

    assertEquals(ret, "SUCCESS!");

    ret = AsyncHttpClient.getDefaultInstance()
    .executeString(new AsyncHttpGet("http://localhost:6003/foo"), null)
    .get();

    assertEquals(ret, "BORAT!");

    ret = AsyncHttpClient.getDefaultInstance()
    .executeString(new AsyncHttpGet("http://localhost:6003/foo/poo"), null)
    .get();

    assertEquals(ret, "SWEET!");
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:20,代码来源:RedirectTests.java

示例7: testWebSocket

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
public void testWebSocket() throws Exception {
    final Semaphore semaphore = new Semaphore(0);

    AsyncHttpClient.getDefaultInstance().websocket("http://localhost:5000/ws", null, new WebSocketConnectCallback() {
        @Override
        public void onCompleted(Exception ex, WebSocket webSocket) {
            webSocket.send("hello");
            webSocket.setStringCallback(new StringCallback() {
                @Override
                public void onStringAvailable(String s) {
                    assertEquals(s, "hello");
                    semaphore.release();
                }
            });
        }
    });
    
    assertTrue(semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:20,代码来源:WebSocketTests.java

示例8: fetchContacts

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
public void fetchContacts(final ContactProviderListener callback) {
    createClient().executeString(createGetRequest(), new AsyncHttpClient.StringCallback() {
        @Override
        public void onCompleted(Exception e, AsyncHttpResponse asyncHttpResponse, String result) {
            if (result != null) {
                try {
                    callback.onContacts(gson.fromJson(result, Contacts.class));
                } catch (JsonSyntaxException jse) {
                    callback.onError(jse);
                }
            } else {
                callback.onError(e);
            }
        }
    });
}
 
开发者ID:lavong,项目名称:SpockUnitTestsDemo,代码行数:17,代码来源:ContactsProvider.java

示例9: executePostJsonObject

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
public static Future<?> executePostJsonObject(Request request, final ResponseCallback responseCallback) throws JSONException {
    StringBuilder stringBuilder = new StringBuilder(64);
    String params = HttpUtils.getRequestParams(request.getURLParams());
    if (!TextUtils.isEmpty(params)) {
        stringBuilder.append("?").append(params);
    }
    AsyncHttpPost asyncHttpPost = new AsyncHttpPost(stringBuilder.toString());
    JSONObject jsonObject;
    if (request.getBodyParams() != null) {
        jsonObject = new JSONObject(request.getBodyParams());
    } else {
        jsonObject = new JSONObject("{}");
    }
    AsyncHttpRequestBody<JSONObject> asyncHttpRequestBody = new JSONObjectBody(jsonObject);
    asyncHttpPost.setBody(asyncHttpRequestBody);
    addHead(request.getHeader(), asyncHttpPost);
    return asyncHttpClient.executeString(asyncHttpPost, new AsyncHttpClient.StringCallback() {
        @Override
        public void onCompleted(Exception e, AsyncHttpResponse asyncHttpResponse, String s) {
            if (responseCallback != null) {
                responseCallback.onCompleted(e, asyncHttpResponse, s);
            }
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:26,代码来源:HttpApi.java

示例10: executePostJsonArray

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
public static Future<?> executePostJsonArray(Request request, final ResponseCallback responseCallback) throws JSONException {
    StringBuilder builder = new StringBuilder(request.getURL());
    String params = HttpUtils.getRequestParams(request.getURLParams());
    if (!TextUtils.isEmpty(params)) {
        builder.append("?").append(params);
    }
    if (BuildConfig.DEBUG) {
        LogUtils.d(builder.toString());
    }
    AsyncHttpPost asyncHttpPost = new AsyncHttpPost(builder.toString());
    JSONArray jsonArray;
    if (request.getArray() != null) {
        jsonArray = new JSONArray(request.getArray());
    } else {
        jsonArray = new JSONArray("[]");
    }
    AsyncHttpRequestBody<JSONArray> asyncHttpRequestBody = new JSONArrayBody(jsonArray);
    asyncHttpPost.setBody(asyncHttpRequestBody);
    addHead(request.getHeader(), asyncHttpPost);

    return asyncHttpClient.executeString(asyncHttpPost, new AsyncHttpClient.StringCallback() {
        @Override
        public void onCompleted(Exception e, AsyncHttpResponse asyncHttpResponse, String s) {
            if (responseCallback != null) {
                responseCallback.onCompleted(e, asyncHttpResponse, s);
            }
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:30,代码来源:HttpApi.java

示例11: executePost

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
public static Future<?> executePost(Request request, final ResponseCallback responseCallback) {
    AsyncHttpPost asyncHttpPost = new AsyncHttpPost(request.getURL());
    String body;
    if (request.getBodyParams() != null) {
        if (BuildConfig.DEBUG) {
            LogUtils.d(request.getBodyParams().toString());
        }
        body = HttpUtils.getRequestParams(request.getBodyParams());
    } else {
        body = "";
    }
    AsyncHttpRequestBody<String> asyncHttpRequestBody = new StringBody(body);
    asyncHttpPost.setBody(asyncHttpRequestBody);
    addHead(request.getHeader(), asyncHttpPost);
    return asyncHttpClient.executeString(asyncHttpPost, new AsyncHttpClient.StringCallback() {
        @Override
        public void onCompleted(Exception e, AsyncHttpResponse asyncHttpResponse, String s) {
            if (responseCallback != null) {
                responseCallback.onCompleted(e, asyncHttpResponse, s);
            }
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:24,代码来源:HttpApi.java

示例12: executeGet

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
public static Future<?> executeGet(Request request, final ResponseCallback responseCallback) {
    StringBuilder stringBuffer = new StringBuilder(request.getURL());
    if (request.getURLParams() != null) {
        String params = HttpUtils.getRequestParams(request.getURLParams());
        if (!TextUtils.isEmpty(params)) {
            stringBuffer.append("?").append(params);
        }
    }
    if (BuildConfig.DEBUG) {
        LogUtils.d(stringBuffer.toString());
    }
    AsyncHttpGet asyncHttpGet = new AsyncHttpGet(stringBuffer.toString());
    CacheControl cacheControl = request.getCacheControl();
    if (cacheControl != null) {
        asyncHttpGet.setHeader("Cache-Control", cacheControl.getCacheHeaderProperty());
    }
    addHead(request.getHeader(), asyncHttpGet);
    return asyncHttpClient.executeString(asyncHttpGet, new AsyncHttpClient.StringCallback() {
        @Override
        public void onCompleted(Exception e, AsyncHttpResponse asyncHttpResponse, String s) {
            if (responseCallback != null) {
                responseCallback.onCompleted(e, asyncHttpResponse, s);
            }
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:27,代码来源:HttpApi.java

示例13: XHRPollingTransport

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
public XHRPollingTransport(AsyncHttpClient client, String sessionUrl, String sessionId) {
    this.client = client;
    this.sessionUrl = Uri.parse(sessionUrl);
    this.sessionId = sessionId;
    
    doLongPolling();
    connected = true;
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:9,代码来源:XHRPollingTransport.java

示例14: doLongPolling

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
private void doLongPolling() {
    this.client.executeString(new AsyncHttpGet(computedRequestUrl()), new AsyncHttpClient.StringCallback() {
        @Override
        public void onCompleted(Exception e, AsyncHttpResponse source, String result) {
            if (e != null) {
                close(e);
                return;
            }

            sendResult(result);
            doLongPolling();
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:15,代码来源:XHRPollingTransport.java

示例15: SpdyMiddleware

import com.koushikdutta.async.http.AsyncHttpClient; //导入依赖的package包/类
public SpdyMiddleware(AsyncHttpClient client) {
    super(client);
    addEngineConfigurator(new AsyncSSLEngineConfigurator() {
        @Override
        public void configureEngine(SSLEngine engine, GetSocketData data, String host, int port) {
            configure(engine, data, host, port);
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:10,代码来源:SpdyMiddleware.java


注:本文中的com.koushikdutta.async.http.AsyncHttpClient类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。