按标签搜索 FlickR 照片

Search FlickR Photos by Tag

我正在尝试查询 FlickR 照片并收到 JSON 响应。我正在使用 Retrofit 调用 FlickR API。在我的代码中,用户输入文本,这是通过 EditText 捕获的。我想根据这个词查询。我从 Flick 收到以下错误:"Parameterless searches have been disabled. Please use flickr.photos.getRecent instead."

public class MainActivity extends AppCompatActivity {

private EditText mSearchTerm;
private Button mRequestButton;
private String mQuery;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSearchTerm = (EditText) findViewById(R.id.ediText_search_term);
    mRequestButton = (Button) findViewById(R.id.request_button);
    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<List<Photo>> call = apiInterface.getPhotos(mQuery);
            call.enqueue(new Callback<List<Photo>>() {
                @Override
                public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {

                }

                @Override
                public void onFailure(Call<List<Photo>> call, Throwable t) {

                }
            });

        }
    });



}

//Synchronous vs. Asynchronous
public interface ApiInterface {
    @GET("?&method=flickr.photos.search&api_key=1c448390199c03a6f2d436c40defd90e&format=json")  //
    Call<List<Photo>> getPhotos(@Query("q") String photoSearchTerm);
   }

}

根据他们的 API docs,您希望将 text 作为 @Query 参数而不是 q 传递。所以喜欢:

Call<List<Photo>> getPhotos(@Query("text") String photoSearchTerm);

其他:
• 可能想对 post 隐藏 API 密钥。
• 可能需要用if(!TextUtils.isEmpty(mQuery)) 将代码包装在onClick() 方法中,以防止问题再次发生。 (也可以将 TextChangeWatcher 添加到您的 EditText 和 enable/disable 基于 EditText

中字符串长度的搜索按钮