Issue
Hey guys I got a weird bug I have two int: totalPages and totalResults which are initialized in method getFilmographySizeAndPaginationSize() as you can see bellow and and I checked with Log.v() and they are correctly initialize but when I try to reuse totalPages and totalResults at method onStart() and in method getFullFilmography(); and I try to check with a Log.v() they both give = 0
I'm using OkHTTP library but I checked and JSON received is okay, then I use the following snippet to assign totalPages and totalResults :
JSONObject results = new JSONObject(jsonData);
int pages = results.getInt("total_pages");
totalPages = pages;
setTotalPages(pages);
int totalResult = results.getInt("total_results");
totalResults = totalResult;
setTotalResults(totalResult);
Relevant snippet of activity class:
public class ActorFilmographyActivity extends AppCompatActivity {
public static final String TAG = ActorFilmographyActivity.class.getSimpleName();
@Bind(R.id.actorNameLabel)
TextView actorLabel;
@Bind(R.id.actorProfileImage)
ImageView profileImage;
@Bind(R.id.recyclerView)
RecyclerView filmsRecyclerView;
private Film[] filmography;
private FilmographyAdapter adapter;
private Actor actor;
private static final String API_KEY = "0000000000";
private Response response;
public int totalResults;
public int totalPages;
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getTotalResults() {
return totalResults;
}
public void setTotalResults(int totalResults) {
this.totalResults = totalResults;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actor_filmography);
ButterKnife.bind(this);
Intent intent = getIntent();
if (intent != null) {
actor = (Actor) intent.getSerializableExtra("actor");
actorLabel.setText(actor.getName().toString());
loadActorProfilePicture();
getFilmographySizeAndPaginationSize();
}
}
@Override
protected void onStart() {
super.onStart();
Log.v("ON START", totalPages + " " + totalResults);
getFullFilmography();
}
private void getFullFilmography() {
Toast.makeText(ActorFilmographyActivity.this,"getFullFilmography" + totalPages + " " + totalResults, Toast.LENGTH_SHORT).show();
Log.v(TAG, "pager ");
}
private void getFilmographySizeAndPaginationSize() {
String filmographyByActorIdUrl = "http://api.themoviedb.org/3/discover/movie?with_cast=" + actor.getId() + "&sort_by=release_date.asc&api_key=" + API_KEY + "&page=" + 1;
OkHttpClient client = new OkHttpClient();
final Request request = new Request.Builder()
.url(filmographyByActorIdUrl)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
alertUserAboutError();
}
@Override
public void onResponse(Response response) throws IOException {
final String jsonData = response.body().string();
Log.v(TAG, "REST raw response for page " + 1 + jsonData);
try {
JSONObject results = new JSONObject(jsonData);
int pages = results.getInt("total_pages");
totalPages = pages;
setTotalPages(pages);
int totalResult = results.getInt("total_results");
totalResults = totalResult;
setTotalResults(totalResult);
Log.v(TAG, "total pages=" + totalPages + " and results= " + totalResults); //PRINTS total pages = XX and result= XX but then totalPages and totalResults print both 0 at method getFullFilmography(); on onStart()
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
//....
}
As you can see checking with debugger JSON is being retrieved correctly on method getFilmographySizeAndPaginationSize() :

But when I use debugger in method getFullFilmography() now totalPages and totalResults are both set to 0.
Appreciate any help!
Solution
Your asynchronous task, which you start to execute in your onCreate(...) method has not finished, if onStart(...) was called by the operating system.
Try to move your stuff which was placed in your onStart(...)-method into a method which could be called after you get your result from your asychronous http-operation.
Answered By - Christopher

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.