Имеется фейковый апи по адресу: https://jsonplaceholder.typicode.com/photos Как мне выхватить данные только одного блока, например:
{
"albumId": 1,
"id": 3,
"title": "officia porro iure quia iusto qui ipsa ut modi",
"url": "https://via.placeholder.com/600/24f355",
"thumbnailUrl": "https://via.placeholder.com/150/24f355"
},
Мой код:
public interface Api {
@GET("photos")
Call<List<Model>> galleryContent();
}
public class ApiClient {
public static final String BASE_URL =
"https://jsonplaceholder.typicode.com/";
private static Retrofit retrofit;
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
public class Model {
private String albumId;
private String id;
private String title;
private String url;
private String thumbnailUrl;
public String getAlbumId() {
return albumId;
}
public String getId() {
return id;
}
public String getTitle() {
return title;
}
public String getUrl() {
return url;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
}
public class QRActivity extends AppCompatActivity {
TextView textView;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr);
loadData();
textView = findViewById(R.id.text_view_result);
}
public void loadData() {
Api api = ApiClient.getClient().create(Api.class);
Call<List<Model>> gallery = api.galleryContent();
gallery.enqueue(new Callback<List<Model>>() {
@Override
public void onResponse(Call<List<Model>> call, Response<List<Model>>
response) {
if (response.body() != null && response.isSuccessful()) {
Log.i("test", "onResponse: " + response.body().size());
} else {
try {
Log.e(TAG, "onResponse: " +
response.errorBody().string());
} catch (IOException e) {
e.printStackTrace();
}
}
List<Model> models = response.body();
for (Model model : models) {
textView.append(model.getAlbumId() + "\n");
textView.append(model.getId()+"\n");
textView.append(model.getTitle()+"\n");
textView.append(model.getUrl()+"\n");
textView.append(model.getThumbnailUrl()+"\n");
}
}
@Override
public void onFailure(Call<List<Model>> call, Throwable t) {
Log.i("tes2", "onFailure: " + t);
}
});
}
}