Skip to content Skip to sidebar Skip to footer

Why Is JSON Document Not Fully Consumed?

I'm trying to retrieve JSON data from an external source for practice. I have gotten all the code in place but for some reason I get an error saying that the document is not fully

Solution 1:

The value is being pass, but however null is also getting passed with it so use an if statement rather than using while like this..

if (myJSON != null) {
                myJSON = bufferedReaderObject.readLine();
                completeJSONdata += myJSON;
            }

then convert in Gson like this..

    Gson gson = new Gson();
    deserializedContainerObject = gson.fromJson(completeJSONdata, DeserializedContainer.class);

write the getters and setters in DeserializedVariables class

public String getMovieName() {
         return movieName;
     }

     public void setMovieName(String movieName) {
         this.movieName = movieName;
     }

     public Integer getMovieYear() {
         return movieYear;
     }

     public void setMovieYear(Integer movieYear) {
         this.movieYear = movieYear;
     }

     public Double getMovieRating() {
         return movieRating;
     }

     public void setMovieRating(Double movieRating) {
         this.movieRating = movieRating;
     }

And now you can retrieve it in your onPostExecute() like this..

@Override
    protected void onPostExecute( DeserializedContainer result) {

        mListener.onSuccess( result );

        for (int i = 0; i <result.deserializedContainerList.size(); i++) {
            DeserializedVariables deserializedVariables = result.deserializedContainerList.get(i);
            Log.d( TAG, "onPostExecuss: " + deserializedVariables.getMovieName() );
        }

    }

Solution 2:

Your completeJSONdata is incorrect as you are always putting "null" towards the end.

Instead your while clause should be for example :

        while ((myJSON = bufferedReaderObject.readLine()) != null) {
            completeJSONdata += myJSON;
        }

By the way, don't forget to close your streams for instance by using a try with :

try (InputStream inputStreamObject = httpURLConnection.getInputStream()) {
// ...
}

Post a Comment for "Why Is JSON Document Not Fully Consumed?"