2017-12-29 12 views
1

VPSでホストしているMy JSONファイルが2.2 MBで、OkHttpを使用して取得する要求を作成してからJSONをログに記録すると、すべてのJSONが要求されていません。OkHttpがJSON全体を取得しない

マイコード:

public void sendJSONRequest() { 
    // init http client 
    mOkHttpClient = new OkHttpClient(); 
    // init a request 
    mRequest = new okhttp3.Request.Builder().url(url).build(); 
    // execute the request (async) 
    mOkHttpClient.newCall(mRequest).enqueue(new Callback() { 
     @Override 
     public void onFailure(Call call, IOException e) { 
      Log.i(TAG, e.getMessage()); 
     } 

     @Override 
     public void onResponse(Call call, okhttp3.Response response) throws IOException { 
      Log.i(TAG, response.body().string()); 
      parseGameJSONResponse(response.body().string()); 
     } 
    }); 
} 

parseGameJSONResponse内投げる取得エラー:

java.lang.IllegalStateException: closed 
                      at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:398) 
                      at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:392) 
                      at okhttp3.internal.Util.bomAwareCharset(Util.java:449) 
                      at okhttp3.ResponseBody.string(ResponseBody.java:174) 

JSONが

解析JSON方法切断したため、エラーがスローされます。

