Search code examples
javaandroidrestretrofitflickr

Search FlickR Photos by Tag


I am trying to query FlickR photos and receive a JSON response. I am using Retrofit to make the call to the FlickR API. In my code, the user enters text, which is captured via an EditText. I want to query based on this term. I am receiving the following error from 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);
   }

}

Solution

  • Based off their API docs you're looking to pass text as the @Query param instead of q. So like:

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

    Other things:
    • Might want to hide your API Key from your post.
    • Might want to wrap the code in your onClick() method with if(!TextUtils.isEmpty(mQuery)) to prevent the issue from happening again. (Could also add a TextChangeWatcher to your EditText and enable/disable the search button based on the length of the string in the EditText