【问题标题】:Search FlickR Photos by Tag按标签搜索 FlickR 照片
【发布时间】:2016-08-20 01:18:22
【问题描述】:

我正在尝试查询 FlickR 照片并接收 JSON 响应。我正在使用 Retrofit 来调用 FlickR API。在我的代码中,用户输入文本,该文本是通过 EditText 捕获的。我想根据这个词查询。我从 Flick 收到以下错误:“无参数搜索已被禁用。请改用 flickr.photos.getRecent。”

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

}

【问题讨论】:

  • 你搜索过 Flickr API 文档吗?

标签: java android rest retrofit flickr


【解决方案1】:

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

Call&lt;List&lt;Photo&gt;&gt; getPhotos(@Query("text") String photoSearchTerm);

其他事项:
• 可能希望在您的帖子中隐藏您的 API 密钥。
• 可能希望将onClick() 方法中的代码与if(!TextUtils.isEmpty(mQuery)) 一起包装,以防止问题再次发生。 (也可以在 EditText 中添加一个 TextChangeWatcher 并根据 EditText 中字符串的长度启用/禁用搜索按钮

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多