use of okhttp3.OkHttpClient in project cw-omnibus by commonsguy.
the class OkHttpTests method syncTest.
@Test
public void syncTest() throws IOException {
OkHttpClient client = new OkHttpClient.Builder().build();
Request request = new Request.Builder().url(URL).build();
Response response = client.newCall(request).execute();
Assert.assertEquals(EXPECTED, response.body().string());
}
use of okhttp3.OkHttpClient in project cw-omnibus by commonsguy.
the class QuestionsFragment method onViewCreated.
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
OkHttpClient client = ((MainActivity) getActivity()).getOkHttpClient();
Request request = new Request.Builder().url(SO_URL).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(getClass().getSimpleName(), "Exception loading JSON", e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
Reader in = response.body().charStream();
BufferedReader reader = new BufferedReader(in);
final SOQuestions questions = new Gson().fromJson(reader, SOQuestions.class);
reader.close();
view.post(new Runnable() {
@Override
public void run() {
setListAdapter(new ItemsAdapter(questions.items));
}
});
} else {
Log.e(getClass().getSimpleName(), response.toString());
}
}
});
}
use of okhttp3.OkHttpClient in project Meizhi by drakeet.
the class GankFragment method getOldVideoPreview.
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
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String body = response.body().string();
mVideoPreviewUrl = LoveStrings.getVideoPreviewImageUrl(body);
startPreview(mVideoPreviewUrl);
}
});
}
use of okhttp3.OkHttpClient in project buck by facebook.
the class DistBuildFactory method newFrontendService.
public static FrontendService newFrontendService(CommandRunnerParams params) {
DistBuildConfig config = new DistBuildConfig(params.getBuckConfig());
ClientSideSlb slb = config.getFrontendConfig().createClientSideSlb(params.getClock(), params.getBuckEventBus(), new CommandThreadFactory("StampedeNetworkThreadPool", SLB_THREAD_PRIORITY));
OkHttpClient client = config.createOkHttpClient();
return new FrontendService(ThriftOverHttpServiceConfig.of(new LoadBalancedService(slb, client, params.getBuckEventBus())));
}
use of okhttp3.OkHttpClient in project buck by facebook.
the class DefaultDefectReporter method uploadReport.
private DefectSubmitResult uploadReport(final DefectReport defectReport, DefectSubmitResult.Builder defectSubmitResult, ClientSideSlb slb) throws IOException {
long timeout = rageConfig.getHttpTimeout();
OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(timeout, TimeUnit.MILLISECONDS).readTimeout(timeout, TimeUnit.MILLISECONDS).writeTimeout(timeout, TimeUnit.MILLISECONDS).build();
HttpService httpService = new RetryingHttpService(buckEventBus, new LoadBalancedService(slb, httpClient, buckEventBus), rageConfig.getMaxUploadRetries());
try {
Request.Builder requestBuilder = new Request.Builder();
requestBuilder.addHeader(REQUEST_PROTOCOL_VERSION, rageConfig.getProtocolVersion().name().toLowerCase());
requestBuilder.post(new RequestBody() {
@Override
public MediaType contentType() {
return MediaType.parse("application/x-www-form-urlencoded");
}
@Override
public void writeTo(BufferedSink bufferedSink) throws IOException {
writeReport(defectReport, bufferedSink.outputStream());
}
});
HttpResponse response = httpService.makeRequest(rageConfig.getReportUploadPath(), requestBuilder);
String responseBody;
try (InputStream inputStream = response.getBody()) {
responseBody = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
}
if (response.statusCode() == HTTP_SUCCESS_CODE) {
defectSubmitResult.setIsRequestSuccessful(true);
if (rageConfig.getProtocolVersion().equals(AbstractRageConfig.RageProtocolVersion.SIMPLE)) {
return defectSubmitResult.setReportSubmitMessage(responseBody).setReportSubmitLocation(responseBody).build();
} else {
// Decode Json response.
RageJsonResponse json = objectMapper.readValue(responseBody.getBytes(Charsets.UTF_8), RageJsonResponse.class);
return defectSubmitResult.setIsRequestSuccessful(json.getRequestSuccessful()).setReportSubmitErrorMessage(json.getErrorMessage()).setReportSubmitMessage(json.getMessage()).setReportSubmitLocation(json.getRageUrl()).build();
}
} else {
throw new IOException(String.format("Connection to %s returned code %d and message: %s", response.requestUrl(), response.statusCode(), responseBody));
}
} catch (IOException e) {
throw new IOException(String.format("Failed uploading report because [%s].", e.getMessage()));
} finally {
httpService.close();
}
}
Aggregations