tccpg288
tccpg288

Reputation: 3352

How to Handle JSON Responses with Retrofit

I am making a call to the FlickR API based on a search term that a user enters in an editText. The result is going to be several JSON objects (or at least that is what I am aiming for), which I ultimately want to display in a GridView.

Right now, I know that I am making a successful call to the FlickR API since I am firing the onResponse method of Retrofit. However, I am unsure how to view the raw response data to display in a gridview:

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

}

Upvotes: 1

Views: 254

Answers (1)

Long Ranger
Long Ranger

Reputation: 6008

You can use the Retrofit + Picasso to retrieve the photo and fetch it into the adapter

http://themakeinfo.com/2015/04/android-retrofit-images-tutorial/

Upvotes: 1

Related Questions