2017-04-01 39 views
0

有一個在應用程序沒有錯誤,也logcat中沒有顯示任何有用的應用程序不會崩潰,但是,它也不會顯示任何東西。這裏是JSON響應我收到類型:安卓:無法顯示JSON數據,原因不明

{ 
"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 
} 

這裏是什麼應用程序正在顯示的截圖: Click here to see the image

請注意,主海報,背景和所有其他的事情,包括tabLayout是MainActivity的一部分,viewPager由5個片段組成,其中2個在此活動中正常工作;一個人應該工作,並正在與另一個活動使用它的工作,但在這裏不工作,其他兩個根本不工作。

在MainActivity能夠加載所述數據的罰款。在此之前,我試圖將一個Bundle從這個活動發送到其餘的片段,但五分之三的結果是空的,剩下的兩個仍然加載數據就好了。現在,我在MainActivity中使用返回數據的函數,然後在片段中使用getActivity()方法,我知道我無法在其他任何地方使用這些片段。現在,我已經檢查了一切,碎片正在接收數據,但即使是現在,它們也沒有顯示任何內容。下面是解析JSON,我使用的是recyclerView,也是我使用裏面的AsyncTask凌空代碼,以便顯示默認messgaes只有當一切設置:

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:"); 
    } 
} 

這裏是logcat的痕跡:

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 

請幫助,因爲我是新的,不知道該怎麼辦.....

PS 「Credits」是另一個java類的名稱,應該顯示所有在該特定電影中表演的名人名單。這不會妨礙其他兩個不顯示任何數據的類的功能。

+0

崩潰發生在Credit類,更具體地在'credits.java' o正如錯誤日誌所說的那樣,第109行。 –

回答

1

最重要的一點似乎是org.json.JSONException: No value for credits。您是否試圖獲得一個名爲credits的標籤(這在您的JSON數據中不存在)?如果是這樣,做一次檢查像

if(jsonObject.has("credits")) { 
    ... 
} 
+0

學分是應該顯示出現在電視連續劇中的所有名人列表的另一個班級的名稱,這個班級中的任何問題都不應妨礙兩個不起作用的主要班級的功能。 –

1

您正在訪問這是不是在你的JSON退出credits鍵值。所以請嘗試獲得這樣的信用值,如果它退出json,它將獲得credits關鍵值,否則它將跳過。

,如果你的信用值是字符串,那麼請儘量使用像

String credit = jsonObject.optString("credits") 

如果你的信用值的JSONObject然後請儘量使用像

JSONObject credit = jsonObject.optJSONObject("credits") 

如果你的信用值JSONArray那麼請嘗試使用像

JSONArray credit = jsonObject.optJSONArray("credits")