use of okhttp3.Dispatcher in project amhttp by Eddieyuan123.
the class OnAddListener method parseNetworkResponse.
@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
ResponseBody responseBody = response.body();
if (responseBody == null) {
throw new NullPointerException("response body is null");
} else {
String responseStr = responseBody.string();
Type type = ClassUtils.getType(OnAddListener.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 amhttp by Eddieyuan123.
the class OnUploadListener method parseNetworkResponse.
@Override
public void parseNetworkResponse(Response response, FileCard fileCard) throws Throwable {
ResponseBody responseBody = response.body();
if (responseBody == null) {
throw new NullPointerException("response body is null");
} else {
String responseStr = responseBody.string();
Type type = ClassUtils.getType(OnUploadListener.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 Tusky by Vavassor.
the class BaseActivity method createMastodonAPI.
protected void createMastodonAPI() {
mastodonApiDispatcher = new Dispatcher();
Gson gson = new GsonBuilder().registerTypeAdapter(Spanned.class, new SpannedTypeAdapter()).registerTypeAdapter(StringWithEmoji.class, new StringWithEmojiTypeAdapter()).create();
OkHttpClient okHttpClient = OkHttpUtils.getCompatibleClientBuilder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
Request.Builder builder = originalRequest.newBuilder();
String accessToken = getAccessToken();
if (accessToken != null) {
builder.header("Authorization", String.format("Bearer %s", accessToken));
}
Request newRequest = builder.build();
return chain.proceed(newRequest);
}
}).dispatcher(mastodonApiDispatcher).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(getBaseUrl()).client(okHttpClient).addConverterFactory(GsonConverterFactory.create(gson)).build();
mastodonAPI = retrofit.create(MastodonAPI.class);
}
use of okhttp3.Dispatcher in project retrofit by square.
the class Crawler method main.
public static void main(String... args) throws Exception {
Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20));
dispatcher.setMaxRequests(20);
dispatcher.setMaxRequestsPerHost(1);
OkHttpClient okHttpClient = new OkHttpClient.Builder().dispatcher(dispatcher).connectionPool(new ConnectionPool(100, 30, TimeUnit.SECONDS)).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(HttpUrl.parse("https://example.com/")).addConverterFactory(PageAdapter.FACTORY).client(okHttpClient).build();
PageService pageService = retrofit.create(PageService.class);
Crawler crawler = new Crawler(pageService);
crawler.crawlPage(HttpUrl.parse(args[0]));
}
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;
}
Aggregations