Отправляю GET
запрос и приходит в log такой ответ onResponse: com.example.nikolai.engadgetnews.Feed.EngadgetArticle@dc9f193
Объясните, что я делаю не так и как распарсить ответ
Feed.java
public class Feed extends Fragment {
private final String TAG = "log_tag";
private RecyclerView recyclerView;
private ArrayList<EngadgetArticle> engadgetArticles;
private static EngadgetAPI engadgetAPI;
private Retrofit retrofit;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.feed_recycler_view, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
engadgetArticles = new ArrayList<>();
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(engadgetArticles);
recyclerView.setAdapter(recyclerViewAdapter);
retrofit = new Retrofit.Builder()
.baseUrl("https://newsapi.org/v1/articles/")
.addConverterFactory(GsonConverterFactory.create())
.build();
engadgetAPI = retrofit.create(EngadgetAPI.class);
Call<EngadgetArticle> call = engadgetAPI.getArticle();
call.enqueue(new Callback<EngadgetArticle>() {
@Override
public void onResponse(Call<EngadgetArticle> call, Response<EngadgetArticle> response) {
Log.d(TAG, "onResponse: " + response.body());
}
@Override
public void onFailure(Call<EngadgetArticle> call, Throwable t) {
Log.d(TAG, "onFailure: " + t.getMessage());
}
});
return view;
}
}
EngadgetAPI.java
interface EngadgetAPI {
@GET("?source=engadget&sortBy=latest&apiKey=eca9819850b64dafbd7513908f89bf16")
Call<EngadgetArticle> getArticle();
}
EngadgetArticle.java
public class EngadgetArticle {
@SerializedName("status")
@Expose
private String status;
@SerializedName("source")
@Expose
private String source;
@SerializedName("sortBy")
@Expose
private String sortBy;
@SerializedName("articles")
@Expose
private List<Article> articles;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getSortBy() {
return sortBy;
}
public void setSortBy(String sortBy) {
this.sortBy = sortBy;
}
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
public class Article {
@SerializedName("author")
@Expose
private String author;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("url")
@Expose
private String url;
@SerializedName("urlToImage")
@Expose
private String urlToImage;
@SerializedName("publishedAt")
@Expose
private String publishedAt;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrlToImage() {
return urlToImage;
}
public void setUrlToImage(String urlToImage) {
this.urlToImage = urlToImage;
}
public String getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
}
}
json файл
"status": "ok",
"source": "engadget",
"sortBy": "latest",
"articles": [
{
"author": "Chris Velazco",
"title": "Report: Snap Inc. spent millions to get better at augmented reality",
"description": "The creators of Snapchat are running toward an IPO at full speed, but it seems to have nabbed itself a neat holiday gift along the way. According to a report fr...",
"url": "https://www.engadget.com/2016/12/25/report-snap-inc-spent-millions-to-get-better-at-augmented-real/",
"urlToImage": "https://s.aolcdn.com/dims5/amp:55075ec747d5a032c8953a00d050b0a9a6b93920/t:1200,630/q:80/?url=https%3A%2F%2Fs.aolcdn.com%2Fhss%2Fstorage%2Fmidas%2Fbee7f5055f269cb94e106bdec56b5047%2F204742224%2Fcimagine_home-page_brands-retailers-1280x626.jpg",
"publishedAt": "2016-12-25T20:30:00Z"
},
{
"author": "Devindra Hardawar",
"title": "'Hidden Figures' is the uplifting NASA story we need right now",
"description": "It's a much-needed reminder that black women were crucial to the early space program.",
"url": "https://www.engadget.com/2016/12/25/hidden-figures-nasa/",
"urlToImage": "https://s.aolcdn.com/dims5/amp:e49440c46f9c049d4c90ee8d049a6408d6f0afc6/t:1200,630/q:80/?url=https%3A%2F%2Fs.aolcdn.com%2Fhss%2Fstorage%2Fmidas%2F658b7bedb47c6dad15e4344e7e846f24%2F204742266%2FHidden%2BFigures%2Bgallery%2B2.jpg",
"publishedAt": "2016-12-25T19:00:00Z"
},
{
"author": "Edgar Alvarez",
"title": "How Rebecca Minkoff uses tech to make her fashion stores stand out",
"description": "The designer is trying to stay ahead with features like smart mirrors and self-checkout.",
"url": "https://www.engadget.com/2016/12/25/rebecca-minkoff-tech-stores/",
"urlToImage": "https://s.aolcdn.com/dims5/amp:5797416217fbd890727fd3b8a7cc18a91da3e54a/t:1200,630/q:80/?url=https%3A%2F%2Fs.aolcdn.com%2Fhss%2Fstorage%2Fmidas%2Fe55b61a39bcd31d1270389d9ba3d278c%2F204741129%2FRebecca-Minkoff-2.jpg",
"publishedAt": "2016-12-25T18:00:00Z"
},
{
"author": "Andrew Tarantola",
"title": "After Math: Merry Christmahanukwanzakkahs",
"description": "It's been a festive week and, for once, not a complete socio-political trainwreck. I know, I'm just as surprised as you are, but some good things really did hap...",
"url": "https://www.engadget.com/2016/12/25/after-math-merry-christmahanukwanzakkahs/",
"urlToImage": "https://s.aolcdn.com/dims5/amp:bd409488a296bd9d29364f408b5206847228026b/t:1200,630/q:80/?url=https%3A%2F%2Fs.aolcdn.com%2Fhss%2Fstorage%2Fmidas%2F3e48c8ac3b14fe5ed9bb08e048fadbaa%2F204739750%2F20643253.jpg",
"publishedAt": "2016-12-25T16:00:00Z"
},
{
"author": "Engadget",
"title": "Pros and cons: Our quick verdict on the LG V20",
"description": "It's not the most well-rounded smartphone out there.",
"url": "https://www.engadget.com/2016/12/25/lg-v20-mini-review/",
"urlToImage": "https://s.aolcdn.com/dims5/amp:2b0cc8a5eed8343a310013a438f284c563dfbe60/t:1200,630/q:80/?url=https%3A%2F%2Fs.aolcdn.com%2Fhss%2Fstorage%2Fmidas%2Fa4510b0492de244fac7b6d921d9f7351%2F204695598%2Flgv20.jpg",
"publishedAt": "2016-12-25T14:30:00Z"
},
{
"author": "Jon Fingas",
"title": "FBI looks into Chinese hack targeting federal insurance",
"description": "It wants to know how state-sponsored attackers got into the FDIC's systems.",
"url": "https://www.engadget.com/2016/12/25/fbi-looks-into-chinese-hack-of-fdic/",
"urlToImage": "https://s.aolcdn.com/dims5/amp:c1add72f67215de3acb91adc5c90d2643a879d3f/t:1200,630/q:80/?url=https%3A%2F%2Fs.aolcdn.com%2Fdims-shared%2Fdims3%2FGLOB%2Fcrop%2F3500x2334%2B0%2B19%2Fresize%2F1600x1067%21%2Fformat%2Fjpg%2Fquality%2F85%2Fhttps%3A%2F%2Fs.aolcdn.com%2Fhss%2Fstorage%2Fmidas%2F47d7673d26df499e8ac32cd5d3690821%2F204741504%2FRTR2MU3E.jpeg",
"publishedAt": "2016-12-25T07:50:00Z"
},
{
"author": "Jon Fingas",
"title": "'Star Citizen' switches to Amazon's game engine",
"description": "It's not a huge transition, but it could provide some certainty for players.",
"url": "https://www.engadget.com/2016/12/24/star-citizen-switches-to-amazon-game-engine/",
"urlToImage": "https://s.aolcdn.com/dims5/amp:00c8e95b2777f574a2aac86168f0844e7b5e8026/t:1200,630/q:80/?url=https%3A%2F%2Fs.aolcdn.com%2Fhss%2Fstorage%2Fmidas%2F8e56ce9bbd793909d0175812f144f90d%2F204741493%2Fstar-citizen-alpha-2-6.jpg",
"publishedAt": "2016-12-25T04:04:00Z"
},
{
"author": "Jon Fingas",
"title": "Louis CK's app gives you direct access to his comedy shows",
"description": "You can watch on your phone without hassles or go-betweens.",
"url": "https://www.engadget.com/2016/12/24/louis-ck-mobile-app/",
"urlToImage": "https://s.aolcdn.com/dims5/amp:152084cb0279a55407587120a7aa3fcb951269c9/t:1200,630/q:80/?url=https%3A%2F%2Fs.aolcdn.com%2Fdims-shared%2Fdims3%2FGLOB%2Fcrop%2F3000x2000%2B0%2B0%2Fresize%2F1600x1067%21%2Fformat%2Fjpg%2Fquality%2F85%2Fhttps%3A%2F%2Fs.aolcdn.com%2Fhss%2Fstorage%2Fmidas%2Fd9cbcb6a7506110bf081a6b4349e45eb%2F204741474%2F620200702.jpg",
"publishedAt": "2016-12-25T02:31:00Z"
},
{
"author": "Jon Fingas",
"title": "Astronaut and climate scientist Piers Sellers dies at 61",
"description": "You may not have heard of him, but the NASA veteran was a legend in environmental circles.",
"url": "https://www.engadget.com/2016/12/24/astronaut-piers-sellers-dies/",
"urlToImage": "https://s.aolcdn.com/dims5/amp:d9d0bd6e1e2bbf95ebabe5e220ecefefb6cf5cf8/t:1200,630/q:80/?url=https%3A%2F%2Fs.aolcdn.com%2Fhss%2Fstorage%2Fmidas%2Fc9355912e28c43eb74a91b280da8d445%2F204741419%2Fpiers-sellers-nasa.jpg",
"publishedAt": "2016-12-25T01:02:00Z"
},
{
"author": "Jon Fingas",
"title": "Firefox will support Windows XP and Vista until September 2017",
"description": "Mozilla is ready to move on from old operating systems.",
"url": "https://www.engadget.com/2016/12/24/firefox-xp-vista-support-ends-in-september-2017/",
"urlToImage": "https://s.aolcdn.com/dims5/amp:1f8ff9e2627fad5798b30f47161a02a13ea57688/t:1200,630/q:80/?url=https%3A%2F%2Fs.aolcdn.com%2Fdims-shared%2Fdims3%2FGLOB%2Fcrop%2F3500x1884%2B0%2B0%2Fresize%2F1600x861%21%2Fformat%2Fjpg%2Fquality%2F85%2Fhttps%3A%2F%2Fs.aolcdn.com%2Fhss%2Fstorage%2Fmidas%2F3ea57c771bfbc2009b1a5ce9b5f73cb%2F204741249%2FRTR3EEAZ.jpeg",
"publishedAt": "2016-12-24T23:31:00Z"
}
]
}
В теле ответа Вы получаете объект EngadgetArticle
.
@Override
public void onResponse(Call<EngadgetArticle> call, Response<EngadgetArticle> response) {
Log.d(TAG, "onResponse: " + response.body());
EngadgetArticle engadgetArticle = response.body();
// далее, делаете, что-то с engadgetArticle ...
}
UPD:
Log.d
Выводит Вам дефолтный toString
объекта EngadgetArticle
.
Метод toString() есть у всех объектов. По-умолчанию он состоит из двух составляющих разделенных собачкой. Эти составляющие: имя_класса_объекта
и хэш_кода
. Если метод hashCode
не переопределен, то он по умолчанию выдает адрес объекта в памяти. Вот вы и получаете, имя_классса@адрес
в памяти вашего объекта = EngadgetArticle@dc9f193
Виртуальный выделенный сервер (VDS) становится отличным выбором
Какими компонентами можно реализовать подобный интерфейс и какие свойства этих компонентов использовать? Прежде всего интересует реализация...
Вечер добрый, у меня стоит задача: надо поставить лайк под записью, пишу вот этот код:
Создаю десктоп приложение на джавеДля хранение данных решил использовать H2 бд