Issue
I am having a string which has white space. How to remove the white space in retrofit. In the below response, you can see "Poster" and "Poster " (There is whitespace)
Where exactly need to handle this json key to remove the whitespace in retrofit?
{
"movies": [{
"Title": "TheAvengers ",
"Year": "2012 ",
"Rated": "PG-13 ",
"Genre": "Action, Adventure, Sci-Fi ",
"Actors": "Robert Downey Jr., Chris Evans, Mark Ruffalo, Chris Hemsworth ",
"Plot": "Earth's mightiest heroes must come together and learn to fight as a team if they are going to stop the mischievous Loki and his alien army from enslaving humanity. ",
"Language": "English, Russian, Hindi ",
"Country": "USA ",
"Poster": "https://m.media-amazon.com/images/M/MV5BNDYxNjQyMjAtNTdiOS00NGYwLWFmNTAtNThmYjU5ZGI2YTI1XkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_SX300.jpg ",
"Released": "04 May 2012 ",
"Runtime": "143 min ",
"Director": "Joss Whedon ",
"Writer": "Joss Whedon (screenplay), Zak Penn (story), Joss Whedon (story) ",
"Awards": "Nominated for 1 Oscar. Another 38 wins \u0026 79 nominations. "
}, {
"Title": "Sleepless ",
"Year": "2017 ",
"Rated": "R ",
"Released": "13Jan 2017 ",
"Runtime": "95 min ",
"Genre": "Action, Crime, Thriller ",
"Director": "Baran bo Odar ",
"Writer": "Andrea Berloff (screenplay by), Frédéric Jardin (based on the film Nuit blanche written by), Nicolas Saada (based on the film Nuit blanche written by), Olivier Douyère (based on the film Nuit blanche written by)",
"Actors": "Jamie Foxx, Michelle Monaghan, Scoot McNairy, Dermot Mulroney ",
"Plot": "A cop with a connection to the criminal underworld scours a nightclub in search of his kidnapped son. ",
"Language": "English ",
"Country": "USA ",
"Awards": "1 nomination. ",
"Poster ": "https://m.media-amazon.com/images/M/MV5BNjEwMDAyOTM4OV5BMl5BanBnXkFtZTgwMzc4MjMyMDI@._V1_SX300.jpg "
}]}
Below is the code what I done and I am hanged where I need to remove the whitespace for Poster.
Pojo Class:
public class Movies implements Serializable {
@SerializedName("Title")
private String Title;
@SerializedName("Year")
private String Year;
@SerializedName("Rated")
private String Rated;
@SerializedName("Released")
private String Released;
@SerializedName("Runtime")
private String Runtime;
@SerializedName("Genre")
private String Genre;
@SerializedName("Director")
private String Director;
@SerializedName("Writer")
private String Writer;
@SerializedName("Actors")
private String Actors;
@SerializedName("Plot")
private String Plot;
@SerializedName("Language")
private String Language;
@SerializedName("Country")
private String Country;
@SerializedName("Awards")
private String Awards;
@SerializedName("Poster")
private String Poster;
// Getter Methods
public String getTitle() {
return Title;
}
public String getYear() {
return Year;
}
public String getRated() {
return Rated;
}
public String getReleased() {
return Released;
}
public String getRuntime() {
return Runtime;
}
public String getGenre() {
return Genre;
}
public String getDirector() {
return Director;
}
public String getWriter() {
return Writer;
}
public String getActors() {
return Actors;
}
public String getPlot() {
return Plot;
}
public String getLanguage() {
return Language;
}
public String getCountry() {
return Country;
}
public String getAwards() {
return Awards;
}
public String getPoster() {
return Poster == null ? Uri.parse("R.drawable.ic_launcher_background").toString() : Poster;
}
// Setter Methods
public void setTitle(String Title) {
this.Title = Title;
}
public void setYear(String Year) {
this.Year = Year;
}
public void setRated(String Rated) {
this.Rated = Rated;
}
public void setReleased(String Released) {
this.Released = Released;
}
public void setRuntime(String Runtime) {
this.Runtime = Runtime;
}
public void setGenre(String Genre) {
this.Genre = Genre;
}
public void setDirector(String Director) {
this.Director = Director;
}
public void setWriter(String Writer) {
this.Writer = Writer;
}
public void setActors(String Actors) {
this.Actors = Actors;
}
public void setPlot(String Plot) {
this.Plot = Plot;
}
public void setLanguage(String Language) {
this.Language = Language;
}
public void setCountry(String Country) {
this.Country = Country;
}
public void setAwards(String Awards) {
this.Awards = Awards;
}
public void setPoster(String Poster) {
this.Poster = Poster;
}
}
MovieResponse:
public class MovieResponse implements Serializable {
@SerializedName("movies")
private List<Movies> movies;
public List<Movies> getMovies() {
return movies;
}
public void setMovies(List<Movies> movies) {
this.movies = movies;
}
}
RetrofitService:
public class RetrofitService {
private static Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.myjson.com/bins/")
.addConverterFactory(GsonConverterFactory.create())
.build();
public static <S> S cteateService(Class<S> serviceClass) {
return retrofit.create(serviceClass);
}
}
MoviesRepository: (Here is where I am handling the response)
public class MoviesRepository {
private static MoviesRepository moviesRepository;
private MovieApiInterface newsApi;
public static Application application;
public static MoviesRepository getInstance() {
if (moviesRepository == null) {
moviesRepository = new MoviesRepository(application);
}
return moviesRepository;
}
public MoviesRepository(Application application) {
this.application = application;
newsApi = RetrofitService.cteateService(MovieApiInterface.class);
}
public MutableLiveData<MovieResponse> getMovieUpdates() {
final MutableLiveData<MovieResponse> moviesData = new MutableLiveData<>();
newsApi.getMovieDetails().enqueue(new Callback<MovieResponse>() {
@Override
public void onResponse(Call<MovieResponse> call, Response<MovieResponse> response) {
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
if (response.isSuccessful()) {
if(response.body()!=null) {
moviesData.setValue(response.body());
}
}
}
@Override
public void onFailure(Call<MovieResponse> call, Throwable t) {
Log.i("RETROFIT RESPONSE", "Failure"+call.toString());
Log.i("RETROFIT RESPONSE", "Failure"+t);
// moviesData.setValue(null);
}
});
return moviesData;
}
public interface MovieApiInterface {
@GET("9xqev")
Call<MovieResponse> getMovieDetails();
}
I am clueless where I need to check by converting from gson.FromJson to change the poster key string. which has whitespace.
I have used MVVM where all datas I am getting except poster which has whitespace.
public class MoviesViewModel extends AndroidViewModel {
private MutableLiveData<MovieResponse> mutableLiveData;
private MoviesRepository moviesRepository;
public MoviesViewModel(@NonNull Application application) {
super(application);
moviesRepository = new MoviesRepository(application);
}
public void init() {
if (mutableLiveData != null) {
return;
}
moviesRepository = MoviesRepository.getInstance();
mutableLiveData = moviesRepository.getMovieUpdates();
}
public MutableLiveData<MovieResponse> getNewsRepository() {
return moviesRepository.getMovieUpdates();
}
}
Other ways I tried are:
Failed Case 1 :
I am not able to place two Poster with whitespace in Serializable
@SerializedName("Poster")
private String Poster;
@SerializedName("Poster ") // with whitespace.
private String Poster;
Failed Case 2: (Used trim but this is reflecting only for values but not for key)
public String getPoster() {
return Poster == null ? Uri.parse("R.drawable.ic_launcher_background").toString() : Poster.trim();
}
Solution
Let me simplify from @JoopEggen 's answer and comments.
When you apply two fields
@SerializedName(value="Poster", alternate={"Poster ", "Pöster"})
String poster;
It throws "You can't declare multiple JSON fields named Poster"
So what's the solution is,
//assign a dummy json field as images and replace with poster
@SerializedName(value = "Images", alternate = {"Poster"}) //no whitespace
private String Images;
@SerializedName("Poster ") //with white space
private String Poster;
Handle this two images in adapter from getter setter.
String poster= movies.get(position).getPoster(); //Poster with whitespace
String image = movies.get(position).getImages(); //Poster without whitespace
Answered By - Shadow
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.