use of okhttp3.OkHttpClient.Builder in project RxJava-Android-Samples by kaushikgopal.
the class GithubService method createGithubService.
public static GithubApi createGithubService(final String githubToken) {
Retrofit.Builder builder = new Retrofit.Builder().addCallAdapterFactory(RxJava2CallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).baseUrl("https://api.github.com");
if (!TextUtils.isEmpty(githubToken)) {
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(chain -> {
Request request = chain.request();
Request newReq = request.newBuilder().addHeader("Authorization", format("token %s", githubToken)).build();
return chain.proceed(newReq);
}).build();
builder.client(client);
}
return builder.build().create(GithubApi.class);
}
use of okhttp3.OkHttpClient.Builder in project okhttputils by hongyangAndroid.
the class PostFormRequest method buildRequestBody.
@Override
protected RequestBody buildRequestBody() {
if (files == null || files.isEmpty()) {
FormBody.Builder builder = new FormBody.Builder();
addParams(builder);
FormBody formBody = builder.build();
return formBody;
} else {
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
addParams(builder);
for (int i = 0; i < files.size(); i++) {
PostFormBuilder.FileInput fileInput = files.get(i);
RequestBody fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileInput.filename)), fileInput.file);
builder.addFormDataPart(fileInput.key, fileInput.filename, fileBody);
}
return builder.build();
}
}
use of okhttp3.OkHttpClient.Builder in project Fast-Android-Networking by amitshekhariitbhu.
the class RxInternalNetworking method generateSimpleObservable.
public static <T> Observable<T> generateSimpleObservable(RxANRequest request) {
Request okHttpRequest;
Request.Builder builder = new Request.Builder().url(request.getUrl());
InternalNetworking.addHeadersToRequestBuilder(builder, request);
RequestBody requestBody;
switch(request.getMethod()) {
case GET:
{
builder = builder.get();
break;
}
case POST:
{
requestBody = request.getRequestBody();
builder = builder.post(requestBody);
break;
}
case PUT:
{
requestBody = request.getRequestBody();
builder = builder.put(requestBody);
break;
}
case DELETE:
{
requestBody = request.getRequestBody();
builder = builder.delete(requestBody);
break;
}
case HEAD:
{
builder = builder.head();
break;
}
case PATCH:
{
requestBody = request.getRequestBody();
builder = builder.patch(requestBody);
break;
}
}
if (request.getCacheControl() != null) {
builder.cacheControl(request.getCacheControl());
}
okHttpRequest = builder.build();
if (request.getOkHttpClient() != null) {
request.setCall(request.getOkHttpClient().newBuilder().cache(InternalNetworking.sHttpClient.cache()).build().newCall(okHttpRequest));
} else {
request.setCall(InternalNetworking.sHttpClient.newCall(okHttpRequest));
}
return Observable.create(new ANOnSubscribe<T>(request));
}
use of okhttp3.OkHttpClient.Builder in project Fast-Android-Networking by amitshekhariitbhu.
the class InternalNetworking method performUploadRequest.
public static Response performUploadRequest(ANRequest request) throws ANError {
Request okHttpRequest;
Response okHttpResponse;
try {
Request.Builder builder = new Request.Builder().url(request.getUrl());
addHeadersToRequestBuilder(builder, request);
final RequestBody requestBody = request.getMultiPartRequestBody();
final long requestBodyLength = requestBody.contentLength();
builder = builder.post(new RequestProgressBody(requestBody, request.getUploadProgressListener()));
if (request.getCacheControl() != null) {
builder.cacheControl(request.getCacheControl());
}
okHttpRequest = builder.build();
if (request.getOkHttpClient() != null) {
request.setCall(request.getOkHttpClient().newBuilder().cache(sHttpClient.cache()).build().newCall(okHttpRequest));
} else {
request.setCall(sHttpClient.newCall(okHttpRequest));
}
final long startTime = System.currentTimeMillis();
okHttpResponse = request.getCall().execute();
final long timeTaken = System.currentTimeMillis() - startTime;
if (request.getAnalyticsListener() != null) {
if (okHttpResponse.cacheResponse() == null) {
Utils.sendAnalytics(request.getAnalyticsListener(), timeTaken, requestBodyLength, okHttpResponse.body().contentLength(), false);
} else {
if (okHttpResponse.networkResponse() == null) {
Utils.sendAnalytics(request.getAnalyticsListener(), timeTaken, 0, 0, true);
} else {
Utils.sendAnalytics(request.getAnalyticsListener(), timeTaken, requestBodyLength != 0 ? requestBodyLength : -1, 0, true);
}
}
}
} catch (IOException ioe) {
throw new ANError(ioe);
}
return okHttpResponse;
}
use of okhttp3.OkHttpClient.Builder in project Fast-Android-Networking by amitshekhariitbhu.
the class Rx2InternalNetworking method generateDownloadObservable.
public static <T> Observable<T> generateDownloadObservable(final Rx2ANRequest request) {
Request okHttpRequest;
Request.Builder builder = new Request.Builder().url(request.getUrl());
InternalNetworking.addHeadersToRequestBuilder(builder, request);
builder = builder.get();
if (request.getCacheControl() != null) {
builder.cacheControl(request.getCacheControl());
}
okHttpRequest = builder.build();
OkHttpClient okHttpClient;
if (request.getOkHttpClient() != null) {
okHttpClient = request.getOkHttpClient().newBuilder().cache(InternalNetworking.sHttpClient.cache()).addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().body(new ResponseProgressBody(originalResponse.body(), request.getDownloadProgressListener())).build();
}
}).build();
} else {
okHttpClient = InternalNetworking.sHttpClient.newBuilder().addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().body(new ResponseProgressBody(originalResponse.body(), request.getDownloadProgressListener())).build();
}
}).build();
}
request.setCall(okHttpClient.newCall(okHttpRequest));
return new DownloadANObservable<>(request);
}
Aggregations