Search in sources :

Example 11 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor 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 12 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor 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 13 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor 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)

Example 14 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project ListenerMusicPlayer by hefuyicoder.

the class NetworkModule method provideLastFMRetrofit.

@Provides
@Named("lastfm")
@PerApplication
Retrofit provideLastFMRetrofit() {
    String endpointUrl = Constants.BASE_API_URL_LASTFM;
    Gson gson = new GsonBuilder().create();
    GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(gson);
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().cache(new Cache(FileUtil.getHttpCacheDir(ListenerApp.getContext()), Constants.HTTP_CACHE_SIZE)).connectTimeout(Constants.HTTP_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS).readTimeout(Constants.HTTP_READ_TIMEOUT, TimeUnit.MILLISECONDS).build();
    //        OkHttpClient newClient = client.newBuilder().addInterceptor(loggingInterceptor).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(endpointUrl).client(client).addConverterFactory(gsonConverterFactory).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
    return retrofit;
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache) Named(javax.inject.Named) PerApplication(io.hefuyi.listener.injector.scope.PerApplication) Provides(dagger.Provides)

Example 15 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project ListenerMusicPlayer by hefuyicoder.

the class NetworkModule method provideKuGouRetrofit.

@Provides
@Named("kugou")
@PerApplication
Retrofit provideKuGouRetrofit() {
    String endpointUrl = Constants.BASE_API_URL_KUGOU;
    Gson gson = new GsonBuilder().create();
    GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create(gson);
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().cache(new Cache(FileUtil.getHttpCacheDir(ListenerApp.getContext()), Constants.HTTP_CACHE_SIZE)).connectTimeout(Constants.HTTP_CONNECT_TIMEOUT, TimeUnit.MILLISECONDS).readTimeout(Constants.HTTP_READ_TIMEOUT, TimeUnit.MILLISECONDS).build();
    //        OkHttpClient newClient = client.newBuilder().addInterceptor(loggingInterceptor).build();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(endpointUrl).client(client).addConverterFactory(gsonConverterFactory).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
    return retrofit;
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache) Named(javax.inject.Named) PerApplication(io.hefuyi.listener.injector.scope.PerApplication) Provides(dagger.Provides)

Aggregations

HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)22 OkHttpClient (okhttp3.OkHttpClient)17 Provides (dagger.Provides)11 Retrofit (retrofit2.Retrofit)10 Singleton (javax.inject.Singleton)9 GsonBuilder (com.google.gson.GsonBuilder)7 File (java.io.File)6 Cache (okhttp3.Cache)6 GsonConverterFactory (retrofit2.converter.gson.GsonConverterFactory)6 Observable (rx.Observable)6 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)5 List (java.util.List)5 Interceptor (okhttp3.Interceptor)5 Activity (android.app.Activity)4 Bundle (android.os.Bundle)4 ListView (android.widget.ListView)4 Gson (com.google.gson.Gson)4 Expose (com.google.gson.annotations.Expose)4 RxJavaCallAdapterFactory (retrofit2.adapter.rxjava.RxJavaCallAdapterFactory)4 Context (android.content.Context)3