After the array inside the json object in modification?
I am working with a modification and submitting a request to the server using the post method. I am getting a response straight for this json.
adreess class for post i am using this object
{
"years":"1855",
"months":"6",
"skills":1
}
Now I have to implement an array inside an object in skills how to place this
{
"years":"1",
"months":"6",
"skills":["1","2","3"],
}
How to get an array inside an object in IM modification using an interface like this
Call<AddressResponce> address(@Body Adreess adreess);
+3
source to share
3 answers
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("years")
@Expose
private String years;
@SerializedName("months")
@Expose
private String months;
@SerializedName("skills")
@Expose
private List<String> skills = null;
public String getYears() {
return years;
}
public void setYears(String years) {
this.years = years;
}
public String getMonths() {
return months;
}
public void setMonths(String months) {
this.months = months;
}
public List<String> getSkills() {
return skills;
}
public void setSkills(List<String> skills) {
this.skills = skills;
}
}
Hope you find this helpful ..!
+4
source to share
@SerializedName("skills")
@Expose
private List<Integer> skills= new ArrayList<Integer>();
Press URl in receive mode
Source
https://drive.google.com/open?id=0BzBKpZ4nzNzUUy00M2RCSERvMWc
package com.keshav.retroft2arrayinsidearrayexamplekeshav.models;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class TopRatedMovies {
@SerializedName("page")
@Expose
private Integer page;
@SerializedName("results")
@Expose
private List<Result> results = new ArrayList<Result>();
@SerializedName("total_results")
@Expose
private Integer totalResults;
@SerializedName("total_pages")
@Expose
private Integer totalPages;
/**
*
* @return
* The page
*/
public Integer getPage() {
return page;
}
/**
*
* @param page
* The page
*/
public void setPage(Integer page) {
this.page = page;
}
/**
*
* @return
* The results
*/
public List<Result> getResults() {
return results;
}
/**
*
* @param results
* The results
*/
public void setResults(List<Result> results) {
this.results = results;
}
/**
*
* @return
* The totalResults
*/
public Integer getTotalResults() {
return totalResults;
}
/**
*
* @param totalResults
* The total_results
*/
public void setTotalResults(Integer totalResults) {
this.totalResults = totalResults;
}
/**
*
* @return
* The totalPages
*/
public Integer getTotalPages() {
return totalPages;
}
/**
*
* @param totalPages
* The total_pages
*/
public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}
}
package com.keshav.retroft2arrayinsidearrayexamplekeshav.models;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class Result {
@SerializedName("poster_path")
@Expose
private String posterPath;
@SerializedName("adult")
@Expose
private Boolean adult;
@SerializedName("overview")
@Expose
private String overview;
@SerializedName("release_date")
@Expose
private String releaseDate;
@SerializedName("genre_ids")
@Expose
private List<Integer> genreIds = new ArrayList<Integer>();
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("original_title")
@Expose
private String originalTitle;
@SerializedName("original_language")
@Expose
private String originalLanguage;
@SerializedName("title")
@Expose
private String title;
@SerializedName("backdrop_path")
@Expose
private String backdropPath;
@SerializedName("popularity")
@Expose
private Double popularity;
@SerializedName("vote_count")
@Expose
private Integer voteCount;
@SerializedName("video")
@Expose
private Boolean video;
@SerializedName("vote_average")
@Expose
private Double voteAverage;
public String getPosterPath() {
return posterPath;
}
public void setPosterPath(String posterPath) {
this.posterPath = posterPath;
}
public Boolean getAdult() {
return adult;
}
public void setAdult(Boolean adult) {
this.adult = adult;
}
public String getOverview() {
return overview;
}
public void setOverview(String overview) {
this.overview = overview;
}
public String getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
public List<Integer> getGenreIds() {
return genreIds;
}
public void setGenreIds(List<Integer> genreIds) {
this.genreIds = genreIds;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOriginalTitle() {
return originalTitle;
}
public void setOriginalTitle(String originalTitle) {
this.originalTitle = originalTitle;
}
public String getOriginalLanguage() {
return originalLanguage;
}
public void setOriginalLanguage(String originalLanguage) {
this.originalLanguage = originalLanguage;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBackdropPath() {
return backdropPath;
}
public void setBackdropPath(String backdropPath) {
this.backdropPath = backdropPath;
}
public Double getPopularity() {
return popularity;
}
public void setPopularity(Double popularity) {
this.popularity = popularity;
}
public Integer getVoteCount() {
return voteCount;
}
public void setVoteCount(Integer voteCount) {
this.voteCount = voteCount;
}
public Boolean getVideo() {
return video;
}
public void setVideo(Boolean video) {
this.video = video;
}
public Double getVoteAverage() {
return voteAverage;
}
public void setVoteAverage(Double voteAverage) {
this.voteAverage = voteAverage;
}
}
package com.keshav.retroft2arrayinsidearrayexamplekeshav;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.keshav.retroft2arrayinsidearrayexamplekeshav.adapters.RetrofitArrayInsideArrayAdapter;
import com.keshav.retroft2arrayinsidearrayexamplekeshav.api.MovieApi;
import com.keshav.retroft2arrayinsidearrayexamplekeshav.api.MovieService;
import com.keshav.retroft2arrayinsidearrayexamplekeshav.models.Result;
import com.keshav.retroft2arrayinsidearrayexamplekeshav.models.TopRatedMovies;
import com.keshav.retroft2arrayinsidearrayexamplekeshav.utils.PaginationScrollListener;
import java.util.List;
import java.util.concurrent.TimeoutException;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivityArrayInsideArray extends AppCompatActivity
{
private static final String TAG = "ArrayInsideArray";
RetrofitArrayInsideArrayAdapter adapter;
LinearLayoutManager linearLayoutManager;
RecyclerView recyclerView;
ProgressBar progressBar;
LinearLayout errorLayout;
Button btnRetry;
TextView txtError;
private static final int PAGE_START = 1; // TODO Important Role Here
// private static final int PAGE_START = 2; // TODO Important Role Here
// private static final int PAGE_START = 3; // TODO Important Role Here
// private static final int PAGE_START = 4; // TODO Important Role Here
// private static final int PAGE_START = 5; // TODO Important Role Here
private int currentPage = PAGE_START;
private MovieService movieService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.main_recycler);
progressBar = (ProgressBar) findViewById(R.id.main_progress);
errorLayout = (LinearLayout) findViewById(R.id.error_layout);
btnRetry = (Button) findViewById(R.id.error_btn_retry);
txtError = (TextView) findViewById(R.id.error_txt_cause);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle("Array Inside Array Retrofit 2");
adapter = new RetrofitArrayInsideArrayAdapter(this);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
//init service and load data
movieService = MovieApi.getClient().create(MovieService.class);
loadFirstPage();
btnRetry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadFirstPage();
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void loadFirstPage() {
Log.d(TAG, "loadFirstPage: ");
// To ensure list is visible when retry button in error view is clicked
hideErrorView();
callTopRatedMoviesApi().enqueue(new Callback<TopRatedMovies>() {
@Override
public void onResponse(Call<TopRatedMovies> call, Response<TopRatedMovies> response) {
// Got data. Send it to adapter
hideErrorView();
List<Result> results = fetchResults(response);
for(int i=0;i<results.size();i++) {
Log.e("keshav", "Title ->" + results.get(i).getTitle());
Log.e("keshav", "Id ->" + results.get(i).getGenreIds());
}
progressBar.setVisibility(View.GONE);
adapter.addAll(results);
// if (currentPage <= TOTAL_PAGES) adapter.addLoadingFooter();
// else isLastPage = true;
}
@Override
public void onFailure(Call<TopRatedMovies> call, Throwable t) {
t.printStackTrace();
showErrorView(t);
}
});
}
private List<Result> fetchResults(Response<TopRatedMovies> response) {
TopRatedMovies topRatedMovies = response.body();
return topRatedMovies.getResults();
}
private Call<TopRatedMovies> callTopRatedMoviesApi() {
return movieService.getTopRatedMovies(
getString(R.string.my_api_key),
"en_US",
currentPage
);
}
private void showErrorView(Throwable throwable) {
if (errorLayout.getVisibility() == View.GONE) {
errorLayout.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
txtError.setText(fetchErrorMessage(throwable));
}
}
private String fetchErrorMessage(Throwable throwable) {
String errorMsg = getResources().getString(R.string.error_msg_unknown);
if (!isNetworkConnected()) {
errorMsg = getResources().getString(R.string.error_msg_no_internet);
} else if (throwable instanceof TimeoutException) {
errorMsg = getResources().getString(R.string.error_msg_timeout);
}
return errorMsg;
}
// Helpers -------------------------------------------------------------------------------------
private void hideErrorView() {
if (errorLayout.getVisibility() == View.VISIBLE) {
errorLayout.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
}
}
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
}
package com.keshav.retroft2arrayinsidearrayexamplekeshav.adapters;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.keshav.retroft2arrayinsidearrayexamplekeshav.R;
import com.keshav.retroft2arrayinsidearrayexamplekeshav.models.Result;
import com.keshav.retroft2arrayinsidearrayexamplekeshav.utils.PaginationAdapterCallback;
import java.util.ArrayList;
import java.util.List;
public class RetrofitArrayInsideArrayAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// View Types
private static final int ITEM = 0;
private static final String BASE_URL_IMG = "https://image.tmdb.org/t/p/w150";
private List<Result> movieResults;
private Context context;
private boolean retryPageLoad = false;
private String errorMsg;
public RetrofitArrayInsideArrayAdapter(Context context) {
this.context = context;
movieResults = new ArrayList<>();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
Log.e("keshav", "onCreateViewHolder Adapter ->" + viewType);
switch (viewType) {
case ITEM:
View viewItem = inflater.inflate(R.layout.item_list_new, parent, false);
viewHolder = new MovieVH(viewItem);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Result result = movieResults.get(position); // Movie
Log.e("keshav", "onBindViewHolder ->" + getItemViewType(position));
switch (getItemViewType(position)) {
case ITEM:
final MovieVH movieVH = (MovieVH) holder;
// movieVH.mMovieTitle.setText(result.getTitle());
// movieVH.mMovieTitle.setText("Keshav "+result.getGenreIds());
movieVH.mMovieTitle.setText(result.getTitle() + " " + result.getGenreIds());
movieVH.mYear.setText(formatYearLabel(result));
movieVH.mMovieDesc.setText(result.getOverview());
movieVH.tv_vote_count.setText("Vote Count :- " + result.getVoteCount());
movieVH.tv_id.setText("ID :- " + result.getId());
movieVH.tv_video.setText("Video :- " + result.getVideo());
movieVH.tv_vote_average.setText("Vote Average :- " + result.getVoteAverage());
movieVH.tv_popularity.setText("Popularity :- " + result.getPopularity());
// load movie thumbnail
loadImage(result.getPosterPath())
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
// TODO: 08/11/16 handle failure
// movieVH.mProgress.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
// image ready, hide progress now
// movieVH.mProgress.setVisibility(View.GONE);
return false; // return false if you want Glide to handle everything else.
}
})
.into(movieVH.mPosterImg);
// load movie thumbnail
loadImage(result.getBackdropPath())
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
// TODO: 08/11/16 handle failure
movieVH.mProgress.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
// image ready, hide progress now
movieVH.mProgress.setVisibility(View.GONE);
return false; // return false if you want Glide to handle everything else.
}
})
.into(movieVH.iv_backdrop_path);
break;
}
}
@Override
public int getItemCount() {
return movieResults == null ? 0 : movieResults.size();
}
@Override
public int getItemViewType(int position) {
Log.e("keshav", "getItemViewType ->" + movieResults.size());
Log.e("keshav", "getItemView-1 ->" + movieResults.size());
Log.e("keshav", "getItemView-2 ->" + position);
return ITEM;
}
private String formatYearLabel(Result result) {
if (result != null && result.getReleaseDate() != null) {
Log.e("keshav", "formatYearLabel -> " + result.getReleaseDate());
return result.getReleaseDate().substring(0, 4) // we want the year only
+ " | "
+ result.getOriginalLanguage().toUpperCase();
} else {
return "Keshav";
}
}
private DrawableRequestBuilder<String> loadImage(@NonNull String posterPath) {
return Glide
.with(context)
.load(BASE_URL_IMG + posterPath)
.diskCacheStrategy(DiskCacheStrategy.ALL) // cache both original & resized image
.centerCrop()
.crossFade();
}
/*
Helpers - Pagination
_________________________________________________________________________________________________
*/
public void add(Result r) {
movieResults.add(r);
notifyItemInserted(movieResults.size() - 1);
}
public void addAll(List<Result> moveResults) {
for (Result result : moveResults) {
add(result);
}
}
public Result getItem(int position) {
return movieResults.get(position);
}
public void showRetry(boolean show, @Nullable String errorMsg) {
retryPageLoad = show;
notifyItemChanged(movieResults.size() - 1);
if (errorMsg != null) this.errorMsg = errorMsg;
}
protected class MovieVH extends RecyclerView.ViewHolder {
private TextView mMovieTitle;
private TextView mMovieDesc;
private TextView mYear; // displays "year | language"
private TextView tv_vote_count;
private TextView tv_id;
private TextView tv_video;
private TextView tv_vote_average;
private TextView tv_popularity;
private ImageView mPosterImg;
private ImageView iv_backdrop_path;
private ProgressBar mProgress;
public MovieVH(View itemView) {
super(itemView);
mMovieTitle = (TextView) itemView.findViewById(R.id.movie_title);
mMovieDesc = (TextView) itemView.findViewById(R.id.movie_desc);
tv_vote_count = (TextView) itemView.findViewById(R.id.tv_vote_count);
tv_id = (TextView) itemView.findViewById(R.id.tv_id);
tv_video = (TextView) itemView.findViewById(R.id.tv_video);
tv_vote_average = (TextView) itemView.findViewById(R.id.tv_vote_average);
tv_popularity = (TextView) itemView.findViewById(R.id.tv_popularity);
mYear = (TextView) itemView.findViewById(R.id.movie_year);
mPosterImg = (ImageView) itemView.findViewById(R.id.movie_poster);
iv_backdrop_path = (ImageView) itemView.findViewById(R.id.iv_backdrop_path);
mProgress = (ProgressBar) itemView.findViewById(R.id.movie_progress);
}
}
}
0
source to share