java - OkHttp 没有得到整个 JSON

标签 java android json okhttp

我在我的 VPS 中托管的 JSON 文件是 2.2 MB,当我使用 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;
}

谢谢大家。非常感谢任何形式的帮助,非常感谢

最佳答案

它似乎尝试读取相同的 InputStream 两次(可能不会保存在内存中)。

我认为您应该只使用 response.string() 而不是 response.body().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

关于java - OkHttp 没有得到整个 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48026734/

相关文章:

java - Android:如何优化从 firebase 获取数据?

java - JBoss 启动 java.io.FileInputStream 初始化 AbstractKernelController 时出错

android - ImageView 作为网络图像的掩码或参数 - 如何?

java - 刷新 JLabel 图标图像

java - 程序输出太多行?

java - 为什么我在这里得到一个 ClassCastException?

android - Android 中的随机布局 XML

php - 在php中修改json数组的日期格式

javascript - 如何处理未定义 JSON 上的未捕获类型错误?

javascript - 无法从 Angular 中的 JSON 对象填充数组