use of okhttp3.Dispatcher in project okhttp by square.
the class Benchmark method startServer.
private MockWebServer startServer() throws IOException {
Logger.getLogger(MockWebServer.class.getName()).setLevel(Level.WARNING);
MockWebServer server = new MockWebServer();
if (tls) {
SslClient sslClient = SslClient.localhost();
server.useHttps(sslClient.socketFactory, false);
server.setProtocols(protocols);
}
final MockResponse response = newResponse();
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
return response;
}
});
server.start();
return server;
}
use of okhttp3.Dispatcher in project okhttp by square.
the class CallTest method cancelInFlightBeforeResponseReadThrowsIOE.
@Test
public void cancelInFlightBeforeResponseReadThrowsIOE() throws Exception {
Request request = new Request.Builder().url(server.url("/a")).build();
final Call call = client.newCall(request);
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
call.cancel();
return new MockResponse().setBody("A");
}
});
try {
call.execute();
fail();
} catch (IOException expected) {
}
}
use of okhttp3.Dispatcher in project amhttp by Eddieyuan123.
the class OnDownloadListener method parseNetworkResponse.
@Override
public void parseNetworkResponse(final Response response, FileCard fileCard) throws Throwable {
ResponseBody body = response.body();
if (body == null) {
throw new NullPointerException("response body is null");
} else {
final Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
InputStream is = body.byteStream();
long contentLength = body.contentLength();
final File file = FileUtils.saveFile(is, contentLength, fileCard, new OnSaveListener() {
@Override
public void OnProgress(final long progress, final long total) {
dispatcher.dispatchToUIThread(new Runnable() {
@Override
public void run() {
onProgressChanged(progress, total);
}
});
}
});
dispatcher.dispatchToUIThread(new Runnable() {
@Override
public void run() {
onResponseSuccess((T) file);
}
});
}
}
use of okhttp3.Dispatcher in project amhttp by Eddieyuan123.
the class OnFindListener method parseNetworkResponse.
@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
ResponseBody responseBody = response.body();
String responseStr = null;
if (responseBody != null) {
responseStr = responseBody.string();
}
Type type = ClassUtils.getType(OnFindListener.this.getClass());
T bean = null;
if (type != null) {
if (TextUtils.equals(type.toString(), "class java.lang.String"))
bean = (T) responseStr;
else
bean = new Gson().fromJson(responseStr, type);
}
final T finalBean = bean;
Dispatcher dispatcher = Dispatcher.getDispatcher(Looper.getMainLooper());
dispatcher.dispatchToUIThread(new Runnable() {
@Override
public void run() {
onResponseSuccess(finalBean);
}
});
}
use of okhttp3.Dispatcher in project instructure-android by instructure.
the class CanvasRestAdapter method buildRollCallAdapter.
public Retrofit buildRollCallAdapter(@NonNull String url) {
final Gson gson = new GsonBuilder().setLenient().create();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(DEBUG ? HttpLoggingInterceptor.Level.HEADERS : HttpLoggingInterceptor.Level.NONE);
return new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create(gson)).client(new OkHttpClient.Builder().addInterceptor(loggingInterceptor).addInterceptor(new RollCallInterceptor()).readTimeout(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS).dispatcher(mDispatcher).build()).build();
}
Aggregations