This question already has answers here:
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
(2 answers)
Closed 5 years ago.
I went through all the questions on stack overflow and various articles,but none of them seem to solve my problem.Please help
{"status":"ok","source":"abc-news-au","sortBy":"top","articles":[{"author":"http://www.abc.net.au/news/5511636","title":"Hope for Waters as Commonwealth to argue Ludlam, Roberts wrongly elected","description":"The Commonwealth will argue only Malcolm Roberts and Scott Ludlam should be found to have been wrongly elected to Parliament, potentially also opening the door for a Larissa Waters return.","url":"http://www.abc.net.au/news/2017-09-26/only-roberts-and-ludlam-wrongly-elected-commonwealth/8990398","urlToImage":"http://www.abc.net.au/news/image/8720606-1x1-700x700.jpg","publishedAt":"2017-09-26T09:10:12Z"},{"author":"http://www.abc.net.au/news/rural/anna-vidot/4592718","title":"New records tipped to fall as Australia braces for second heatwave","description":"Temperature records set on Saturday could fall within days as a heatwave looms, the bureau says.","url":"http://www.abc.net.au/news/2017-09-26/bom-records-will-fall-as-eastern-australia-braces-for-heatwave/8988650","urlToImage":"http://www.abc.net.au/news/image/5197898-1x1-700x700.jpg","publishedAt":"2017-09-26T10:11:00Z"},{"author":"http://www.abc.net.au/news/alle-mcmahon/6883402","title":"The Earth will get a little cooler after Mount Agung erupts. Here's why","description":"When Mount Agung finally erupts in Bali — and experts say it could be any moment — global atmospheric temperatures will drop. But don't get too exited, it will far from reverse global warming.","url":"http://www.abc.net.au/news/2017-09-26/how-volcanic-eruptions-can-affect-world-temperatures-mount-agung/8987770","urlToImage":"http://www.abc.net.au/news/image/8988046-1x1-700x700.jpg","publishedAt":"2017-09-26T09:54:47Z"},{"author":"http://www.abc.net.au/news/jessica-haynes/8462720","title":"What are your legal rights if you make an HR complaint at work?","description":"What are your legal rights if you make a human resources complaint at work? And what should happen if someone makes a complaint against you?","url":"http://www.abc.net.au/news/2017-09-26/what-are-your-legal-rights-if-you-make-a-hr-complaint-at-work/8987296","urlToImage":"http://www.abc.net.au/news/image/8968496-1x1-700x700.jpg","publishedAt":"2017-09-26T03:41:24Z"},{"author":null,"title":"Why Twitter won't ban Trump despite his threatening tweets","description":"US President Donald Trump is effectively given carte blanche by his favourite social media platform to tweet what he likes, after questions are raised about his threatening tone.","url":"http://www.abc.net.au/news/2017-09-26/why-twitter-wont-delete-trumps-threatening-north-korea-tweet/8988388","urlToImage":"http://www.abc.net.au/news/image/8940614-1x1-700x700.jpg","publishedAt":"2017-09-26T09:51:22Z"},{"author":"http://www.abc.net.au/news/ellie--sibson/7553504","title":"Waiting 'to kill someone' my whole life: Court hears alleged killer's confession","description":"A police recording taken from Korean student Eunji Ban's accused killer is played in the Supreme Court in Brisbane, where he confesses to the bashing and being troubled by a demon his whole life.","url":"http://www.abc.net.au/news/2017-09-26/korea-students-injuries-so-horrific-gender-indistinguishable/8989462","urlToImage":"http://www.abc.net.au/news/image/5116560-1x1-700x700.jpg","publishedAt":"2017-09-26T08:29:24Z"},{"author":null,"title":"Former Swans star Barry Hall clobbers opponent in local footy final","description":"Barry Hall is at it again. This time, the former Sydney Swans star clobbers an opponents twice during a local footy grand final in Queensland.","url":"http://www.abc.net.au/news/2017-09-26/barry-hall-caught-striking-two-opponents-in-qafl-grand-final/8990650","urlToImage":"http://www.abc.net.au/news/image/8990710-1x1-700x700.jpg","publishedAt":"2017-09-26T08:37:48Z"},{"author":"http://www.abc.net.au/news/david-chau/8543210","title":"Fears Afterpay's 'instant approvals' will cause serious financial hardship","description":"With Afterpay's increasing popularity, consumer advocates are concerned it will lead to greater financial hardship for customers.","url":"http://www.abc.net.au/news/2017-09-26/afterpay-consumer-debt/8988394","urlToImage":"http://www.abc.net.au/news/image/8988408-1x1-700x700.jpg","publishedAt":"2017-09-26T10:00:55Z"},{"author":null,"title":"Apartment lending rules tightened in Perth, Brisbane as oversupply bites","description":"The ANZ tells its brokers to tighten lending restrictions in 11 postcodes in Brisbane and Perth, where buyers will now need a 20 per cent deposit before they can borrow.","url":"http://www.abc.net.au/news/2017-09-26/anz-tightens-apartment-lending-rules-in-brisbane-and-perth/8987698","urlToImage":"http://www.abc.net.au/news/image/8871160-1x1-700x700.jpg","publishedAt":"2017-09-26T05:31:47Z"},{"author":"http://www.abc.net.au/news/mazoe-ford/6525834, http://www.abc.net.au/news/antonette-collins/6555778","title":"'I love chatting to pedos': Ben McCormack pleads guilty to child porn charges","description":"Ben McCormack could face a maximum of 15 years in jail after pleading guilty over child pornography charges.","url":"http://www.abc.net.au/news/2017-09-26/ben-mccormack-a-current-affair-former-journalist-pleads-guilty/8987272","urlToImage":"http://www.abc.net.au/news/image/8989938-1x1-700x700.jpg","publishedAt":"2017-09-26T07:48:34Z"}]}
This my json
User.java
public class User {
#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 = null;
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;
}
}
Article.java
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;
}
}
public class GithubAdapter extends RecyclerView.Adapter<GithubAdapter.GithubViewHolder>
{
private Context context;
private Article[] data;
public GithubAdapter(Context context, Article[] data) {
this.context = context;
this.data = data;
}
#Override
public GithubViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
View view=layoutInflater.inflate(R.layout.list_item,parent,false);
return new GithubViewHolder(view);
}
#Override
public void onBindViewHolder(GithubViewHolder holder, int position) {
Article user=data[position];
holder.textView.setText(user.getTitle());
Glide.with(holder.imageView.getContext()).load(user.getUrl()).into(holder.imageView);
}
#Override
public int getItemCount() {
return data.length;
}
Adapter
public class GithubViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView textView;
public GithubViewHolder(View itemView) {
super(itemView);
imageView=(ImageView)itemView.findViewById(R.id.imageview);
textView=(TextView)itemView.findViewById(R.id.textview);
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
public static final String GITURL="https://newsapi.org/v1/articles?source=abc-news-au&sortBy=top&apiKey=7379cf7......";
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
StringRequest stringRequest=new StringRequest(GITURL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
GsonBuilder gsonBuilder=new GsonBuilder();
Gson gson= gsonBuilder.create();
Article[] users= gson.fromJson(response,Article[].class);
recyclerView.setAdapter(new GithubAdapter(MainActivity.this,users));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Something went wrong",Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
I keep getting Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $. I even tried converting Article to list in Adapter,but it doesn't work.Any help will be appreciated.
The following line is wrong:
Article[] users= gson.fromJson(response,Article[].class);
It should be
User user = gson.fromJson(response, User.class);
Then you can get the articles with user.getArticles();
According to your json it is an Object. However, you are parsing as if it is an array.
Article[] users= gson.fromJson(response,Article[].class); // here you are treating response as array not an object
correct way of parsing will be
User user = gson.fromJson(response,User.class);
Try this
User users= gson.fromJson(response,User.class);
recyclerView.setAdapter(new GithubAdapter(MainActivity.this,users.getArticles());
In your response have object and array both , Create pojo class for object and arrayList.
create a pojo class for your responce(Object first)
public class Test
{
private String status, source, sortBy;
ArrayList<Articles> articles;
public String getStatus() {
return status;
}
public String getSource() {
return source;
}
public String getSortBy() {
return sortBy;
}
public ArrayList<Articles> getArticles() {
return articles;
}
}
create pojo class for ArrayList data
public class Articles {
private String author,title,description,url,
urlToImage,publishedAt;
public String getAuthor() {
return author;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getUrl() {
return url;
}
public String getUrlToImage() {
return urlToImage;
}
public String getPublishedAt() {
return publishedAt;
}
}
In your main Activity After Response do like this
StringRequest stringRequest=new StringRequest(GITURL, new
Response.Listener<String>() {
#Override
public void onResponse(String response) {
Gson gson = new Gson();
Test testModel = gson.fromJson(response,Test.class);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"Something went
wrong",Toast.LENGTH_LONG).show();
}
});
Hope this will work.
Related
I'm working on an Android project and I'm using retrofit to fetch JSON data. My JSON look like this.
{
"status":"ok",
"count":1,
"count_total":1,
"pages":1,
"posts":[
{
"id":4,
"type":"post",
"slug":"my news",
"status":"publish",
"title":"test news title goes here",
"title_plain":"test news title goes here",
"content":"<p>news title goes<\/span><\/p>\n",
"date":"2018-05-12 20:00:51",
"modified":"2018-05-12 20:00:51"
}
]
}
My model class -Article.java
public class Article {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("status")
#Expose
private String status;
#SerializedName("title")
#Expose
private String title;
#SerializedName("title_plain")
#Expose
private String titlePlain;
#SerializedName("content")
#Expose
private String content;
#SerializedName("date")
#Expose
private String date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitlePlain() {
return titlePlain;
}
public void setTitlePlain(String titlePlain) {
this.titlePlain = titlePlain;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
I'm calling to retrofit method like this and already added a dialog box for loading. But I'm not getting response from the API. (It works perfectly when I'm typing on the browser)
NewsService service = RetrofitClient.getRetrofitInstance().create(NewsService.class);
Call<List<Article>> call = service.getNewestArticles();
call.enqueue(new Callback<List<Article>>() {
#Override
public void onResponse(Call<List<Article>> call, Response<List<Article>> response) {
dialog.dismiss();
generateDataList(response.body());
}
#Override
public void onFailure(Call<List<Article>> call, Throwable t) {
}
});
private void generateDataList(List<Article> articles) {
lstNews.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
lstNews.setLayoutManager(layoutManager);
adapter = new ListNewsAdapter(articles,getActivity().getBaseContext());
adapter.notifyDataSetChanged();
lstNews.setAdapter(adapter);
lstNews.setAdapter(adapter);
}
I'm new to retrofit. Could someone please help me to fix this issue?
You're not actually loading a list (array) of articles but an object conatining a list of articles, so you need 2 model classes :
public class ArticleResponse {
#SerializedName("status")
#Expose
private String status;
#SerializedName("count")
#Expose
private int count;
#SerializedName("count_total")
#Expose
private int countTotal;
#SerializedName("pages")
#Expose
private int pages;
#SerializedName("posts")
#Expose
private List<Article> articles;
//getter/Setter
//...
}
Your retrofit service should return that ArticleResponse instead, then you can get your article array through articles getter from your model
NewsService service = RetrofitClient.getRetrofitInstance().create(NewsService.class);
Call<ArticleResponse> call = service.getNewestArticles();
call.enqueue(new Callback<ArticleResponse>() {
#Override
public void onResponse(Call<ArticleResponse> call, Response<ArticleResponse> response) {
dialog.dismiss();
generateDataList(response.body().getArticles());
}
#Override
public void onFailure(Call<ArticleResponse> call, Throwable t) {
Log.w( t );
}
});
I'm developing an Android app which connects to a Springboot server.
The app (using okhttp3 and retrofit) calls the server which returns an array list in an object (Photo Response) below.
The list is populating but the object MediaContentGroup does not populate in any photo object even though the fields of id and title do.
I've debugged through my server to ensure that I am using the correct object names when using #SerializedName.
I'm pretty new to Android development and using springboot, so if anyone could help me to understand why the MediaContentGroup object is always null, I would really appreciate it.
Is there any possible way I've set it to only accept Strings??
Thanks
public class PhotoResponse {
#SerializedName("photoFeed")
#Expose
private PhotoFeed photoFeed;
public PhotoFeed getPhotoFeed() {
return photoFeed;
}
}
public class PhotoFeed {
#SerializedName("photoList")
#Expose
private List<Photo> photoList = new ArrayList<>();
public List<Photo> getPhotoList() {
return photoList;
}
}
public class Photo {
public final static int HD_720_WIDTH = 1280;
public final static int HD_720_HEIGHT = 720;
#SerializedName("id")
#Expose
private String id;
#SerializedName("title")
#Expose
private String title;
#SerializedName("mediaContentGroup")
#Expose
private MediaContentGroup mediaContentGroup;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void setMediaContentGroup(MediaContentGroup mediaContentGroup){
this.mediaContentGroup = mediaContentGroup;
}
public MediaContentGroup getMediaContentGroup(){
return this.mediaContentGroup;
}
public String getImageUrl() {
return mediaContentGroup.getImages().get(0).getUrl();
}
public String getThumbUrl() {
return mediaContentGroup.getThumbnails().get(0).getUrl();
}
}
public class MediaContentGroup {
#SerializedName("images")
#Expose
public List<MediaContent> images = new ArrayList<>();
public void setImages(List<MediaContent> images){ this.images = images;}
public List<MediaContent> getImages() {
return images;
}
#SerializedName("thumbnails")
#Expose
public List<Thumbnail> thumbnails = new ArrayList<>();
public void setThumbnails(List<Thumbnail> thumbnails){ this.thumbnails = thumbnails;}
public List<Thumbnail> getThumbnails() {
return thumbnails;
}
}
public class Thumbnail {
#SerializedName("url")
#Expose
private String url;
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
}
I am trying to retrieve Reddit information from a particular subreddit using Retrofit 2. I have followed many tutorials and videos and my code seems to be correct from my perspective but I only manage to have null objects in my model class. I have the permission for internet in the Manifest.
This is a link the JSON I am working with HERE
MainActivity
public class MainActivity extends AppCompatActivity
{
TextView mTextView;
Data mData;
private static final String TAG = "Battlestations";
#Override
protected void onCreate(Bundle savedInstanceState)
{
mTextView = (TextView) findViewById(R.id.test_view);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Call<Data> serviceCall = Service.getDesktopService().desks();
serviceCall.enqueue(new Callback<Data>()
{
#Override
public void onResponse(Call<Data> call, Response<Data> response)
{
Log.d("Reponce","return");
Log.i(TAG, "Response is " + mData.getChildren());
}
#Override
public void onFailure(Call<Data> call, Throwable t)
{
}
});
}
}
Api/Service Class
public class Service
{
private static final String BASE_URL = "https://www.reddit.com/r/";
private static DeskInterface mRetrofit;
public static DeskInterface getDesktopService()
{
if(mRetrofit == null)
{
Retrofit build = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
mRetrofit = build.create(DeskInterface.class);
}
return mRetrofit;
}
public interface DeskInterface
{
#GET("battlestations/hot/.json")
Call<Data> desks();
}
}
Data
public class Data
{
private List<Child> children = null;
public List<Child> getChildren()
{
return children;
}
public void setChildren(List<Child> children)
{
this.children = children;
}
}
Child
public class Child
{
private Data_ data;
public Data_ getData()
{
return data;
}
public void setData(Data_ data)
{
this.data = data;
}
}
Data_
public class Data_
{
private String subreddit;
private Integer score;
private String author;
private String subredditNamePrefixed;
private String url;
private String title;
public String getSubreddit()
{
return subreddit;
}
public void setSubreddit(String subreddit)
{
this.subreddit = subreddit;
}
public Integer getScore()
{
return score;
}
public void setScore(Integer score)
{
this.score = score;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getSubredditNamePrefixed()
{
return subredditNamePrefixed;
}
public void setSubredditNamePrefixed(String subredditNamePrefixed)
{
this.subredditNamePrefixed = subredditNamePrefixed;
}
public String getUrl()
{
return url;
}
public void setUrl(String url)
{
this.url = url;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
}
You need to add mData = response.body() in onResponse() (also check response.isSuccessful() first)
The problem is that your Data does not correspond with Reddit JSON. Your Data class
public class Data {
private List<Child> children = null;
}
does not match with the given json, which is:
{
"kind":"listing",
"data":{
"modhash":"...",
"children":[...],
"after":"...",
"before":"..."
}
}
Retrofit automagically convert from json to java but only if the mapping is correct.
A correct Java class would be:
public class Subreddit {
String kind;
Data data;
}
public class Data{
String modhash;
List<Child> children;
String after;
String before;
}
and then modify desks method interface to
Call<Subreddit> desks();
You would have to go recursively for the entire depth of the JSON to get the right mapping.
But before you get to work, just replace your Retrofit interface:
public interface DeskInterface{
#GET("battlestations/hot/.json")
Call<Data> desks();
}
with:
public interface DeskInterface{
#GET("battlestations/hot/.json")
Call<JsonObject> desks();
}
and it should return something. If is still null, then further investigation is needed. If it returns a valid response(some json text) then copy/paste that subreddit to this website where it converts all the json to a valid Java class
I am trying to use the google books API with Retrofit, it is returning an empty result.
this is the url:
https://www.googleapis.com/books/v1/volumes?q=9781451648546
In Retrofit I have an interface:
public interface IServiceEndPoint {
#GET("https://www.googleapis.com/books/v1/volumes?q=9781451648546")
Call<BookList> getBooks();
}
in my webservice class I have the following method:
public void getBooks(final Callback<BookList> callback){
IServiceEndPoint endPoint = mRetrofit.create(IServiceEndPoint.class);
Call<BookList> call = endPoint.getBooks();
call.enqueue(callback);
}
in the activity class I have the method:
private void getBooks(){
WebserviceHelper.getmInstance().getBooks(new Callback<BookList>() {
#Override
public void onResponse(Call<BookList> call, Response<BookList> response) {
mBooks = response.body().getResults();
mBookAdapter.update(mBooks);
}
#Override
public void onFailure(Call<BookList> call, Throwable t) {
}
});
}
I have a Java class Book and BookList.
public class Book implements Serializable {
#SerializedName("id")
private String id;
#SerializedName("title")
private String title;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
public class BookList extends Book implements Serializable {
#SerializedName("results")
private List<Book> results;
public List<Book> getResults() {
return results;
}
public void setResults(List<Book> results) {
this.results = results;
}
}
In the manifest file I added
uses-permission android:name="android.permission.INTERNET
mBooks is returning null value, how could I solve this?
Thank you.
EDIT: shuvro's answer helped me correcting the problem. I also forgot to include the volumeInfo in my Book class. My book class looks as followed now:
public class Book implements Serializable {
#SerializedName("id")
private String id;
private VolumeInfo volumeInfo;
public VolumeInfo getVolumeInfo() {
return volumeInfo;
}
public void setVolumeInfo(VolumeInfo volumeInfo) {
this.volumeInfo = volumeInfo;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Additionally I created the class volumeInfo:
public class VolumeInfo {
private String title;
private String subtitle;
private String publisher;
private String description;
public String getSubtitle() {
return subtitle;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Thanks all for the help!
Add the two dependency in your gradle file .
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
Create a class , lets sats ServiceGenerator , your class should be like this
public class ServiceGenerator {
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl("https://www.googleapis.com/books/v1/")
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}
}
Now your declare your interface like this
public interface IServiceEndPoint {
#GET("volumes")
Call<BookList> getBooks(#Query("q") String id);
}
Now in activity or in fragment , use retrofit in this way
IServiceEndPoint serviceEndPoint = ServiceGenerator.createService(IServiceEndPoint.class)
Call<BookList> call = serviceEndPoint.getBooks("9781451648546");
call.enqueue(new Callback<BookList>() {
#Override
public void onResponse(Call<BookList> call, Response<BookList> response) {
//do whatever you want to do
}
#Override
public void onFailure(Call<BookList> call, Throwable t) {
}
});
You BookList POJO class has nothing to do with JSON response. It should be something like that:
public class BookList {
#SerializedName("items")
private List<Item> items = new ArrayList<Item>();
}
You can find all POJO classes for that response here.
I would go on this way and simply follow the way to do usually with retrofit.
public interface GBookService {
#GET("volumes?q=9781451648546")
Call<BookList> getBooks();
}
//
public class ApiHelper {
GBookService service;
public ApiHelper(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.googleapis.com/books/v1/")
.build();
service = retrofit.create(GBookService.class);
}
public GBookService getService(){
return service;
}
}
and where you want to use it :
Call<BookList> call = apiHelper.getService().getBooks();
call.enqueue(new Callback<BookList>() {
#Override
public void onResponse(Call<BookList> call, Response<BookList> response) {
}
#Override
public void onFailure(Call<BookList> call, Throwable t) {
}
});
And BookList, you got the idea I guess
public class BookList {
String kind;
int totalItems;
List<Book> items;
...
}
(off course adapt with your own code)
Also be sure to have added the internet permission.
You can follow this because there is no reasons to not success calling the api. just be sure also your field name are correct and match the one contained in the JSON returned.
I recently heard about Realm for android (also available for iOS) in this talk by Joaquim Verques . Very interesting and very powerful tool for persistent data.
I decided to give it a try after the video, researching and reading the documentation.
I found it very easy to use and set up but i end up stuck in the middle of my project because i couldn't manage to successfully make a query with many to many relationships.
I have created a small example for this topic.
My models:
public class Feed extends RealmObject {
#PrimaryKey
private int id;
private String title;
private String description;
private String link;
private Terms terms;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public Terms getTerms() {
return terms;
}
public void setTerms(Terms terms) {
this.terms = terms;
}
}
public class Terms extends RealmObject {
private String tag;
private RealmList<Category> categories;
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public RealmList<Category> getCategories() {
return categories;
}
public void setCategories(RealmList<Category> categories) {
this.categories = categories;
}
}
public class Category extends RealmObject {
#PrimaryKey
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
So far so good, now im going to save a list of feeds to realm (make it persistent) and then try to make some query.
public static void test(final Context context,final List<Feed> feedList) {
Realm realm = null;
try {
realm = Realm.getInstance(context);
realm.beginTransaction();
realm.copyToRealmOrUpdate(feedList);
realm.commitTransaction();
RealmResults<Feed> realmResults = realm.where(Feed.class).findAll();// return me all feeds
RealmResults<Feed> realmResults1 = realm.where(Feed.class).equalTo("id", 1).findAll(); // return feed with id 1
RealmResults<Feed> realmResults2 = realm.where(Feed.class).equalTo("terms.tag", "tech").findAll(); // return feeds //with tag = "tech"
RealmResults<Feed> realmResults3 = realm.where(Feed.class).equalTo("terms.category.name", "test").findAll(); //exception here
}
} catch (Exception e) {
//Empty
Log.d("Test","",e);
} finally {
if (realm != null)
realm.close();
}
}
}
Everything run well untill the last query and i get this exception:"java.lang.IllegalArgumentException: Invalid query: category does not refer to a class."
So my question is How do i do that kind of query successfully, as i will like realm to return me every feed with terms which has at least 1 category with name = "test"
Thank you very much
Your field is called "categories" and not "category". The third query should be:
RealmResults<Feed> realmResults3 = realm.where(Feed.class).equalTo("terms.categories.name", "test").findAll();