Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OkHttp不';我无法获取整个JSON_Java_Android_Json_Okhttp3 - Fatal编程技术网

Java OkHttp不';我无法获取整个JSON

Java OkHttp不';我无法获取整个JSON,java,android,json,okhttp3,Java,Android,Json,Okhttp3,我在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)

我在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;
}
public ArrayList parseGameJSONResponse(字符串json){
ArrayList upcomingGames=新建ArrayList();
//主JSON对象
JSONObject mainJsonObject=null;
试一试{
mainJsonObject=新的JSONObject(json);
}捕获(JSONException e){
e、 printStackTrace();
}
布尔RemovedUpplicates=mSettingsValue.GetRemovedUpplicates();
if(mainJsonObject!=null){
//主JSON数据数组
JSONArray JSONArray=null;
试一试{
jsonArray=mainJsonObject.getJSONArray(“数据”);
}捕获(JSONException e){
e、 printStackTrace();
}
if(jsonArray!=null&&jsonArray.length()>0){
试一试{
for(int i=0;iclient = new OkHttpClient.Builder()
    .connectTimeout(10, TimeUnit.SECONDS)
    .writeTimeout(10, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .build();