I have a json. I have to get source_url into my recyclerView -
[{
"id":3110,
"date":"2020-05-07T18:33:44",
"date_gmt":"2020-05-07T18:33:44",
"modified":"2020-05-07T18:35:37",
"modified_gmt":"2020-05-07T18:35:37",
"_embedded":{
"wp:featuredmedia":[
{
"id":3111,
"date":"2020-05-07T18:33:08",
"slug":"prof-dr-abul-khair",
"source_url":"https:\/\/www.healthmen.com.bd\/wpcontent\/uploads\/2020\/05\/Prof.-Dr.-Abul-Khair-scaled.jpg"
}]
}
}]
I want to get source_url from this JSON
But, here is an array wp:featuredmedia. So that, I followed this process-
Created a class named FeaturedMedia -
public class FeaturedMedia {
#SerializedName("source_url")
#Expose
private String souceurl;
public String getSourceurl(){
return sourceurl;
}
}
Then, I created another class named MediaDetails where I take the FeaturedMedia as a List-
public class MediaDetails{
#SerializedName("wp:featuredmedia")
List<FeaturedMedia> featureMediaList;
public List<FeaturedMedia> getFeaturedMediaList(){
return featuredMediaList;
}}
Then the Model Class-
public class Model{
#SerializedName("id")
#Expose
int id;
#SerializedName("_embedded")
#Expose
MediaDetalis embedded;
public int getId(){
return id;
}
public MediaDetails getEmbedded(){
return embedded;
}}
After all, I created RecyclerViewAdapter to get the data-
public CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomAdapterHolder{
List<Model> modelList;
public void onBindViewHolder(#NonNull CustomAdapterHolder holder, int position){
final Model model=modelList.get(position);
int id= model.getId():
String embedded= String.valurOf(model.getEmbedded.getFeaturedMediaList());
}
But, embedded can not get the source_url value. I completed this CustomAdapter. Here I just presented needed code of that adapter. How can I get the source_url value in this RecyclerView?
Your POJO class will be (Respect to given JSON)
public class Model {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("date")
#Expose
private String date;
#SerializedName("date_gmt")
#Expose
private String dateGmt;
#SerializedName("modified")
#Expose
private String modified;
#SerializedName("modified_gmt")
#Expose
private String modifiedGmt;
#SerializedName("_embedded")
#Expose
private Embedded embedded;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDateGmt() {
return dateGmt;
}
public void setDateGmt(String dateGmt) {
this.dateGmt = dateGmt;
}
public String getModified() {
return modified;
}
public void setModified(String modified) {
this.modified = modified;
}
public String getModifiedGmt() {
return modifiedGmt;
}
public void setModifiedGmt(String modifiedGmt) {
this.modifiedGmt = modifiedGmt;
}
public Embedded getEmbedded() {
return embedded;
}
public void setEmbedded(Embedded embedded) {
this.embedded = embedded;
}
}
Then Embedded class
public class Embedded {
#SerializedName("wp:featuredmedia")
#Expose
private List<WpFeaturedmedium> wpFeaturedmedia = null;
public List<WpFeaturedmedium> getWpFeaturedmedia() {
return wpFeaturedmedia;
}
public void setWpFeaturedmedia(List<WpFeaturedmedium> wpFeaturedmedia) {
this.wpFeaturedmedia = wpFeaturedmedia;
}
}
Then WpFeaturedmedium
public class WpFeaturedmedium {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("date")
#Expose
private String date;
#SerializedName("slug")
#Expose
private String slug;
#SerializedName("source_url")
#Expose
private String sourceUrl;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getSourceUrl() {
return sourceUrl;
}
public void setSourceUrl(String sourceUrl) {
this.sourceUrl = sourceUrl;
}
}
Check - How to create POJO class
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;
}
}
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.
i tried retrofit with simple json response which have [ {},{},{},{},{},{},] array of objects which works but when i tried retrofit with status:
{"ok",count: 4,count_total: 4,pages: 1,posts: [{},{},{},{}] }
I came up with null results ..plz find the correct solution how do i Call the retrofit for correct result.
pojo class`
public class Categories {
#SerializedName("status")
private int status;
#SerializedName("id")
private int id;
#SerializedName("type")
private String type;
#SerializedName("title")
private String title;
#SerializedName("content")
private String content;
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 getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
mainactivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<List<Categories>> call = apiService.response();
call.enqueue(new Callback<List<Categories>>() {
#Override
public void onResponse(Call<List<Categories>> call, Response<List<Categories>> response) {
List<Categories> movies = response.body();
recyclerView
.setAdapter(new MoviesAdapter(movies,
R.layout.list_item_movie, getApplicationContext()));
}
#Override
public void onFailure(Call<List<Categories>> call, Throwable t) {
}
});
}
apiclient.java
public class ApiClient {
public static final String BASE_URL = "http://androidaura.com/health/api/get_recent_posts/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
You should change your POJO to following:
Public class Model {
String status; // because "ok" is string not boolean
int count;
int count_total;
int pages;
List<Post> posts;
public class Post {
// add attribute of your old pojo
}
}
Also make sure your class attribute be same as Server response;
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.