Search in sources :

Example 86 with OkHttpClient

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);
    }
}
Also used : Context(android.content.Context) Iterables(com.google.common.collect.Iterables) Bundle(android.os.Bundle) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) Action1(rx.functions.Action1) Observable(rx.Observable) Toast(android.widget.Toast) Map(java.util.Map) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) Schedulers(rx.schedulers.Schedulers) View(android.view.View) AdapterView(android.widget.AdapterView) ActionBar(android.app.ActionBar) Subscriber(rx.Subscriber) LayoutInflater(android.view.LayoutInflater) Expose(com.google.gson.annotations.Expose) ViewGroup(android.view.ViewGroup) Retrofit(retrofit2.Retrofit) List(java.util.List) TextView(android.widget.TextView) OkHttpClient(okhttp3.OkHttpClient) BaseAdapter(android.widget.BaseAdapter) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) GET(retrofit2.http.GET) RxJavaCallAdapterFactory(retrofit2.adapter.rxjava.RxJavaCallAdapterFactory) Path(retrofit2.http.Path) ListView(android.widget.ListView) Activity(android.app.Activity) Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 87 with OkHttpClient

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();
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Observable(rx.Observable) Retrofit(retrofit2.Retrofit) ListView(android.widget.ListView) ArrayList(java.util.ArrayList) List(java.util.List) Func1(rx.functions.Func1) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) ArrayAdapter(android.widget.ArrayAdapter)

Example 88 with OkHttpClient

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);
    }
}
Also used : Context(android.content.Context) Iterables(com.google.common.collect.Iterables) Bundle(android.os.Bundle) LayoutInflater(android.view.LayoutInflater) Expose(com.google.gson.annotations.Expose) Response(retrofit2.Response) ViewGroup(android.view.ViewGroup) Retrofit(retrofit2.Retrofit) List(java.util.List) TextView(android.widget.TextView) OkHttpClient(okhttp3.OkHttpClient) BaseAdapter(android.widget.BaseAdapter) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Callback(retrofit2.Callback) GET(retrofit2.http.GET) Toast(android.widget.Toast) Map(java.util.Map) Path(retrofit2.http.Path) View(android.view.View) ListView(android.widget.ListView) Activity(android.app.Activity) Call(retrofit2.Call) Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 89 with OkHttpClient

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();
}
Also used : Request(okhttp3.Request) Interceptor(okhttp3.Interceptor)

Example 90 with OkHttpClient

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();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) AuthenticationInterceptor(com.example.networking.AuthenticationInterceptor) Interceptor(okhttp3.Interceptor) AuthenticationInterceptor(com.example.networking.AuthenticationInterceptor) BaseUrlInterceptor(com.example.networking.BaseUrlInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

OkHttpClient (okhttp3.OkHttpClient)149 Request (okhttp3.Request)73 Response (okhttp3.Response)61 IOException (java.io.IOException)52 Test (org.junit.Test)35 Call (okhttp3.Call)24 Retrofit (retrofit2.Retrofit)24 Interceptor (okhttp3.Interceptor)19 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)18 MockResponse (okhttp3.mockwebserver.MockResponse)18 File (java.io.File)15 GsonBuilder (com.google.gson.GsonBuilder)12 Provides (dagger.Provides)12 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)12 Gson (com.google.gson.Gson)10 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Cache (okhttp3.Cache)10 Observable (rx.Observable)10 Singleton (javax.inject.Singleton)9