2017-08-12 26 views
10

Mesela Flickr API bazı verileri almak için güçlendirme kullanıyorum. yöntem böyle görünüyor aramayı yapıyorum:Güçlendirme API çağrımda nasıl hata ayıklayabilirim?

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

asla onResponse() yönteme giriyor Ancak. onResponse() asla baskılar içindeki ilk günlük deyimi ne onFailure() günlük deyimi yapar. Tarayıcıda call.request().url().toString() tarafından döndürülen URL'yi girmeyi denediğimde, iyi çalışıyor ve beklenen JSON'u elde ediyorum. Neden enqueue() yöntemim hiç ateş etmiyor?

Yardımlarınız için teşekkürler! Retrofit ile birlikte

+1

izleyin. Bu şekilde gönderdiğiniz şeyleri ve orijinal biçimlerinde neler aldığınızı görebilirsiniz. (:() + Call.request() url() toString.. "Fotoğraf url", "TEMP_TAG"); 've eğer çalışırsa (call.request() öğesine bakın – TooManyEduardos

+0

Ayrıca günlüğü' Log.d kaldırmak Olmadan bile çağrı tüketen olabilir) – TooManyEduardos

cevap

13

kullanın HttpLoggingInterceptor.

bu yardımcı olur, lütfen build.gradle içinde bu ekleme -

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

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

Kotlin Kodu Ve yapacaksın -

//Retrofit and OkHttp for Networking 
compile 'com.squareup.retrofit2:retrofit:2.3.0' 
compile 'com.squareup.retrofit2:converter-gson:2.3.0' 
//Logging Network Calls 
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0' 

İçinde senin APIClient class Bu eklenti Güçlendirme Ağı çağrısını kaydedebilme Yaptığın şey.

daha fazla bilgiye ihtiyaç olursa haber verin. HTTP istek ve yanıt verilerini kaydeder

8

Bir OkHttp önleme.

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

Sen setLevel arayarak istediği zaman günlük düzeyini değiştirebilir. YOK, TEMEL, BAŞLIKLAR, VÜCUT

özel konuma oturum açmak için, yapıcı bir Logger örneğini pass:

4 seviyesi vardır.

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

Gradle

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

Bu gerçek bir cevap isnt, ancak yeni ağ çağrıları test ederken benim kişisel öneri hep Charles veya Postman gibi bir şey kullanmak bu reference

+0

Hmm, bu benim için bir şey yapıyor gibi görünmüyor. Yukarıdaki kodu kullandı ve aramaları yapan yöntemde 'HttpLoggingInterceptor' komutunu koydu, ancak hiçbir şey çıktı almaz. – intA

+0

Bu Uyarlama # istemcisini (istemci) kullanarak müşteriye güçlendirme için istemci eklediniz mi? –

+0

Evet, '.client (client) 'ı, benim Retrofit nesnesini – intA

İlgili konular