如何调试改装API调用? [英] How can I debug my retrofit API call?

查看:34
本文介绍了如何调试改装API调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用改造从Flickr api中获取一些数据.我拨打电话的方法如下:

I'm using retrofit to get some data from the Flickr api. The method I'm making the call in looks like this:

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()方法从不触发?

However it is never getting into the onResponse() method. The first log statement within onResponse() never prints, neither does the log statement in onFailure(). When I try entering the URL that is returned by call.request().url().toString() in the browser it works fine, and I get the expected JSON. Why is my enqueue() method never firing?

感谢您的帮助!

推荐答案

使用 HttpLoggingInterceptor 以及翻新.

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

If this helps, add this inside your build.gradle -

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

在您的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;
    }
}

科林代码

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)
    }

您将能够记录您进行的改造网络调用.

And you will be able to log the Retrofit Network calls that you make.

让我知道是否需要更多信息.

Let me know if you need more information.

这篇关于如何调试改装API调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