public ArrayList<Game> parseGameJSONResponse(String json) { 
    ArrayList<Game> upcomingGames = new ArrayList<>(); 
    // Main JSON Object 
    JSONObject mainJsonObject = null; 
    try { 
     mainJsonObject = new JSONObject(json); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    boolean removeDuplicates = mSettingsValue.getRemoveDuplicates(); 
    if (mainJsonObject != null) { 
     // MAIN JSON Data Array 
     JSONArray jsonArray = null; 
     try { 
      jsonArray = mainJsonObject.getJSONArray("data"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     if (jsonArray != null && jsonArray.length() > 0) { 
      try { 
       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject gameObject = jsonArray.getJSONObject(i); 
        Game game = new Game(); 

        if (gameObject.has("id")) { 
         game.id = gameObject.getInt("id"); 
        } 

        if (gameObject.has("name")) { 
         String name = gameObject.getString("name"); 
         game.name = name; 
         if (name.endsWith("Edition") && removeDuplicates) { 
          // skip this iteration because it's a special edition and we don't want editions if setting is set to true 
          continue; 
         } 
        } 

        if (gameObject.has("slug")) { 
         // Creates the URL here 
         game.url = gameObject.getString("slug"); 
        } 

        if (gameObject.has("updated_at")) { 
         game.updated_at = gameObject.getLong("updated_at"); 
        } 

        if (gameObject.has("summary")) { 
         game.summary = gameObject.getString("summary"); 
        } 

        if (gameObject.has("first_release_date")) { 
         game.first_release_date = gameObject.getLong("first_release_date"); 
        } 

        // Game Release Dates 
        if (gameObject.has("release_dates")) { 
         JSONArray jsonReleaseDatesArray = gameObject.getJSONArray("release_dates"); 
         ArrayList<ReleaseDate> releaseDates = new ArrayList<>(); 
         for (int y = 0; y < jsonReleaseDatesArray.length(); y++) { 
          ReleaseDate releaseDate = new ReleaseDate(); 
          JSONObject jsonReleaseDateObject = jsonReleaseDatesArray.getJSONObject(y); 
          if (jsonReleaseDateObject.has("category") && !jsonReleaseDateObject.isNull("category")) { 
           releaseDate.category = jsonReleaseDateObject.getInt("category"); 
          } 
          if (jsonReleaseDateObject.has("platform") && !jsonReleaseDateObject.isNull("platform")) { 
           releaseDate.platform = jsonReleaseDateObject.getInt("platform"); 
          } 
          if (jsonReleaseDateObject.has("date") && !jsonReleaseDateObject.isNull("date")) { 
           releaseDate.date = jsonReleaseDateObject.getLong("date"); 
          } 
          if (jsonReleaseDateObject.has("region") && !jsonReleaseDateObject.isNull("region")) { 
           releaseDate.region = jsonReleaseDateObject.getInt("region"); 
           // Toast.makeText(getContext(), releaseDate.region + ": Region", Toast.LENGTH_SHORT).show(); 
          } 
          if (jsonReleaseDateObject.has("y") && !jsonReleaseDateObject.isNull("y")) { 
           releaseDate.year = jsonReleaseDateObject.getInt("y"); 
          } 
          if (jsonReleaseDateObject.has("m") && !jsonReleaseDateObject.isNull("m")) { 
           releaseDate.month = jsonReleaseDateObject.getInt("m"); 
          } 
          if (jsonReleaseDateObject.has("human") && !jsonReleaseDateObject.isNull("human")) { 
           releaseDate.human = jsonReleaseDateObject.getString("human"); 
          } 
          releaseDates.add(releaseDate); 
         } 
         game.releaseDates = releaseDates; 
        } 

        // Screenshots 
        if (gameObject.has("screenshots")) { 
         JSONArray jsonScreenshotsArray = gameObject.getJSONArray("screenshots"); 
         ArrayList<String> screenshots = new ArrayList<>(); 
         for (int y = 0; y < jsonScreenshotsArray.length(); y++) { 
          JSONObject jsonScreenshotObject = jsonScreenshotsArray.getJSONObject(y); 
          screenshots.add(jsonScreenshotObject.getString("cloudinary_id")); 
         } 
         game.screenshots = screenshots; 
        } 

        // Videos 
        if (gameObject.has("videos")) { 
         ArrayList<String> videos = new ArrayList<>(); 
         JSONArray jsonVideosArray = gameObject.getJSONArray("videos"); 
         for (int y = 0; y < jsonVideosArray.length(); y++) { 
          JSONObject jsonVideoObject = jsonVideosArray.getJSONObject(y); 
          videos.add(jsonVideoObject.getString("video_id")); 
         } 
         game.videos = videos; 
        } 

        // Cover image 
        if (gameObject.has("cover")) { 
         JSONObject jsonCoverObject = gameObject.getJSONObject("cover"); 
         game.cover = jsonCoverObject.getString("cloudinary_id"); 
        } 

        // Websites 
        if (gameObject.has("websites")) { 
         JSONArray jsonWebsitesArray = gameObject.getJSONArray("websites"); 
         ArrayList<Website> websites = new ArrayList<>(); 
         for (int y = 0; y < jsonWebsitesArray.length(); y++) { 
          Website website = new Website(); 
          JSONObject jsonWebsiteObject = jsonWebsitesArray.getJSONObject(y); 
          website.category = jsonWebsiteObject.getInt("category"); 
          website.url = jsonWebsiteObject.getString("url"); 
          websites.add(website); 
         } 
         game.websites = websites; 
        } 

        upcomingGames.add(game); 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    Toast.makeText(getContext(), "" + upcomingGames.size(), Toast.LENGTH_SHORT).show(); 
    return upcomingGames; 
} 

ありがとう。どんな種類の助けを本当にありがとう

+0

parseGameJSONResponseのコードも追加できますか? – Stephen

+0

よろしくお願いします。問題ありません –

答えて

1

それは同じInputStreamを2回(メモリに保存できません)読み込もうとしているようです。

私はresponse.body()。string()の代わりにresponse.string()を使うべきだと思います。

また、タイミングに関連していると思われる場合は、タイムアウトを編集できます。

client = new OkHttpClient.Builder() 
    .connectTimeout(10, TimeUnit.SECONDS) 
    .writeTimeout(10, TimeUnit.SECONDS) 
    .readTimeout(30, TimeUnit.SECONDS) 
    .build(); 

詳細はこちらをご覧ください。 https://github.com/square/okhttp/issues/1240

+0

あなたはresponse.toString()を意味していますか?そしてありがとうございます –

+0

ありがとうございます。それは今働く。 string()は一度だけ呼び出すことができます。 –

関連する問題