List of usage examples for com.squareup.okhttp Callback Callback
Callback
From source file:m.m.com.m.core.service.AbstractService.java
License:Apache License
public void runOnUiThread() { try {//www . j a va 2 s. c o m new OkHttpClient().newCall(mRequest).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { mOnRequestResponse.onFail(-2); } @Override public void onResponse(Response response) throws IOException { mOnRequestResponse.onSucess(response.body().string()); } }); } catch (Exception e) { e.printStackTrace(); } }
From source file:me.drakeet.meizhi.ui.GankFragment.java
License:Open Source License
private void getOldVideoPreview(OkHttpClient client) { String url = "http://gank.io/" + String.format("%s/%s/%s", mYear, mMonth, mDay); Request request = new Request.Builder().url(url).build(); client.newCall(request).enqueue(new Callback() { @Override//w ww . j av a 2 s . com public void onFailure(Request request, IOException e) { e.printStackTrace(); } @Override public void onResponse(Response response) throws IOException { String body = response.body().string(); mVideoPreviewUrl = LoveStrings.getVideoPreviewImageUrl(body); startPreview(mVideoPreviewUrl); } }); }
From source file:me.lock8.Mzigo.java
License:Apache License
public void enqueue(final Request request, final Callback callback) { if (checkForDuplicatedRequest(request)) return;/*from w w w . j a v a 2 s . c o m*/ final Call call = client.newCall(request); journalCall(request, call, callback); call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { notifyFailure(request, e); unJournalCall(request, callback); } @Override public void onResponse(Response response) throws IOException { notifyResponse(response); unJournalCall(request, callback); } }); }
From source file:microsoft.aspnet.signalr.client.http.android.AndroidOkHttpConnection.java
License:Open Source License
@Override public HttpConnectionFuture execute(final Request request, final ResponseCallback responseCallback) { mLogger.log("Create new AsyncTask for HTTP Connection", LogLevel.Verbose); final HttpConnectionFuture future = new HttpConnectionFuture(); com.squareup.okhttp.Request okHttpRequest = createRequest(request); final Call call = client.newCall(okHttpRequest); call.enqueue(new Callback() { @Override/*from w w w . j ava2 s . co m*/ public void onFailure(com.squareup.okhttp.Request request, IOException e) { mLogger.log("Error executing request: " + e.getMessage(), LogLevel.Critical); future.triggerError(e); } @Override public void onResponse(Response response) throws IOException { mLogger.log("Request executed", LogLevel.Verbose); InputStream bodyStream = response.body().byteStream(); Map<String, List<String>> headersMap = response.headers().toMultimap(); try { responseCallback.onResponse(new StreamResponse(bodyStream, response.code(), headersMap)); future.setResult(null); } catch (Exception e) { mLogger.log("Error calling onResponse: " + e.getMessage(), LogLevel.Critical); future.triggerError(e); } } }); future.onCancelled(new Runnable() { @Override public void run() { call.cancel(); } }); return future; }
From source file:net.qiujuer.common.okhttp.core.HttpCore.java
License:Apache License
/** * ============Delivery============// w w w . j ava2 s.co m */ protected void deliveryAsyncResult(Request request, HttpCallback<?> callback) { Util.log("onDelivery:" + request.url().toString()); Util.log("Headers:\n" + request.headers().toString()); final HttpCallback<?> resCallBack = callback == null ? new DefaultCallback() : callback; //Call start callStart(resCallBack, request); mOkHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(final Request request, final IOException e) { Util.log("onFailure:" + request.toString()); callFailure(resCallBack, request, null, e); callFinish(resCallBack); } @Override public void onResponse(final Response response) { try { Object ret = null; final String string = response.body().string(); final boolean haveValue = !TextUtils.isEmpty(string); Util.log("onResponse:Code:%d Body:%s.", response.code(), (haveValue ? string : "null")); if (haveValue) { ret = mResolver.analysis(string, resCallBack.getClass()); } callSuccess(resCallBack, ret, response.code()); } catch (Exception e) { Util.log("onResponse Failure:" + response.request().toString()); callFailure(resCallBack, response.request(), response, e); } callFinish(resCallBack); } }); }
From source file:net.qiujuer.common.okhttp.core.HttpCore.java
License:Apache License
/** * ============Delivery============/*from w w w.j av a 2s . c om*/ */ protected void deliveryAsyncResult(OkHttpClient client, Request request, HttpCallback<?> callback) { Util.log("onDelivery:" + request.url().toString()); Util.log("Headers:\n" + request.headers().toString()); final HttpCallback<?> resCallBack = callback == null ? new DefaultCallback() : callback; //Call start callStart(resCallBack, request); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(final Request request, final IOException e) { Util.log("onFailure:" + request.toString()); callFailure(resCallBack, request, null, e); callFinish(resCallBack); } @Override public void onResponse(final Response response) { try { Object ret = null; final String string = response.body().string(); final boolean haveValue = !TextUtils.isEmpty(string); Util.log("onResponse:Code:%d Body:%s.", response.code(), (haveValue ? string : "null")); if (haveValue) { ret = mResolver.analysis(string, resCallBack.getClass()); } callSuccess(resCallBack, ret, response.code()); } catch (Exception e) { Util.log("onResponse Failure:" + response.request().toString()); callFailure(resCallBack, response.request(), response, e); } callFinish(resCallBack); } }); }
From source file:net.qiujuer.common.okhttp.core.HttpCore.java
License:Apache License
protected void deliveryAsyncResult(final Request request, final StreamCall call, final HttpCallback<?> callback) { Util.log("onDelivery:" + request.url().toString()); final HttpCallback<?> resCallBack = callback == null ? new DefaultCallback() : callback; //Call start/*from ww w .j a v a 2s .c o m*/ callStart(resCallBack, request); mOkHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(final Request request, final IOException e) { Util.log("onFailure:" + request.toString()); callFailure(resCallBack, request, null, e); callFinish(resCallBack); } @Override public void onResponse(final Response response) { OutputStream out = call.getOutputStream(); InputStream in = null; byte[] buf = new byte[mBufferSize]; try { Util.log("onResponse:Code:%d Stream.", response.code()); ResponseBody body = response.body(); bindResponseProgressCallback(request.body(), body, callback); in = body.byteStream(); int size; while ((size = in.read(buf)) != -1) { out.write(buf, 0, size); out.flush(); } // On success call.onSuccess(response.code()); } catch (Exception e) { Util.log("onResponse Failure:" + response.request().toString()); callFailure(resCallBack, response.request(), response, e); } finally { com.squareup.okhttp.internal.Util.closeQuietly(in); com.squareup.okhttp.internal.Util.closeQuietly(out); } callFinish(resCallBack); } }); }
From source file:net.yatomiya.e4.util.StandardHttpClient.java
License:Open Source License
public Call download(String url, Date lastModifiedSince, boolean isSynchronous, Consumer<Response> responseHandler) { Request request = HttpUtils.createRequestBuilder(HttpUrl.parse(url), lastModifiedSince).build(); Callback callback = new Callback() { @Override//ww w . jav a 2 s . c om public void onResponse(Response response) throws IOException { responseHandler.accept(response); } @Override public void onFailure(Request request, IOException e) { responseHandler.accept(null); } }; return execute(request, callback, isSynchronous); }
From source file:net.yatomiya.nicherry.services.bbs.ModelUpdateHandler.java
License:Open Source License
public Call execute(ModelProcessor processor, boolean isSynchronous, boolean isForceUpdate) { this.processor = processor; this.isSynchronous = isSynchronous; this.isForceUpdate = isForceUpdate; bbsService = processor.getManager().getBBSService(); executeTime = JUtils.getCurrentTime(); BBSHttpClient client = bbsService.getHttpClient(); Request request = createRequestBuilder().build(); updateEvent = createUpdateEvent();// w ww . jav a 2 s .c om updateEvent.setModel(getModel()); updateEvent.setRequest(request); EUtils.get(EventService.class).send(BBSService.Event.Update.START, getModel()); Callback callback = new Callback() { @Override public void onResponse(Response response) throws IOException { doOnResponse(response); } @Override public void onFailure(Request request, IOException e) { doOnFailure(request, e); } }; httpCall = client.execute(request, callback, isSynchronous); return httpCall; }
From source file:net.yatomiya.nicherry.services.bbs.PostMessageHandler.java
License:Open Source License
public Call execute(ThreadProcessor processor, boolean isSynchronous, String name, String mail, String body, Map<String, String> additionalParams) { this.processor = processor; this.isSynchronous = isSynchronous; this.name = name; this.mail = mail; this.body = body; executeTime = JUtils.getCurrentTime(); EUtils.get(EventService.class).send(BBSService.Event.Post.START, getModel()); BBSService bbsSrv = processor.getManager().getBBSService(); BBSHttpClient client = bbsSrv.getHttpClient(); ThreadId threadId = getModel().getId(); MBoard board = bbsSrv.getBoard(threadId.getBoardId()); String hostUrl = HttpUtils.getHostUrl(board.getUrl()); String postUrl = hostUrl + "/test/bbs.cgi"; Request.Builder builder = new Request.Builder(); builder.url(postUrl);//from w ww . jav a2 s.c o m Map<String, String> valueMap = new HashMap<>(); valueMap.put("bbs", threadId.getBoardId().getIdValue()); valueMap.put("key", threadId.getIdValue()); valueMap.put("time", String.valueOf((JUtils.getCurrentTime() / 1000) - 60)); valueMap.put("submit", "??"); valueMap.put("FROM", name); valueMap.put("mail", mail); valueMap.put("MESSAGE", body); if (additionalParams != null) valueMap.putAll(additionalParams); String valueStr = HttpUtils.buildFormPostData(valueMap, NConstants.CHARSET_SHIFT_JIS); builder.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded; charset=Shift_JIS"), StringUtils.getBytes(valueStr, NConstants.CHARSET_SHIFT_JIS))); builder.header("Referer", board.getUrl()); Request request = builder.build(); Callback callback = new Callback() { @Override public void onResponse(Response response) throws IOException { EUtils.asyncExec(() -> doOnResponse(response)); } @Override public void onFailure(Request request, IOException e) { EUtils.asyncExec(() -> doOnFailure(request, e)); } }; httpCall = client.execute(request, callback, isSynchronous); postEvent = new PostMessageEvent(name, mail, body); return httpCall; }