gpt4 book ai didi

java - 按标签搜索 FlickR 照片

转载 作者:行者123 更新时间:2023-11-30 01:04:05 27 4
gpt4 key购买 nike

我正在尝试查询 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);
}

}

最佳答案

基于他们的 API docs你想通过 text作为@Query参数而不是 q .就像这样:

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

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

关于java - 按标签搜索 FlickR 照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39049591/

27 4 0