Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Okhttp logging não funciona

Adicionei o httploggin e funcionou 1 ou 2 vezes, depois sumiu e não aparece mais, já olhei todos os passos, criei o emulador de novo, limpei cache, etc. Mas não aparece. Não consigo descobrir porque minha requisição falha no retrofit e funciona com o WebClient (conferi a url). Mas enfim, quero mesmo saber como ativar de novo o log do okhttp.

Classe Retrofit:

public class RetrofitConfig {

    private final Retrofit retrofit;

    public RetrofitConfig() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.level(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder client = new OkHttpClient.Builder();
        client.addInterceptor(interceptor);

        retrofit = new Retrofit.Builder()
                .baseUrl("http://x.x.x.x:8080/rest/denuncias/")
                .addConverterFactory(JacksonConverterFactory.create())
                .client(client.build())
                .build();
    }

    public DenunciaService getSinosiService() {
        return retrofit.create(DenunciaService.class);
    }
}

Interface Service

public interface DenunciaService {

    @GET("listar")
    Call<List<Denuncia>> buscarDenuncias();

    @POST("denuncia")
    Call<Void> enviarDenuncia(@Body Denuncia denuncia);

}

Método que pega o objeto repassar ao retrofit para executar o POST

    private void enviarDenuncia() {

        Denuncia denuncia = recuperarDenuncia();
        denuncia.setDataDenuncia(null);
        Call call = new RetrofitConfig().getSinosiService().enviarDenuncia(denuncia);

        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                Log.i("onResponse", "sucesso");
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                Log.e("onFailure", "requisição falhou");

            }
        });
    }

Saída do terminal

Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@d57b925
W/m.lf.sinosinov: Accessing hidden method Landroid/widget/PopupWindow;->setEpicenterBounds(Landroid/graphics/Rect;)V (light greylist, linking)
W/m.lf.sinosinov: Accessing hidden method Landroid/widget/PopupWindow;->setClipToScreenEnabled(Z)V (light greylist, reflection)
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@ce0aede
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
I/System.out: AGUA
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@65ac429
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
D/EGL_emulation: eglMakeCurrent: 0xe694ab40: ver 2 0 (tinfo 0xeead1920)
W/Java7Support: Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added
W/m.lf.sinosinov: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setUseSessionTickets(Z)V (light greylist, reflection)
    Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setHostname(Ljava/lang/String;)V (light greylist, reflection)
W/m.lf.sinosinov: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->getAlpnSelectedProtocol()[B (light greylist, reflection)
    Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setAlpnProtocols([B)V (light greylist, reflection)
W/m.lf.sinosinov: Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (light greylist, reflection)
    Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (light greylist, reflection)
    Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (light greylist, reflection)
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/Choreographer: Skipped 32 frames!  The application may be doing too much work on its main thread.
E/onFailure: requisição falhou
2 respostas

E o gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.lf.sinosinovo"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.gms:play-services-maps:16.1.0'
    testImplementation 'junit:junit:4.12'
    implementation 'com.squareup.retrofit2:retrofit:2.6.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.squareup.retrofit2:converter-jackson:2.6.2'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1'
    def room_version = "2.2.1"

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
}
solução!

Consegui resolver. O problema é na biblioteca jackson, acho que ela não suporta ou talvez a versão que esteja usando não é compatível com a versão do retrofit e consequentemente leva o okhttp a não mostrar os logs também (apesar de ambas estarem na 2.6.2).

Mudei para a bliblioteca Gson (que eu estava usando o WebCliente, por isso resolvi testar com ela) e o método post deu certo e também voltou a aparecer os logs do Okhttp.

Portanto a solução foi trocar a fábrica no configurador do retrofit para Gson (a versão também acompanha a versão do retrofit: 2.6.2, conforme o gradle acima):

        retrofit = new Retrofit.Builder()
                .baseUrl("http://xxx.xxx.x.xx:8080/rest/denuncias/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(client.build())
                .build();

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software