Reputation: 63
I am working on an Android project where I am downloading information from https://www.themoviedb.org/ including a jpg file to be displayed in an activity. All is working as expected aside from time to time the jpg path is empty, resulting in a blank square instead of a poster. Example of a constructed url when a jpg is available: http://image.tmdb.org/t/p/w185/3cvZAUVP0gjP0p54TOVXQ50fEyZ.jpg Example of a constructed url when a jpg is NOT available: http://image.tmdb.org/t/p/w185null
So I thought I could simply replace the null one with a local png file located in my drawable folder, however I have tried many different approaches, including stating that
if (poster.equals("http://image.tmdb.org/t/p/w185null")) {
poster = "@drawable/noposter.png";
}
However in the android monitor I can see it is recognized as a string. I read the android developer's Accessing Resources in Code section however it doesn't cover my specific need.
I can't seem to display a default picture if needed.
I would greatly appreciate your help.
Down below is the code working but displaying a blank square if no jpg is available:
public class GetMoviesInfo extends AsyncTask<String, Void, List<Movies>> {
public InterfaceAsyncResponse delegate;
private final String LOGTAG = GetMoviesInfo.class.getSimpleName();
private final String API_KEY_HEX_NUM = "a valid key is here in my project";
private final String MOVIE_POSTER_BASE_URL = "http://image.tmdb.org/t/p/";
private final String CHOSEN_MOVIE_POSTER_SIZE = "w185";
public GetMoviesInfo(InterfaceAsyncResponse delegate){
this.delegate = delegate;
}
@Override
protected List<Movies> doInBackground(String... parameters) {
if (parameters.length == 0) {
return null;
}
HttpURLConnection urlConnection = null;
BufferedReader inputReader = null;
String moviesStr = null;
try {
final String BASEURL = "http://api.themoviedb.org/3/discover/movie?";
final String SORT_BY_KEY = "sort_by";
final String APIKEY = "api_key";
String sortedBy = parameters[0];
Uri buildUri = Uri.parse(BASEURL).buildUpon()
.appendQueryParameter(SORT_BY_KEY, sortedBy)
.appendQueryParameter(APIKEY, API_KEY_HEX_NUM)
.build();
URL url = new URL(buildUri.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer bufferStr = new StringBuffer();
if (inputStream == null) {
return null;
}
inputReader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = inputReader.readLine()) != null) {
bufferStr.append(inputLine + "\n");
}
if (bufferStr.length() == 0) {
return null;
}
moviesStr = bufferStr.toString();
} catch (IOException e) {
Log.e(LOGTAG, "Error ", e);
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputReader != null) {
try {
inputReader.close();
} catch (final IOException e) {
Log.e(LOGTAG, "Error closing stream", e);
}
}
}
try {
return extractData(moviesStr);
} catch (JSONException e) {
Log.e(LOGTAG, e.getMessage(), e);
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<Movies> returns) {
if (returns != null) {
delegate.onTaskCompleted(returns);
}
}
private String getReleasedYear(String dateReleased){
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
final Calendar calendar = Calendar.getInstance();
try {
calendar.setTime(dateFormat.parse(dateReleased));
} catch (ParseException e) {
e.printStackTrace();
}
return Integer.toString(calendar.get(Calendar.YEAR));
}
private List<Movies> extractData(String moviesJsonString) throws JSONException {
// Items being extracted
final String MOVIES_ARRAY = "results";
final String ORIGINAL_TITLE = "original_title";
final String POSTER_PATH = "poster_path";
final String OVERVIEW = "overview";
final String VOTE_AVERAGE = "vote_average";
final String RELEASED_DATE = "release_date";
final String POPULARITY = "popularity";
JSONObject moviesJson = new JSONObject(moviesJsonString);
JSONArray moviesArray = moviesJson.getJSONArray(MOVIES_ARRAY);
int moviesLength = moviesArray.length();
List<Movies> movies = new ArrayList<>();
for(int i = 0; i < moviesLength; ++i) {
// For each movie, the JSON object create a new movie object
JSONObject movie = moviesArray.getJSONObject(i);
String title = movie.getString(ORIGINAL_TITLE);
String poster = MOVIE_POSTER_BASE_URL + CHOSEN_MOVIE_POSTER_SIZE + movie.getString(POSTER_PATH);
Log.i("TAG","From GetMoviesInfo_extractData: "+ poster);
String overview = movie.getString(OVERVIEW);
String voteAverage = movie.getString(VOTE_AVERAGE);
String releasedDate = getReleasedYear(movie.getString(RELEASED_DATE));
String popularity = movie.getString(POPULARITY);
movies.add(new Movies(title, poster, overview, voteAverage, releasedDate, popularity));
}
return movies;
}
}
Upvotes: 0
Views: 92
Reputation: 211
you can use picasso library for completing your goal, It is very simple to use i'm sharing the code. for more information use this http://square.github.io/picasso/
> Picasso.with(context)
> .load(url)
> .placeholder(R.drawable.user_placeholder)
> .error(R.drawable.user_placeholder_error)
> .into(imageView);
Upvotes: 1