Reputation: 75
There is no error in the app, the logcat also does not show anything useful, the app doesn't crash, but, it also doesn't show anything. Here is the type of JSON response I'm receiving:
{
"backdrop_path": "/mmxxEpTqVdwBlu5Pii7tbedBkPC.jpg",
"created_by": [
{
"id": 1216615,
"name": "Andrew Kreisberg",
"profile_path": "/giFmwdBAw6uwC8yeHPaW6dq6YT8.jpg"
},
{
"id": 211962,
"name": "Geoff Johns",
"profile_path": "/xkaRZu1o1NILQ4PcRXqnjOrJ0Y0.jpg"
}
],
"episode_run_time": [
44
],
"first_air_date": "2014-10-07",
"genres": [
{
"id": 18,
"name": "Drama"
},
{
"id": 10765,
"name": "Sci-Fi & Fantasy"
}
],
"homepage": "http://www.cwtv.com/shows/the-flash/",
"id": 60735,
"in_production": true,
"languages": [
"en"
],
"last_air_date": "2017-04-25",
"name": "The Flash",
"networks": [
{
"id": 71,
"name": "The CW"
}
],
"number_of_episodes": 65,
"number_of_seasons": 3,
"origin_country": [
"US"
],
"original_language": "en",
"original_name": "The Flash",
"overview": "After a particle accelerator causes a freak storm, CSI Investigator Barry Allen is struck by lightning and falls into a coma. Months later he awakens with the power of super speed, granting him the ability to move through Central City like an unseen guardian angel. Though initially excited by his newfound powers, Barry is shocked to discover he is not the only \"meta-human\" who was created in the wake of the accelerator explosion -- and not everyone is using their new powers for good. Barry partners with S.T.A.R. Labs and dedicates his life to protect the innocent. For now, only a few close friends and associates know that Barry is literally the fastest man alive, but it won't be long before the world learns what Barry Allen has become...The Flash.",
"popularity": 50.611816,
"poster_path": "/lUFK7ElGCk9kVEryDJHICeNdmd1.jpg",
"production_companies": [
{
"name": "Warner Bros. Television",
"id": 1957
},
{
"name": "DC Entertainment",
"id": 9993
},
{
"name": "Berlanti Productions",
"id": 27711
}
],
"seasons": [
{
"air_date": "2016-04-19",
"episode_count": 5,
"id": 79954,
"poster_path": null,
"season_number": 0
},
{
"air_date": "2014-10-07",
"episode_count": 23,
"id": 60523,
"poster_path": "/hJysBrladxYuP5065vCAf91KcyB.jpg",
"season_number": 1
},
{
"air_date": "2015-10-06",
"episode_count": 23,
"id": 66922,
"poster_path": "/8xWZPVX1cv9V5YD1RPeLj9QZDE9.jpg",
"season_number": 2
},
{
"air_date": "2016-10-04",
"episode_count": 19,
"id": 77888,
"poster_path": "/6F8v0n37xbGY4syGcW6pRcB9BcY.jpg",
"season_number": 3
}
],
"status": "Returning Series",
"type": "Scripted",
"vote_average": 6.7,
"vote_count": 984
}
Here is a screenshot of what the app is displaying: Click here to see the image
Please note that the main poster, backdrop and all the other things including the tabLayout are a part of the MainActivity, the viewPager consists of 5 fragments of which 2 work fine in this activity; one is supposed to work and is working when I use it with another activity but doesn't work here and the other two don't work at all.
The MainActivity is able to load the data fine. Before now, I was trying to send a Bundle from this activity to the rest fragments, but out of five, three turned out to be null, the rest two are still loading the data just fine. Now, I'm using a function in MainActivity which returns the data and then am using the getActivity() method in the fragments, I know that I won't be able to use these fragments anywhere else. Now, I've checked everything and the fragments are receiving the data, but even now, they aren't displaying anything. Here is the code parsing the JSON, I'm using a recyclerView, also I'm using Volley inside AsyncTask so that the default messgaes are displayed only when everything is set:
private class connectToTheInternet extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("receivedData", response);
try {
JSONObject parentObject = new JSONObject(response);
Log.d("receivedData", response);
JSONArray genreArray = parentObject.getJSONArray("genres");
for (int i = 0; i < genreArray.length(); i++) {
JSONObject tempObject = genreArray.getJSONObject(i);
if (i < genreArray.length() - 1) {
genreCollector = genreCollector + tempObject.getString("name") + ", ";
} else
genreCollector = genreCollector + tempObject.getString("name");
}
genre.setText(genreCollector);
if (genreArray.length() == 1) {
genreTag.setText("Genres: ");
} else
genreTag.setText("Genre: ");
JSONArray creatorArray = parentObject.getJSONArray("created_by");
for (int i = 0; i < creatorArray.length(); i++) {
JSONObject tempObject = creatorArray.getJSONObject(i);
if (i < creatorArray.length() - 1) {
creatorName = creatorName + tempObject.getString("name") + ", ";
} else {
creatorName = creatorName + tempObject.getString("name");
}
}
createdBy.setText(creatorName);
JSONArray runTimeArray = parentObject.getJSONArray("episode_run_time");
int arrayLength = runTimeArray.length();
for (int i = 0; i < runTimeArray.length(); i++) {
//No need for a tempObject, as the values in this array don't have a 'key'.....
runTimeCollector += runTimeArray.getInt(i);
}
runTimeCollector = runTimeCollector / arrayLength;
runtime.setText(runTimeCollector);
numberOfSeasons.setText(parentObject.getInt("number_of_seasons"));
numberOfEpisodes.setText(parentObject.getInt("number_of_episodes"));
lastAirDate.setText(convertDate(parentObject.getString("last_air_date")));
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getContext(), e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "Error Detected", Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
runTimeTag.setText("RunTime:");
numberOfSeasonsTag.setText("Number Of Seasons:");
numberOfEpisodesTag.setText("Number Of Episodes:");
lastAirDateTag.setText("Last Air Date:");
createdByTag.setText("Created By:");
overViewTag.setText("OVERVIEW:");
}
}
Here is the logcat trace:
04-01 09:23:54.292 1503-1503/com.example.android.jsontutorial W/System.err: org.json.JSONException: No value for credits
04-01 09:23:54.292 1503-1503/com.example.android.jsontutorial W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
04-01 09:23:54.292 1503-1503/com.example.android.jsontutorial W/System.err: at org.json.JSONObject.getJSONObject(JSONObject.java:609)
04-01 09:23:54.292 1503-1503/com.example.android.jsontutorial W/System.err: at com.example.android.jsontutorial.credits$1.onResponse(credits.java:109)
04-01 09:23:54.293 1503-1503/com.example.android.jsontutorial W/System.err: at com.example.android.jsontutorial.credits$1.onResponse(credits.java:103)
04-01 09:23:54.293 1503-1503/com.example.android.jsontutorial W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
04-01 09:23:54.293 1503-1503/com.example.android.jsontutorial W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
04-01 09:23:54.293 1503-1503/com.example.android.jsontutorial W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
04-01 09:23:54.293 1503-1503/com.example.android.jsontutorial W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
04-01 09:23:54.293 1503-1503/com.example.android.jsontutorial W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-01 09:23:54.293 1503-1503/com.example.android.jsontutorial W/System.err: at android.os.Looper.loop(Looper.java:154)
04-01 09:23:54.293 1503-1503/com.example.android.jsontutorial W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6077)
04-01 09:23:54.293 1503-1503/com.example.android.jsontutorial W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-01 09:23:54.293 1503-1503/com.example.android.jsontutorial W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
04-01 09:23:54.293 1503-1503/com.example.android.jsontutorial W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
04-01 09:24:09.710 1503-1682/com.example.android.jsontutorial D/Volley: [132] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] https://api.themoviedb.org/3/tv/top_rated?api_key=43630259102f25bfa2d21a3039b&language=en-US&page=1 0x89bd75e7 NORMAL 1> [lifetime=22435], [size=18147], [rc=200], [retryCount=1]
04-01 09:36:07.024 1503-1510/com.example.android.jsontutorial W/art: Suspending all threads took: 5.062ms
04-01 09:39:12.493 1503-1510/com.example.android.jsontutorial W/art: Suspending all threads took: 26.896ms
Please help as I'm new and don't know what to do.....
P.S. 'Credits' is the name of another java class that is supposed to display a list of all the celebrities acting in that particular movie. This isn't supposed to hinder the functioning of the other two classes that aren't displaying any data.
Upvotes: 0
Views: 122
Reputation: 17535
You are accessing credits
key value which is not exiting in your json. So please try to get credits value like this, it will get credits
key value if its exits in json
otherwise it will skip.
if your credit value is string then please try to use like
String credit = jsonObject.optString("credits")
if your credit value is JSONObject then please try to use like
JSONObject credit = jsonObject.optJSONObject("credits")
if your credit value is JSONArray then please try to use like
JSONArray credit = jsonObject.optJSONArray("credits")
Upvotes: 2
Reputation: 1140
The important bit seems to be org.json.JSONException: No value for credits
. Are you trying to get a tag called credits (that doesn't exist in your JSON data)? If so, do a check like
if(jsonObject.has("credits")) {
...
}
Upvotes: 2