I'm having JSON format as below, I'm able to fetch this JSON data into android using retrofit but after fetching I'm not getting how to display it in Horizontal RecyclerView in Vertical RecyclerView as shown in below image, I want AAAAA to be displayed instead of Section1 and name1 instead of item1 and respective image to be displayed. Please help me to write android code for this.
{"result":
{"AAAAA":[{"firm_name":"name1","image_path":"1.jpg"},
{"firm_name":"name2","image_path":"2.jpg"}],
"BBBBB":[{"firm_name":"name1","image_path":"1.jpg"}],
"CCCCC":[{"firm_name":"name1","image_path":"1.jpg"}],
"DDDDD":[{"firm_name":"name1","image_path":"1.jpg"}],
"EEEEE":[{"firm_name":"name1","image_path":"1.jpg"}]
}
}
This is my Json code
Here is my pojo class
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
#SerializedName("result")
#Expose
private Result result;
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
}
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Result {
#SerializedName("AAAAA")
#Expose
private List<AAAAA> aAAAA = null;
#SerializedName("BBBBB")
#Expose
private List<BBBBB> bBBBB = null;
#SerializedName("CCCCC")
#Expose
private List<Object> cCCCC = null;
#SerializedName("DDDDD")
#Expose
private List<Object> dDDDD = null;
#SerializedName("EEEEE")
#Expose
private List<Object> eEEEE = null;
public List<AAAAA> getAAAAA() {
return aAAAA;
}
public void setAAAAA(List<AAAAA> aAAAA) {
this.aAAAA = aAAAA;
}
public List<BBBBB> getBBBBB() {
return bBBBB;
}
public void setBBBBB(List<BBBBB> bBBBB) {
this.bBBBB = bBBBB;
}
public List<Object> getCCCCC() {
return cCCCC;
}
public void setCCCCC(List<Object> cCCCC) {
this.cCCCC = cCCCC;
}
public List<Object> getDDDDD() {
return dDDDD;
}
public void setDDDDD(List<Object> dDDDD) {
this.dDDDD = dDDDD;
}
public List<Object> getEEEEE() {
return eEEEE;
}
public void setEEEEE(List<Object> eEEEE) {
this.eEEEE = eEEEE;
}
}
Related
I am setting up the recyclerview to show some data from the server and this is how my jsonpojo looks alike :
public class JSON {
public class MainCard {
#SerializedName("Cards")
public List<Cards> cards;
}
public class Cards {
#SerializedName("Title")
public String title;
#SerializedName("Items")
public List<ItemData> items;
}
public class ItemData {
#SerializedName("Name")
public String name;
#SerializedName("Thumb")
public String thumb;
#SerializedName("Link")
public String link;
}
}
and here is the adapter :
public class API_Adpater extends RecyclerView.Adapter<API_Adpater.CardsHolder> {
private List<JSON.ItemData> mlist;
private List<JSON.Cards> mCards;
private Context mcontext;
public API_Adpater(List<JSON.ItemData> mlists, List<JSON.Cards> titles, Context context) {
this.mlist = mlists;
this.mcontext = context;
this.mCards = titles;
}
#NonNull
#Override
public CardsHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.singleitems,viewGroup,false);
return new CardsHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CardsHolder cardsHolder, int i) {
final JSON.ItemData singleitem = mlist.get(i);
final JSON.Cards Title = mCards.get(i);
cardsHolder.textView.setText(Title.title);
cardsHolder.textView2.setText(singleitem.name);
cardsHolder.url = singleitem.thumb;
Glide.with(this.mcontext).load(cardsHolder.url).into(cardsHolder.imageView);
}
#Override
public int getItemCount() {
return mlist.size();
}
class CardsHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
TextView textView2;
String url;
CardsHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
textView2 = itemView.findViewById(R.id.textView2);
}
}
}
and this is the mainactivity :
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(APIService.url)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
Call<JSON.MainCard> call = service.getCards();
call.enqueue(new Callback<JSON.MainCard>() {
#Override
public void onResponse(Call<JSON.MainCard> call, Response<JSON.MainCard> response) {
if (response.isSuccessful()) {
JSON.MainCard mainCard = response.body();
if (mainCard != null && mainCard.cards !=null) {
List<JSON.ItemData> ru = mainCard.cards.items;
// Here on Above Line it can't get the mainCard.Cards.items; it is not showing the `.items` in the code;
setupRV(ru);
}
} else {
Toast.makeText(MainActivity.this, "Reposnce Error", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(Call<JSON.MainCard> call, Throwable t) {
Toast.makeText(MainActivity.this, "Check Internet Connectivity", Toast.LENGTH_SHORT).show();
}
});
}
private void setupRV(List<JSON.ItemData> list) {
RecyclerView recyclerView = findViewById(R.id.rv);
LinearLayoutManager lm = new LinearLayoutManager(this);
recyclerView.setLayoutManager(lm);
recyclerView.setAdapter(new API_Adpater(list,this));
}
}
On response of retrofit, I need to set the line like
List<JSON.ItemData> ru = mainCard.cards.items;
but it is not working as the codeeditor is not getting the .items variable
where is the error?
Can't set the recylcer view.
This is how my json looks alike :
{
"Cards": [
{
"Title": "Title",
"Items": [
{
"Name": "Name",
"Thumb": "Thumb",
"Link": "link"
}
]
},
{
"Title": "Title",
"Items": [
{
"Name": "Name",
"Thumb": "Thumb",
"Link": "link"
}
]
}
]
}
And this the error it can't find the items from the maincards.cards
I need to set the image from the url and name and title in the recycler view.
You're accessing items directly on list in this line
List<JSON.ItemData> ru = mainCard.cards.items;
here cards is a list, that's why you can't access items directly
Get the object of Cards and then use it
List<JSON.ItemData> ru = mainCard.cards.get(index).items;
Based on your requirements
you need to iterate over
if (mainCard != null && mainCard.cards !=null) {
List<JSON.ItemData> ru = new ArrayList();
for(Cards itemCard : mainCard.cards)
{
ru.addAll(itemCard.items);
}
setupRV(ru);
}
Try this..
List<MoviesApi.ItemData> ru = mainCard.cards.getItems();
Make separate class for each model as below.
-----------------------------------com.example.Card.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Card {
#SerializedName("Title")
#Expose
public String title;
#SerializedName("Items")
#Expose
public List<Item> items = null;
}
-----------------------------------com.example.Item.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Item {
#SerializedName("Name")
#Expose
public String name;
#SerializedName("Thumb")
#Expose
public String thumb;
#SerializedName("Link")
#Expose
public String link;
}
-----------------------------------com.example.MainCard.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class MainCard {
#SerializedName("Cards")
#Expose
public List<Card> cards = null;
}
To create these pojos I used this site http://www.jsonschema2pojo.org/
I´am using retrofit 2.0 to get GSON from Youtube API address:
Link (temporarily static) for Youtube Address
GSON from Youtube address
But when I try to get the videoID from response.body it doesn´t work!
My code:
public class Videos {
#com.google.gson.annotations.SerializedName("videoId") String mVideoId;
#com.google.gson.annotations.SerializedName("title") String mTitle;
public Videos(String videoId, String title ) {
this.mVideoId = videoId;
this.mTitle = title;
}
public String getmVideoId() {
return mVideoId;
}
public void setmVideoId(String mVideoId) {
this.mVideoId = mVideoId;
}
public String getmTitle() {
return mTitle;
}
public void setmTitle(String mTitle) {
this.mTitle = mTitle;
}
}
My interface:
public interface YoutubeApiURL {
#GET("youtube/v3/playlistItems")
Call<Videos> listVideos (
#Query("part") String part,
#Query("maxResults") String maxResults,
#Query("playlistId") String playlistId,
#Query("fields") String fields,
#Query("key") String key
);
}
And here my MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn_play);
youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtube_player_view);
btnListar = (Button)findViewById(R.id.btnListaGitHub);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.googleapis.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
YoutubeApiURL service = retrofit.create(YoutubeApiURL.class);
Call<Videos> videos = service.listVideos(
"snippet",
"50",
"PLbZ3V_t0ZLzylEoYuPtmT5uArAzMewz-m",
"items/snippet/resourceId/videoId",
"AIzaSyAH2YDhp_Yle3NhLeCuBqH654lUre4vDHw");
myURL = videos.request().url().toString();
Log.i("ListVideos", myURL);
videos.enqueue(new Callback<Videos>() {
#Override
public void onResponse(Call<Videos> call, Response<Videos> response) {
if (response.isSuccessful()) {
Log.i("ListVideos","Works!");
// Here some code to show de videoIds return
} else {
Log.i("ListVideos","Something wrong");
}
}
#Override
public void onFailure(Call<Videos> call, Throwable t) {
Log.i("ListVideos: ", t.getLocalizedMessage());
}
});
When I ran my code it shows to me that "Works!", but I tried in several ways to get the videoID with no success!
What I doing wrong?
You are making a model class wrong. try below
Videos
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Videos {
#SerializedName("items")
#Expose
private List<Item> items = null;
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
public Videos withItems(List<Item> items) {
this.items = items;
return this;
}
}
Snippet
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Snippet {
#SerializedName("resourceId")
#Expose
private ResourceId resourceId;
public ResourceId getResourceId() {
return resourceId;
}
public void setResourceId(ResourceId resourceId) {
this.resourceId = resourceId;
}
public Snippet withResourceId(ResourceId resourceId) {
this.resourceId = resourceId;
return this;
}
}
ResourceId
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ResourceId {
#SerializedName("videoId")
#Expose
private String videoId;
public String getVideoId() {
return videoId;
}
public void setVideoId(String videoId) {
this.videoId = videoId;
}
public ResourceId withVideoId(String videoId) {
this.videoId = videoId;
return this;
}
}
Item
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Item {
#SerializedName("snippet")
#Expose
private Snippet snippet;
public Snippet getSnippet() {
return snippet;
}
public void setSnippet(Snippet snippet) {
this.snippet = snippet;
}
public Item withSnippet(Snippet snippet) {
this.snippet = snippet;
return this;
}
}
Now for get Video id use below code in success method
response.body().getItems().get(0).getSnippet().getResourceId().getVideoId();
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
Guys recently i switched to Retrofit from volley.
there is a Pojo file which is converted from json.
public class JobModel {
private int status;
private List<JobsBean> jobs;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public List<JobsBean> getJobs() {
return jobs;
}
public void setJobs(List<JobsBean> jobs) {
this.jobs = jobs;
}
public static class JobsBean {
private String job_city;
public String getJob_city() {
return job_city;
}
}
}
but i don't know how to use this pojo file to extract the job_city from JobsBean class
As you can see there is an JsonArray jobs which is converted to
List<JobsBean>
having JsonObjects and the
JobsBean class
is containing all the job_city name.
How can i retrieve these job_city name in an array.
so that i can use them in my arrayadapter.
Change the POJO structure as follow:
public class JobModel {
private int status;
private List<JobsBean> jobs;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public List<JobsBean> getJobs() {
return jobs;
}
public void setJobs(List<JobsBean> jobs) {
this.jobs = jobs;
}
}
public class JobsBean {
private String job_city;
public String getJob_city() {
return job_city;
}
public void setJob_city(String job_city) {
this.job_city = job_city;
}
}
The default GsonConverterFactory should be more than enough to handle this nested POJO. And you should be able to get the result like:
JobModel.getJobs().get(index).getJob_city();
Use ArrayAdapter<JobsBean> and it will take a JobsBean list as a parameter for the model data.
You will need to override getView() to read the data from the JobsBean item and put it into the list item view.
New to using okhttp and Gson. I am practicing by creating a List View that will display information from Rotten Tomatoes API
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
ListView listView;
Response responseObj;
CustomAdapter adapter;
String url = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?apikey=9htuhtcb4ymusd73d4z6jxcj";
Gson gson;
OkHttpClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.myList);
client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Request request, IOException e) {
}
#Override
public void onResponse(com.squareup.okhttp.Response response) throws IOException {
String responseStr = response.body().string();
gson = new Gson();
**responseObj = gson.fromJson(responseStr,Response.class);**
adapter = new CustomAdapter(MainActivity.this, responseObj.getMovies());
listView.setAdapter(adapter);
}
});
}
}
This is the Error I get for line 44
FATAL EXCEPTION: OkHttp Dispatcher
com.google.gson.JsonSyntaxException: java.lang.NumberFormatException:
Invalid double: ""
public class Response {
private String link_template;
/**
* id : 771312089
* title : The Hunger Games: Mockingjay - Part 2
* year : 2015
* mpaa_rating : PG-13
* runtime : 136
* critics_consensus :
* release_dates : {"theater":"2015-11-20"}
* ratings : {"critics_rating":"Fresh","critics_score":70,"audience_rating":"Upright","audience_score":71}
* synopsis : The second half of Suzanne Collins' final Hunger Games book is adapted in this Lionsgate production. ~ Jeremy Wheeler, Rovi
* posters : {"thumbnail":"http://resizing.flixster.com/nim-D7-9jGbUZS5wczNes_PmWyI=/53x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/20/29/11202951_ori.jpg","profile":"http://resizing.flixster.com/nim-D7-9jGbUZS5wczNes_PmWyI=/53x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/20/29/11202951_ori.jpg","detailed":"http://resizing.flixster.com/nim-D7-9jGbUZS5wczNes_PmWyI=/53x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/20/29/11202951_ori.jpg","original":"http://resizing.flixster.com/nim-D7-9jGbUZS5wczNes_PmWyI=/53x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/20/29/11202951_ori.jpg"}
* abridged_cast : [{"name":"Jennifer Lawrence","id":"770800260","characters":["Katniss Everdeen"]},{"name":"Julianne Moore","id":"162654248","characters":["President Alma Coin"]},{"name":"Gwendoline Christie","id":"771060732","characters":["Commander Lyme"]},{"name":"Josh Hutcherson","id":"162654356","characters":["Peeta Mellark"]},{"name":"Robert Knepper","id":"162707688","characters":["Antonius"]}]
* links : {"self":"http://api.rottentomatoes.com/api/public/v1.0/movies/771312089.json","alternate":"http://www.rottentomatoes.com/m/the_hunger_games_mockingjay_part_2/","cast":"http://api.rottentomatoes.com/api/public/v1.0/movies/771312089/cast.json","reviews":"http://api.rottentomatoes.com/api/public/v1.0/movies/771312089/reviews.json","similar":"http://api.rottentomatoes.com/api/public/v1.0/movies/771312089/similar.json"}
*/
private List<MoviesEntity> movies;
public void setLinks(LinksEntity links) {
this.links = links;
}
public void setLink_template(String link_template) {
this.link_template = link_template;
}
public void setMovies(List<MoviesEntity> movies) {
this.movies = movies;
}
public LinksEntity getLinks() {
return links;
}
public String getLink_template() {
return link_template;
}
public List<MoviesEntity> getMovies() {
return movies;
}
public static class LinksEntity {
private String self;
private String alternate;
public void setSelf(String self) {
this.self = self;
}
public void setAlternate(String alternate) {
this.alternate = alternate;
}
public String getSelf() {
return self;
}
public String getAlternate() {
return alternate;
}
}
public static class MoviesEntity {
private String id;
private String title;
private int year;
private String mpaa_rating;
private int runtime;
private String critics_consensus;
/**
* theater : 2015-11-20
*/
private ReleaseDatesEntity release_dates;
/**
* critics_rating : Fresh
* critics_score : 70
* audience_rating : Upright
* audience_score : 71
*/
private RatingsEntity ratings;
private String synopsis;
/**
* thumbnail : http://resizing.flixster.com/nim-D7-9jGbUZS5wczNes_PmWyI=/53x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/20/29/11202951_ori.jpg
* profile : http://resizing.flixster.com/nim-D7-9jGbUZS5wczNes_PmWyI=/53x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/20/29/11202951_ori.jpg
* detailed : http://resizing.flixster.com/nim-D7-9jGbUZS5wczNes_PmWyI=/53x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/20/29/11202951_ori.jpg
* original : http://resizing.flixster.com/nim-D7-9jGbUZS5wczNes_PmWyI=/53x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/20/29/11202951_ori.jpg
*/
private PostersEntity posters;
/**
* self : http://api.rottentomatoes.com/api/public/v1.0/movies/771312089.json
* alternate : http://www.rottentomatoes.com/m/the_hunger_games_mockingjay_part_2/
* cast : http://api.rottentomatoes.com/api/public/v1.0/movies/771312089/cast.json
* reviews : http://api.rottentomatoes.com/api/public/v1.0/movies/771312089/reviews.json
* similar : http://api.rottentomatoes.com/api/public/v1.0/movies/771312089/similar.json
*/
private LinksEntity links;
/**
* name : Jennifer Lawrence
* id : 770800260
* characters : ["Katniss Everdeen"]
*/
private List<AbridgedCastEntity> abridged_cast;
public void setId(String id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setYear(int year) {
this.year = year;
}
public void setMpaa_rating(String mpaa_rating) {
this.mpaa_rating = mpaa_rating;
}
public void setRuntime(int runtime) {
this.runtime = runtime;
}
public void setCritics_consensus(String critics_consensus) {
this.critics_consensus = critics_consensus;
}
public void setRelease_dates(ReleaseDatesEntity release_dates) {
this.release_dates = release_dates;
}
public void setRatings(RatingsEntity ratings) {
this.ratings = ratings;
}
public void setSynopsis(String synopsis) {
this.synopsis = synopsis;
}
public void setPosters(PostersEntity posters) {
this.posters = posters;
}
public void setLinks(LinksEntity links) {
this.links = links;
}
public void setAbridged_cast(List<AbridgedCastEntity> abridged_cast) {
this.abridged_cast = abridged_cast;
}
public String getId() {
return id;
}
public String getTitle() {
return title;
}
public int getYear() {
return year;
}
public String getMpaa_rating() {
return mpaa_rating;
}
public int getRuntime() {
return runtime;
}
public String getCritics_consensus() {
return critics_consensus;
}
public ReleaseDatesEntity getRelease_dates() {
return release_dates;
}
public RatingsEntity getRatings() {
return ratings;
}
public String getSynopsis() {
return synopsis;
}
public PostersEntity getPosters() {
return posters;
}
public LinksEntity getLinks() {
return links;
}
public List<AbridgedCastEntity> getAbridged_cast() {
return abridged_cast;
}
public static class ReleaseDatesEntity {
private String theater;
public void setTheater(String theater) {
this.theater = theater;
}
public String getTheater() {
return theater;
}
}
public static class RatingsEntity {
private String critics_rating;
private int critics_score;
private String audience_rating;
private int audience_score;
public void setCritics_rating(String critics_rating) {
this.critics_rating = critics_rating;
}
public void setCritics_score(int critics_score) {
this.critics_score = critics_score;
}
public void setAudience_rating(String audience_rating) {
this.audience_rating = audience_rating;
}
public void setAudience_score(int audience_score) {
this.audience_score = audience_score;
}
public String getCritics_rating() {
return critics_rating;
}
public int getCritics_score() {
return critics_score;
}
public String getAudience_rating() {
return audience_rating;
}
public int getAudience_score() {
return audience_score;
}
}
public static class PostersEntity {
private String thumbnail;
private String profile;
private String detailed;
private String original;
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public void setProfile(String profile) {
this.profile = profile;
}
public void setDetailed(String detailed) {
this.detailed = detailed;
}
public void setOriginal(String original) {
this.original = original;
}
public String getThumbnail() {
return thumbnail;
}
public String getProfile() {
return profile;
}
public String getDetailed() {
return detailed;
}
public String getOriginal() {
return original;
}
}
public static class LinksEntity {
private String self;
private String alternate;
private String cast;
private String reviews;
private String similar;
public void setSelf(String self) {
this.self = self;
}
public void setAlternate(String alternate) {
this.alternate = alternate;
}
public void setCast(String cast) {
this.cast = cast;
}
public void setReviews(String reviews) {
this.reviews = reviews;
}
public void setSimilar(String similar) {
this.similar = similar;
}
public String getSelf() {
return self;
}
public String getAlternate() {
return alternate;
}
public String getCast() {
return cast;
}
public String getReviews() {
return reviews;
}
public String getSimilar() {
return similar;
}
}
public static class AbridgedCastEntity {
private String name;
private String id;
private List<String> characters;
public void setName(String name) {
this.name = name;
}
public void setId(String id) {
this.id = id;
}
public void setCharacters(List<String> characters) {
this.characters = characters;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public List<String> getCharacters() {
return characters;
}
}
}
}
package com.example.nano1.gsonexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class CustomAdapter extends BaseAdapter {
private List<Response.MoviesEntity> mMovieItem;
private Context context;
private LayoutInflater inflater;
public CustomAdapter(Context context, List<Response.MoviesEntity> mMovieItem) {
this.context = context;
this.mMovieItem = mMovieItem;
}
#Override
public int getCount() {
return mMovieItem.size();
}
#Override
public Object getItem(int position) {
return mMovieItem.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.each_list_item, parent, false);
Response.MoviesEntity item = (Response.MoviesEntity) getItem(position);
ImageView thumbnail = (ImageView) rowView.findViewById(R.id.thumnnail);
TextView title = (TextView) rowView.findViewById(R.id.title);
TextView rating = (TextView) rowView.findViewById(R.id.rating);
String imageURL = item.getPosters().getOriginal();
Picasso.with(context).load(imageURL).into(thumbnail);
title.setText(item.getTitle());
rating.setText(item.getRatings().getAudience_rating());
return rowView;
}
}
in the declared classes above you have members of type int now in the response you are getting values for these members as empty string "" which is not allowed, it should be an integer. that makes the exception, you either:
1- change member type to String, and handle empty string as 0 in the setters/getters
or
2- ask the back-end team to send correct data
or
3- use a custom TypedAdapter in the Gson converter to handle integers when they have empty string
P.S: i am aware the sample json does not contain empty strings but on the real call on the service, you might have an empty strings for integer members