2016-08-20 145 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中字符串的長度啓用/禁用搜索按鈕