有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaOKHTTP无法获取整个JSON

我在VPS中托管的JSON文件是2.2MB,当我使用OkHttp创建一个请求来检索它,然后记录JSON时,我发现并不是所有的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;
}

谢谢你们。非常感谢您的帮助,谢谢


共 (1) 个答案

  1. # 1 楼答案

    它似乎试图读取相同的输入流两次(可能不会保存在内存中)

    我认为你应该用公正的回答。字符串()而不是响应。body()。字符串()

    此外,如果您认为这可能与计时有关,则可以编辑超时

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

    要了解更多信息,请查看此。 https://github.com/square/okhttp/issues/1240