Issue
I want to get and show a value obtained with an Asynctask but i'm not very familiar with it...
My AsyncTask :
private class loadDetails extends AsyncTask<Void,Void,Void>() {
@Override
protected void doInBackground(Void... voids) {
final Movie details = (Movie) getIntent().getExtras().getSerializable("MOVIE_DETAILS");
int idMovie = details.getId();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://api.themoviedb.org/3/movie/" + idMovie + "?api_key=" + API_KEY + "&language=en-US")
.build();
Response response = client.newCall(request).execute();
JSONObject root = new JSONObject(response.body().string());
int runtime = root.getInt("runtime");
Log.i("test", runtime + " min");
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
//?
}
}
My full activity code :
public class MovieDetailActivity extends AppCompatActivity {
private ImageView backdrop_image, poster;
private TextView overview, releaseDate, genresList;
private RatingBar rating;
private static final String TAG = "MovieDetails";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_movie_detail);
new loadDetails().execute();
final Movie details = (Movie) getIntent().getExtras().getSerializable("MOVIE_DETAILS");
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(details.getTitle());
backdrop_image = findViewById(R.id.movie_backdrop_image);
poster = findViewById(R.id.movie_poster);
title = findViewById(R.id.movie_title);
releaseDate = findViewById(R.id.movie_release_date);
rating = (RatingBar) findViewById(R.id.movie_rating_bar);
overview = findViewById(R.id.movie_overview);
genresList = findViewById(R.id.movie_genres);
if (details != null){
Glide.with(this)
.load(IMAGE_BASE_URL + details.getBackdropPath())
.apply(RequestOptions.placeholderOf(R.color.colorPrimary))
.into(backdrop_image);
Glide.with(this)
.load(IMAGE_BASE_URL + details.getPosterPath())
.apply(RequestOptions.placeholderOf(R.color.colorPrimary))
.into(poster);
title.setText(details.getTitle());
releaseDate.setText(details.getReleaseDate());
overview.setText(details.getOverview());
double d = details.getTMDBRating();
float f = (float) d;
rating.setRating(f/2);
}
}
private class loadDetails extends AsyncTask<Void,Void,Void>() {
@Override
protected void doInBackground(Void... voids) {
final Movie details = (Movie) getIntent().getExtras().getSerializable("MOVIE_DETAILS");
int idMovie = details.getId();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://api.themoviedb.org/3/movie/" + idMovie + "?api_key=" + API_KEY + "&language=en-US")
.build();
Response response = client.newCall(request).execute();
JSONObject root = new JSONObject(response.body().string());
int runtime = root.getInt("runtime");
Log.i("test", runtime + " min");
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
//?
}
}
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.save_menu, menu);
return true;
}
}
I want to display the runtime
in a TextView.
Solution
private class loadDetails extends AsyncTask<Void,Void,Integer>() {
@Override
protected Integer doInBackground(Void... voids) {
final Movie details = (Movie) getIntent().getExtras().getSerializable("MOVIE_DETAILS");
int idMovie = details.getId();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://api.themoviedb.org/3/movie/" + idMovie + "?api_key=" + API_KEY + "&language=en-US")
.build();
Response response = client.newCall(request).execute();
JSONObject root = new JSONObject(response.body().string());
int runtime = root.getInt("runtime");
Log.i("test", runtime + " min");
return runtime;
}
@Override
protected void onPostExecute(Integer anInt) {
yourTextView.setText("yourInt = " + anInt);
}
}
that should do the trick.. I didn't compile it but generally the 3rd generics parameter of the async task is the return type from doInBackround()
to onPostExecute()
and there you can touch your views.
Answered By - Alon Aviram
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.