我已成功使用Retrofit调用FlickR,结果返回了几页。
然而,似乎我的response.body()只返回第1页。我的想法是遍历所有页面以获得结果,但我不确定如何在1之后访问后续页面: / p>
mRequestButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mQuery = mSearchTerm.getText().toString();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.flickr.com/services/rest/")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
Call<Flicker> call = apiInterface.getImages(mQuery);
call.enqueue(new Callback<Flicker>() {
@Override
public void onResponse(Call<Flicker> call, Response<Flicker> response) {
Log.v("RESPONSE_CALLED", "ON_RESPONSE_CALLED");
String didItWork = String.valueOf(response.isSuccessful());
Log.v("SUCCESS?", didItWork);
Log.v("RESPONSE_CODE", String.valueOf(response.code()));
Flicker testResponse = response.body();
Log.v("RESPONSE_BODY", "response:" + testResponse);
String total = response.body().getPhotos().getTotal().toString();
Log.v("Total", total);
List<Photo> photoResults = response.body().getPhotos().getPhoto();
int numberOfPages = response.body().getPhotos().getPages();
for (int i = 0; i < numberOfPages; i++) {
for (Photo photo : photoResults) {
if (photo.getUrl_m() != null) {
Log.v("PHOTO_URL:", photo.getUrl_m()
);
}
}
}
}
@Override
public void onFailure(Call<Flicker> call, Throwable t) {
}
});
}
});
mSearchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent toSearch = new Intent(MainActivity.this, FlickRActivity.class);
startActivity(toSearch);
}
});
API接口
public interface ApiInterface {
@GET("?method=flickr.photos.search&api_key=1c448390199c03a6f2d436c40defd90e&format=json&nojsoncallback=1&extras=url_m")
Call<Flicker> getImages(@Query("text") String query);
}
“lebron james”的搜索结果: