如何调试我的改造 API 调用?

问题描述 投票:0回答:3

我正在使用 Retrofit 从 Flickr api 获取一些数据。我拨打电话的方法如下所示:

public static List<String> getImageIds(int size) {
    Call<PhotosList> call = flickrService.getPhotos(apiKey, format, "1");
    Log.d("TEMP_TAG", "photo url: " + call.request().url().toString());
    photoIds = new ArrayList<String>();

    call.enqueue(new Callback<PhotosList>(){
        @Override
        public void onResponse(Call<PhotosList> call, Response<PhotosList> response) {
            Log.d("TEMP_TAG", "it's getting here");
            PhotosList photosList = response.body();
            List<Photo> photos = photosList.getPhotos().getPhoto();

            for(Photo photo : photos) {
                Log.d("TEMP_TAG", "adding photo id to list: " + photo.getId());
                photoIds.add(photo.getId());
            }
        }

        @Override
        public void onFailure(Call<PhotosList> call, Throwable t) {
            // TODO: Clean up
            Log.d("TEMP_TAG", "photoId: ");
        }
    });
    Log.d("TEMP_TAG", "it's getting here too");
    return photoIds;
}

但是它永远不会进入

onResponse()
方法。
onResponse()
中的第一个日志语句永远不会打印,
onFailure()
中的日志语句也不会打印。 当我尝试在浏览器中输入
call.request().url().toString()
返回的 URL 时,它工作正常,并且我得到了预期的 JSON。为什么我的
enqueue()
方法从未触发?

感谢您的帮助!

android json rest retrofit retrofit2
3个回答
72
投票

HttpLoggingInterceptor 与 Retrofit 一起使用。

如果这有帮助,请将其添加到您的 build.gradle 中 -

//Retrofit and OkHttp for Networking
implementation 'com.squareup.retrofit2:retrofit:2.11.0'
implementation 'com.squareup.retrofit2:converter-gson:2.11.0'
//Logging Network Calls
implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0'

在您的

APIClient class
中添加此 -

public class ApiClient {
    private static Retrofit retrofit = null;

    public static Retrofit getClient(){

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .build();


        if(retrofit==null){
            retrofit = new Retrofit.Builder()
                    .baseUrl(BuildConfig.baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .build();
        }
        return retrofit;
    }
}

Kotlin 代码

val interceptor : HttpLoggingInterceptor = HttpLoggingInterceptor().apply {
            level = Level.BODY
        }

val client : OkHttpClient = OkHttpClient.Builder().apply {
            addInterceptor(interceptor)
        }.build()


fun getService(): Service {
        return Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(LiveDataCallAdapterFactory())
                .client(client)
                .build()
                .create(Service::class.java)
    }

您将能够记录您进行的 Retrofit Network 调用。

如果您需要更多信息,请告诉我。


18
投票

一个 OkHttp 拦截器,用于记录 HTTP 请求和响应数据。

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BASIC);
OkHttpClient client = new OkHttpClient.Builder()
    .addInterceptor(logging)
    .build();

您可以随时通过调用

setLevel
更改日志级别。

有 4 个级别:NONE、BASIC、HEADERS、BODY

要登录到自定义位置,请将

Logger
实例传递给构造函数。

HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new 
Logger() {
@Override public void log(String message) {
    Log.d(TAG, "message: ");
    }
});

来自 Gradle

compile 'com.squareup.okhttp3:logging-interceptor:(insert latest version)'

遵循此参考

编辑:我还发现了这个库,它具有非常好的结构和干净的日志。尝试一下!!


2
投票

您可以使用以下类来记录API调用

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor

object HTTPLogger {
    fun getLogger(): OkHttpClient {
        /*
         * OKHTTP interceptor to log all API calls
         */
        val interceptor = HttpLoggingInterceptor()
        interceptor.level = HttpLoggingInterceptor.Level.BODY
        val client = OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .build()
        return client
    }
}

然后您可以在改造实例类中调用此类,如下所示

import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitClientInstance {
    private var retrofit: Retrofit? = null
    val retrofitInstance: Retrofit?
        get() {
            if (retrofit == null) {
                retrofit = Retrofit.Builder()
                        .baseUrl(Constants.BASE_URL)
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .addConverterFactory(GsonConverterFactory.create())
                        .client(HTTPLogger.getLogger())
                        .build()
            }
            return retrofit
        }
}

所需的依赖是

implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
© www.soinside.com 2019 - 2024. All rights reserved.