我正在使用 Retrofit 的
enqueue()
方法拨打电话。我在我的 MainActivity 的 refreshImages()
中调用我的 onCreate()
,refreshImages()
然后调用一个方法 refreshImagesIds()
应该调用 Flickr 的 API 并返回一个 PhotosList
对象,然后我将拉从那里取出Photos
,其中将包含一个Photo
对象列表。我的问题是,由于某种原因,我的 onResponse()
方法中的 enqueue()
永远不会被调用。当我使用调试器时,它会直接跳过它,当我将 Log 语句放入其中时,它们永远不会被写出。我知道它命中的端点是正确的,因为我可以使用 OkHttp 的记录器看到它,并且我的 POJO 对于返回的数据看起来都是正确的。
知道为什么这不起作用吗?下面是我的
refreshImages
和refreshImagesId
。这些都包含在我的 MainAcitivty 中并修改类级变量。
private void refreshImages() {
// make api call
//imageUrls = FlickrServiceManager_withinterface.getKittenImages(8);
refreshImageIds();
List<Photo> photos = photosList.getPhotos().getPhoto();
imageIds = new ArrayList<String>();
for(Photo photo : photos) {
Log.d("TAG", "It is pringint imageIds: " + photo.getId());
imageIds.add(photo.getId());
}
}
private void refreshImageIds() {
Retrofit retrofit = Api.getRestAdapter();
FlickrServiceInterface flickrService = retrofit.create(FlickrServiceInterface.class);
Call<PhotosList> call = flickrService.getPhotos(API_KEY, FORMAT, "1");
imageIds = new ArrayList<String>();
call.enqueue(new Callback<PhotosList>(){
@Override
public void onResponse(Call<PhotosList> call, Response<PhotosList> response) {
photosList = response.body();
}
@Override
public void onFailure(Call<PhotosList> call, Throwable t) {
// TODO: Clean up
Log.d("TEMP_TAG", "Call failed");
}
});
}
还有我的 FlickrServiceInterface:
public interface FlickrServiceInterface {
@GET("?method=flickr.photos.getSizes")
Call<PhotoSizes> getPhotoSizes(@Query("api_key") String apiKey, @Query("format") String format, @Query("nojsoncallback") String jsonCallback, @Query("photo_id") String photoId);
@GET("?method=flickr.photos.getRecent")
Call<PhotosList> getPhotos(@Query("api_key") String apiKey, @Query("format") String format, @Query("nojsoncallback") String jsonCallback);
}
更改对同步改造 API 的调用:
public static List<String> getImageIds(int size) {
Call<PhotosList> call = flickrService.getPhotos(apiKey, format, "1");
photoIds = new ArrayList<String>();
PhotosList photosList = call.execute().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());
}
Log.d("TEMP_TAG", "it's getting here too");
return photoIds;
}
请注意,您需要在
AsyncTask
上调用此方法
编辑
你也可以继续使用 enqueue,但你需要提供一个“onFinish”钩子,这样你就知道什么时候收到你的数据,然后你用数据“通知”客户端:
//interface por communication
public interface ImageIdsCallBack {
public void onFinish( List<String> photoIds );
}
然后就收到这个接口,发送数据:
public static List<String> getImageIds(int size, final ImageIdsCallBack callback) {
Call<PhotosList> call = flickrService.getPhotos(apiKey, format, "1");
photoIds = new ArrayList<String>();
call.enqueue(new Callback<PhotosList>(){
@Override
public void onResponse(Call<PhotosList> call, Response<PhotosList> response) {
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());
}
//send the data to the caller
callback.onFinish(photoIds);
}
@Override
public void onFailure(Call<PhotosList> call, Throwable t) {
// TODO: Clean up
Log.d("TEMP_TAG", "Call failed");
}
});
Log.d("TEMP_TAG", "it's getting here too");
return photoIds;
}
调用方法:
getImageIds( 50 , new ImageIdsCallBack() {
public void onFinish( List<String> photoIds ) {
//update UI with photoIds
}
} );
我通常使用像EventBus这样的库来简化它,我真的向你推荐它。
如果我错了请纠正我,这是在主线程上吗?这将带来不等待响应的问题。
考虑使用
async
我不确切知道它是否有帮助,但我认为我的 call.enqueue() 被跳过但我没有处理我收到的响应我收到了 400 响应。所以我建议记录你得到的响应代码。希望它能帮助某人。
call.enqueue(new Callback < LoginResponse > () {
@ Override
public void onResponse(Call < LoginResponse > call, Response < LoginResponse > response) {
Log.i(TAG, "Response code " + response.code());
if (response.code() == 200) {
Toast.makeText(MainActivity.this, "Going into Login page", Toast.LENGTH_LONG).show();
}
/*if(response.code()==400){
Toast.makeText(MainActivity.this, "Error 400", Toast.LENGTH_LONG).show();
}* added it later after i saw in debugging that i am gettting
400 error because i was using email instead of username */
}
@ Override
public void onFailure(Call < LoginResponse > call, Throwable t) {
Log.e(TAG, "onFailure: " + t.getMessage());
}
});
}
});