本文整理汇总了Java中com.ning.http.client.AsyncCompletionHandler类的典型用法代码示例。如果您正苦于以下问题:Java AsyncCompletionHandler类的具体用法?Java AsyncCompletionHandler怎么用?Java AsyncCompletionHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AsyncCompletionHandler类属于com.ning.http.client包,在下文中一共展示了AsyncCompletionHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: execute
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
public Promise<Response> execute() {
final scala.concurrent.Promise<Response> scalaPromise = scala.concurrent.Promise$.MODULE$.<Response>apply();
try {
WS.client().executeRequest(request, new AsyncCompletionHandler<com.ning.http.client.Response>() {
@Override
public com.ning.http.client.Response onCompleted(com.ning.http.client.Response response) {
final com.ning.http.client.Response ahcResponse = response;
scalaPromise.success(new Response(ahcResponse));
return response;
}
@Override
public void onThrowable(Throwable t) {
scalaPromise.failure(t);
}
});
} catch (IOException exception) {
scalaPromise.failure(exception);
}
return new Promise<Response>(scalaPromise.future());
}
示例2: sendHelper
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
private void sendHelper(Set<OpenTsdbMetric> metrics) {
if (!metrics.isEmpty()) {
try {
requestBuilder
.setBody(mapper.writeValueAsString(metrics))
.execute(new AsyncCompletionHandler<Void>() {
@Override
public Void onCompleted(Response response) throws Exception {
if (response.getStatusCode() != 204) {
logger.error("send to opentsdb endpoint failed: ("
+ response.getStatusCode() + ") "
+ response.getResponseBody());
}
return null;
}
});
} catch (Throwable ex) {
logger.error("send to opentsdb endpoint failed", ex);
}
}
}
示例3: get
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
@Override
public <T> T get(String url, final Class<T> objectClass, String... params) {
Future<T> f;
try {
f = asyncHttpClient.prepareGet(expandUrl(url, params)).execute(
new AsyncCompletionHandler<T>() {
@Override
public T onCompleted(Response response) throws Exception {
return mapper.readValue(response.getResponseBody(), objectClass);
}
@Override
public void onThrowable(Throwable t) {
throw new TrelloHttpException(t);
}
});
return f.get();
} catch (IOException | InterruptedException | ExecutionException e) {
throw new TrelloHttpException(e);
}
}
示例4: postForObject
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
@Override
public <T> T postForObject(String url, T object, final Class<T> objectClass, String... params) {
Future<T> f;
try {
byte[] body = this.mapper.writeValueAsBytes(object);
f = asyncHttpClient.preparePost(expandUrl(url, params)).setBody(body).execute(
new AsyncCompletionHandler<T>() {
@Override
public T onCompleted(Response response) throws Exception {
return mapper.readValue(response.getResponseBody(), objectClass);
}
@Override
public void onThrowable(Throwable t) {
throw new TrelloHttpException(t);
}
});
return f.get();
} catch (IOException | InterruptedException | ExecutionException e) {
throw new TrelloHttpException(e);
}
}
示例5: putForObject
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
@Override
public <T> T putForObject(String url, T object, final Class<T> objectClass, String... params) {
Future<T> f;
try {
byte[] body = this.mapper.writeValueAsBytes(object);
f = asyncHttpClient.preparePut(expandUrl(url, params)).setBody(body).execute(
new AsyncCompletionHandler<T>() {
@Override
public T onCompleted(Response response) throws Exception {
return mapper.readValue(response.getResponseBody(), objectClass);
}
@Override
public void onThrowable(Throwable t) {
throw new TrelloHttpException(t);
}
});
return f.get();
} catch (IOException | InterruptedException | ExecutionException e) {
throw new TrelloHttpException(e);
}
}
示例6: internalSendCommand
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
private void internalSendCommand(String machineIdentifier, String url) throws IOException {
logger.debug("Calling url {}", url);
client.prepareGet(url)
.setHeader("X-Plex-Target-Client-Identifier", machineIdentifier)
.execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response response) throws Exception {
if (response.getStatusCode() != 200) {
logger.error("Error while sending command to Plex: {}\r\n{}", response.getStatusText(), response.getResponseBody());
}
return response;
}
@Override
public void onThrowable(Throwable t) {
logger.error("Error sending command to Plex", t);
}
});
}
示例7: getCurrencies
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
/**
*
*
* @param lstCurrencies
* @param listener
* @param self
* @throws IOException
* @throws JSONException
*/
private void getCurrencies(List<String> lstCurrencies, final CurrencyConverterListener listener, CurrencyConverterSingelton self) throws IOException, JSONException{
// TODO - download more than one currency
String s = "https://api.bitcoinaverage.com/ticker/global/" + lstCurrencies.get(0) + "/";
EncodingUtils.readFromUrl(s, new AsyncCompletionHandler<Response>(){
@Override
public Response onCompleted(Response arg0) throws Exception {
String res = arg0.getResponseBody();
JSONObject json = new JSONObject(res);
//double last = json.getDouble("last");
CurrencyConverterSingelton.currencies.add(new Currency(lstCurrencies.get(0), json));
if(listener != null)
listener.onFinishedGettingCurrencyData(self);
CurrencyConverterSingelton.isReady = true;
return null;
}
});
}
示例8: getSleepResource
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
public void getSleepResource(long replyAfterMillis,
final Callback<String> callback) throws IOException {
client.prepareGet(url)
.addQueryParameter("replyAfterMillis",
Long.toString(replyAfterMillis))
.execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response response)
throws Exception {
callback.callback(response.getResponseBody());
return response;
}
@Override
public void onThrowable(Throwable t) {
t.printStackTrace();
}
});
}
示例9: observable
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
public Observable<Response> observable(String url, String acceptHeader) {
return Observable.create(observer -> {
try {
asyncHttpClient.prepareGet(url).addHeader("Accept", acceptHeader).execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response response) throws Exception {
observer.onNext(response);
observer.onCompleted();
return response;
}
@Override
public void onThrowable(Throwable t) {
observer.onError(t);
}
});
} catch (Exception e) {
observer.onError(e);
}
});
}
示例10: execute
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
public CompletableFuture<Response> execute(String url, int id) {
final CompletableFuture<Response> result = new CompletableFuture<>();
try {
asyncHttpClient.prepareGet(url).execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response response) throws Exception {
logger.debug("Request #{} completed successfully", id);
result.complete(response);
return response;
}
@Override
public void onThrowable(Throwable t) {
logger.debug("Request #{} failed");
result.completeExceptionally(t);
}
});
} catch (IOException e) {
result.completeExceptionally(e);
}
return result;
}
示例11: sendAsyncHttpRequest
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
/**
* Send async http request.
*
* @param url the url
* @param httpMethodType the http method type
* @param body the body
* @param listener the listener
*/
public void sendAsyncHttpRequest(String url, String httpMethodType, String body, final CallStatsAsyncHttpResponseListener listener) {
if (StringUtils.isNotBlank(url) || StringUtils.isNotBlank(httpMethodType) || StringUtils.isNotBlank(body) || listener == null) {
throw new IllegalArgumentException("sendAsyncHttpRequest: Arguments cannot be null");
}
RequestBuilder builder = new RequestBuilder("POST");
Request request = builder.setUrl(BASE_URL+url)
.addHeader("Accept", "application/json")
.addHeader("Accept-Charset", "utf-8")
.setBody(body)
.build();
httpClient.executeRequest(request, new AsyncCompletionHandler<Response>() {
public Response onCompleted(Response response) throws Exception {
logger.info("Response is " + response.getResponseBody());
listener.onResponse(response);
return null;
}
public void onThrowable(Throwable e) {
listener.onFailure((Exception)e);
}
});
}
示例12: postRequest
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
public Promise<WSResponse> postRequest(URL url, String contentType, byte[] body,
RequestPreparer postRequestPreparer) throws IOException {
final scala.concurrent.Promise<WSResponse> scalaPromise =
scala.concurrent.Promise$.MODULE$.apply();
BoundRequestBuilder builder = asyncHttpClient.preparePost(url.toString())
.setHeader("Content-Type", contentType)
.setContentLength(body.length)
.setBody(body);
if (postRequestPreparer != null) {
postRequestPreparer.prepare(builder, contentType, url);
}
builder.execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response response) {
scalaPromise.success(new NingWSResponse(response));
return response;
}
@Override
public void onThrowable(Throwable t) {
scalaPromise.failure(t);
}
});
return Promise.wrap(scalaPromise.future());
}
示例13: internalSendCommand
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
private void internalSendCommand(String machineIdentifier, String url) throws IOException {
logger.debug("Calling url {}", url);
BoundRequestBuilder builder = client.prepareGet(url).setHeaders(getDefaultHeaders())
.setHeader("X-Plex-Target-Client-Identifier", machineIdentifier);
builder.execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response response) throws Exception {
if (response.getStatusCode() != 200) {
logger.error("Error while sending command to Plex: {}\r\n{}", response.getStatusText(),
response.getResponseBody());
}
return response;
}
@Override
public void onThrowable(Throwable t) {
logger.error("Error sending command to Plex", t);
}
});
}
示例14: search
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
@Override
public void search(ContactQuery query, final AsyncCallback<SearchContacts> handler)
throws IOException, UnauthenticatedSessionException, AuthenticationException {
execute(asyncHttpClient.prepareGet(generateSearchURL(query)),
new AsyncCompletionHandler<Integer>() {
@Override
public Integer onCompleted(Response response) throws Exception {
int statusCode = response.getStatusCode();
String responseBody = null;
if (response.getStatusCode() == 200) {
SearchContacts searchContacts = processResponse(
responseBody = response.getResponseBody(),
SearchContacts.class);
handler.onCompleted(searchContacts, response);
}
throw new AuthenticationException(statusCode + " " + responseBody);
}
@Override
public void onThrowable(Throwable t) {
handler.onThrowable(t);
}
});
}
示例15: executeAsync
import com.ning.http.client.AsyncCompletionHandler; //导入依赖的package包/类
private void executeAsync(String url, final AsyncCallback<List<Contact>> handler)
throws IOException, UnauthenticatedSessionException, AuthenticationException {
execute(asyncHttpClient.prepareGet(url), new AsyncCompletionHandler<Integer>() {
@Override
public Integer onCompleted(Response response) throws Exception {
int statusCode = response.getStatusCode();
String responseBody = null;
if (response.getStatusCode() == 200) {
Contacts contacts = processResponse(
responseBody = response.getResponseBody(), Contacts.class);
handler.onCompleted(contacts != null ? contacts.getContacts() : null,
response);
}
throw new AuthenticationException(statusCode + " " + responseBody);
}
@Override
public void onThrowable(Throwable t) {
handler.onThrowable(t);
}
});
}