library implementar java android retrofit okhttp retrofit2

java - implementar - retrofit post



OperaciĆ³n no compatible: Android, Retrofit, OkHttp. Agregar interceptor en OkHttpClient (2)

Este problema ocurre cuando cambia Retrofit 2.0-beta2 a Retrofit 2.0-beta3 . OkHttpClient usar el generador si desea crear el objeto OkHttpClient .

Cambio :

OkHttpClient okClient = new OkHttpClient(); okClient.interceptors().add(new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); // Request customization: add request headers Request.Builder requestBuilder = original.newBuilder() .header("Authorization", token) .method(original.method(), original.body()); Request request = requestBuilder.build(); return chain.proceed(request); } });

a :

OkHttpClient okClient = new OkHttpClient.Builder() .addInterceptor( new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); // Request customization: add request headers Request.Builder requestBuilder = original.newBuilder() .header("Authorization", token) .method(original.method(), original.body()); Request request = requestBuilder.build(); return chain.proceed(request); } }) .build();

Debería resolver tu problema.

Estoy tratando de agregar autenticación basada en token a través de Retrofit 2.0-beta3 y OkHttpClient en Android usando interceptores. Pero obtengo UnsupportedOperationException cuando agrego mi interceptor en OkHttpClient. Aquí está mi código: en ApiClient.java

public static TrequantApiInterface getClient(final String token) { if( sTreqantApiInterface == null) { Log.v(RETROFIT_LOG, "Creating api client for the first time"); OkHttpClient okClient = new OkHttpClient(); okClient.interceptors().add(new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); // Request customization: add request headers Request.Builder requestBuilder = original.newBuilder() .header("Authorization", token) .method(original.method(), original.body()); Request request = requestBuilder.build(); return chain.proceed(request); } }); Retrofit client = new Retrofit.Builder() .baseUrl(baseUrl) .client(okClient) .addConverterFactory(GsonConverterFactory.create()) .build(); sTreqantApiInterface = client.create(TrequantApiInterface.class); } return sTreqantApiInterface; }

Y lo uso como:

private void ampFreqTest(){ String token = getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE) .getString(getString(R.string.key_token), ""); service = ApiClient.getClient(token); //I get an exception on this line: Call<List<AmpFreq>> call = service.getUserAmpFreq("1"); call.enqueue(new Callback<List<AmpFreq>>() { @Override public void onResponse(Response<List<AmpFreq>> response) { Toast.makeText(HomeScreen.this, "Got result", Toast.LENGTH_LONG); Log.v(ApiClient.RETROFIT_LOG, "Success api client." + response.message()); Log.v(ApiClient.RETROFIT_LOG, "Success api client."); } @Override public void onFailure(Throwable t) { Toast.makeText(HomeScreen.this, t.getMessage() , Toast.LENGTH_LONG); Log.v(ApiClient.RETROFIT_LOG, "Fail api client." + t.getMessage() ); } }); }

Pero me sale este error:

Process: com.trequant.usman.trequant_android, PID: 14400 java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Collections.java:932) at com.trequant.usman.trequant_android.api.ApiClient.getClient(ApiClient.java:41)

Me da un error al agregar un nuevo interceptor que dice que no es ModificableCollection pero la documentación para la función interceptores () dice: / **

* Returns a modifiable list of interceptors that observe the full span of each call: from before * the connection is established (if any) until after the response source is selected (either the * origin server, cache, or both). */

¿Qué estoy haciendo mal? ¿Podría ser un error?


Pruebe esto si la otra respuesta no funciona:

OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(new MyInterceptor()) .build(); retrofit = new Retrofit.Builder() .baseUrl("http://google.com") .addConverterFactory(GsonConverterFactory.create()) .client(okHttpClient) .build();