2016-08-20 1 views
0

J'ai réussi à déterminer comment appeler l'API FlickR. Cependant, j'ai de la difficulté à déterminer comment je recherche toutes les photos FlickR en fonction d'un terme de recherche saisi par l'utilisateur.API FlickR - Requête basée sur EditText

Je crois que le terme de recherche doit être entré dans le composant « balises » du FlickR API, mais je ne sais pas comment lier mon Modifier variable texte (mquery) à la balise API 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&tags=<Ali>&api_key=1c448390199c03a6f2d436c40defd90e&format=json") // 
    Call<List<Photo>> getPhotos(@Query("q") String photoSearchTerm); 
    } 

} 

Répondre

1

Essayez ceci, ceci est une API de recherche pour Flickr

public static final String BASE_URL = "https://api.flickr.com/"; 

    @GET("services/rest/?method=flickr.photos.search&api_key="+API_KEY+"&format=json&nojsoncallback=1&extras=url_m") 
    Call<FlickrModel> getImages(@Query("text") String query); 
+0

Merci, mais comment EditText est-il incorporé? – tccpg288

+1

vous faites l'initialisation Retrofit mal ici –

+1

https://github.com/Veeresh8/FlickrAPI vérifier ce repo, qui a tout ce dont vous avez besoin –