use of okhttp3.OkHttpClient in project AnDevCon-RxPatterns by colintheshots.
the class Example11 method createGithubClient.
private void createGithubClient() {
if (mGitHubClient == null) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(loggingInterceptor).addInterceptor(chain -> chain.proceed(chain.request().newBuilder().addHeader("Authorization", "token " + Secrets.GITHUB_PERSONAL_ACCESS_TOKEN).build())).build();
mGitHubClient = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())).addConverterFactory(GsonConverterFactory.create()).client(client).baseUrl(GITHUB_BASE_URL).build().create(GitHubClient.class);
}
}
use of okhttp3.OkHttpClient in project AnDevCon-RxPatterns by colintheshots.
the class Example15 method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example15);
editText = (EditText) findViewById(R.id.editText1);
if (Secrets.API_KEY.length() < 10) {
Toast.makeText(this, "API KEY is unset!", Toast.LENGTH_LONG).show();
return;
}
if (mGooglePlacesClient == null) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(loggingInterceptor).build();
mGooglePlacesClient = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())).addConverterFactory(GsonConverterFactory.create(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create())).baseUrl(GOOGLE_API_BASE_URL).client(client).build().create(GooglePlacesClient.class);
}
RxTextView.textChanges(editText).debounce(DELAY, TimeUnit.MILLISECONDS).map(new Func1<CharSequence, String>() {
@Override
public String call(CharSequence charSequence) {
return charSequence.toString();
}
}).flatMap(new Func1<String, Observable<PlacesResult>>() {
@Override
public Observable<PlacesResult> call(String s) {
Observable<PlacesResult> placesResult = null;
try {
placesResult = mGooglePlacesClient.autocomplete(Secrets.API_KEY, URLEncoder.encode(s, "utf8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return placesResult;
}
}).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<PlacesResult>() {
@Override
public void call(PlacesResult placesResult) {
List<String> strings = new ArrayList<String>();
for (Prediction p : placesResult.predictions) {
strings.add(p.description);
}
ListView listView = (ListView) findViewById(R.id.listView1);
if (listView != null) {
listView.setAdapter(new ArrayAdapter<String>(Example15.this, android.R.layout.simple_list_item_1, strings));
}
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
});
}
use of okhttp3.OkHttpClient in project AnDevCon-RxPatterns by colintheshots.
the class Example1_NoRX method createGithubClient.
private void createGithubClient() {
if (mGitHubClient == null) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(loggingInterceptor).addInterceptor(chain -> chain.proceed(chain.request().newBuilder().addHeader("Authorization", "token " + Secrets.GITHUB_PERSONAL_ACCESS_TOKEN).build())).build();
mGitHubClient = new Retrofit.Builder().client(client).baseUrl(GITHUB_BASE_URL).build().create(GitHubClient.class);
}
}
use of okhttp3.OkHttpClient in project Varis-Android by dkhmelenko.
the class TravisRestClient method getHttpClient.
private OkHttpClient getHttpClient() {
final String userAgent = String.format("TravisClient/%1$s", PackageUtils.getAppVersion());
return mOkHttpClient.newBuilder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder request = original.newBuilder().header("User-Agent", userAgent).header("Accept", "application/vnd.travis-ci.2+json");
String accessToken = AppSettings.getAccessToken();
if (!TextUtils.isEmpty(accessToken)) {
String headerValue = String.format("token %1$s", accessToken);
request.addHeader("Authorization", headerValue);
}
return chain.proceed(request.build());
}
}).build();
}
use of okhttp3.OkHttpClient in project Just-Another-Android-App by athkalia.
the class NetworkModule method provideOkHttpClient.
@Provides
@Singleton
public static OkHttpClient provideOkHttpClient(PropertiesManager propertiesManager, HttpLoggingInterceptor httpLoggingInterceptor, List<Interceptor> networkInterceptors, BaseUrlInterceptor baseUrlInterceptor) {
final OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
// Logs network calls for debug builds
okHttpBuilder.addInterceptor(httpLoggingInterceptor);
// Adds authentication headers when required in network calls
okHttpBuilder.addInterceptor(new AuthenticationInterceptor(propertiesManager));
// Helps with changing base url of network calls in espresso tests to the MockWebServer base url.
okHttpBuilder.addInterceptor(baseUrlInterceptor);
// For release builds nothing is added, the list is empty. For debug builds Stetho interceptor is added.
for (Interceptor networkInterceptor : networkInterceptors) {
okHttpBuilder.addNetworkInterceptor(networkInterceptor);
}
return okHttpBuilder.build();
}
Aggregations