2016-08-20 114 views
1

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

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

} 
+0

您是否搜索过Flickr API文档? – RudolphEst

回答

1

基于了他们的API docs你正在寻找通过text@Query PARAM代替q。因此,像:

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

其他的事情:
•可能要隐藏自己信息的API密钥。
•可能想用if(!TextUtils.isEmpty(mQuery))onClick()方法中的代码包装起来,以防止问题再次发生。 (也可以将TextChangeWatcher添加到EditText中,并根据EditText中字符串的长度启用/禁用搜索按钮