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

int*_*ntA 26 rest android json retrofit retrofit2

我正在使用改造来从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;
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的帮助!

hsm*_*m59 37

使用HttpLoggingInterceptor 和Retrofit.

如果这有帮助,请在build.gradle中添加 -

//Retrofit and OkHttp for Networking
compile 'com.squareup.retrofit2:retrofit:2.4.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
//Logging Network Calls
compile 'com.squareup.okhttp3:logging-interceptor:3.10.0'
Run Code Online (Sandbox Code Playgroud)

在你的内部APIClient class添加 -

public class ApiClient {
    private static Retrofit retrofit = null;

    public static Retrofit getClient(){

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Kotlin代码

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

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


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

您将能够记录您所做的Retrofit Network呼叫.

如果您需要更多信息,请与我们联系.

  • 这只是记录。实际调试又如何呢?一个有断点的?由于某种原因,获取响应主体的字段永远停留在“收集数据......”。请参阅此处:https://i.imgur.com/SCEL7K2.png。怎么会这样?我该怎么做才能让它有价值? (2认同)

nhp*_*nhp 14

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

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BASIC);
OkHttpClient client = new OkHttpClient.Builder()
    .addInterceptor(logging)
    .build();
Run Code Online (Sandbox Code Playgroud)

您可以随时通过调用更改日志级别setLevel.

共有4个级别:NONE,BASIC,HEADERS,BODY

要记录到自定义位置,Logger请将实例传递给构造函数.

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

来自Gradle

compile 'com.squareup.okhttp3:logging-interceptor:(insert latest version)'
Run Code Online (Sandbox Code Playgroud)

请遵循此参考

编辑:我也发现这个库有一个非常好的结构和干净的日志.试试吧!!