Simple JSON parsing using GSON throwing IllegalArgumentException

What I want is this:

1)send a GET request to a WordPress site

Read More

2) Get the json response from the site

3) from that response (which I cannot know how or what elements it contains but I know for sure it contains at least one that interests me. That being “content”) get one element of the JSON and throw the rest.

I really cannot get this working. I have posted the simple code that I understand that is needed to get this working but I get a IllegalArgumentException. Clearly I have misunderstood something in the library

        URL hotelPresentationURL = new URL(
                "http://rodosseavillas.gr/wp-json/posts/206");
        HttpURLConnection conn = (HttpURLConnection) hotelPresentationURL
                .openConnection();
        conn.connect();
        InputStream reader = conn.getInputStream();// optimize

        JsonReader jsonReader = new JsonReader(
                new InputStreamReader(reader));
        jsonReader.setLenient(true);
        jsonReader.beginObject();


        JsonObject response = new Gson().fromJson(jsonReader,
                JsonObject.class);

        JsonElement element = response.get("content");


        jsonReader.close();
        Log.d(GuideFragment.TAG, "JSON RECEIVED: " + element.toString());




05-29 17:29:15.868: E/AndroidRuntime(2636): java.lang.RuntimeException: An error occured while executing doInBackground()
05-29 17:29:15.868: E/AndroidRuntime(2636):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at java.lang.Thread.run(Thread.java:841)
05-29 17:29:15.868: E/AndroidRuntime(2636): Caused by: java.lang.IllegalArgumentException
05-29 17:29:15.868: E/AndroidRuntime(2636):     at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:676)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:642)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at com.google.gson.Gson.fromJson(Gson.java:810)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at com.hellobusiness.fragments.ContentFetcherGuide.doInBackground(GuideFragment.java:61)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at com.hellobusiness.fragments.ContentFetcherGuide.doInBackground(GuideFragment.java:1)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-29 17:29:15.868: E/AndroidRuntime(2636):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-29 17:29:15.868: E/AndroidRuntime(2636):     ... 4 more

Related posts

1 comment

  1. here what i believe is your mistake, what you should be doing is that instead of using JsonObject you should be using proper dto, like this …

    package com.example.helppeople.dto;
    
    public class Author {
    
        private Integer ID;
        private String username;
        private String name;
        private String firstName;
        private String lastName;
        private String nickname;
        private String slug;
        private String URL;
        private String avatar;
        private String description;
        private String registered;
        private Meta meta;
        public Author(Integer iD, String username, String name,
                String firstName, String lastName, String nickname,
                String slug, String uRL, String avatar, String description,
                String registered, Meta meta) {
            super();
            ID = iD;
            this.username = username;
            this.name = name;
            this.firstName = firstName;
            this.lastName = lastName;
            this.nickname = nickname;
            this.slug = slug;
            URL = uRL;
            this.avatar = avatar;
            this.description = description;
            this.registered = registered;
            this.meta = meta;
        }
        public Integer getID() {
            return ID;
        }
        public void setID(Integer iD) {
            ID = iD;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getFirstName() {
            return firstName;
        }
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
        public String getLastName() {
            return lastName;
        }
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
        public String getNickname() {
            return nickname;
        }
        public void setNickname(String nickname) {
            this.nickname = nickname;
        }
        public String getSlug() {
            return slug;
        }
        public void setSlug(String slug) {
            this.slug = slug;
        }
        public String getURL() {
            return URL;
        }
        public void setURL(String uRL) {
            URL = uRL;
        }
        public String getAvatar() {
            return avatar;
        }
        public void setAvatar(String avatar) {
            this.avatar = avatar;
        }
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
        public String getRegistered() {
            return registered;
        }
        public void setRegistered(String registered) {
            this.registered = registered;
        }
        public Meta getMeta() {
            return meta;
        }
        public void setMeta(Meta meta) {
            this.meta = meta;
        }
        @Override
        public String toString() {
            return "Author [ID=" + ID + ", username=" + username + ", name="
                    + name + ", firstName=" + firstName + ", lastName="
                    + lastName + ", nickname=" + nickname + ", slug=" + slug
                    + ", URL=" + URL + ", avatar=" + avatar + ", description="
                    + description + ", registered=" + registered + ", meta="
                    + meta + "]";
            }
    
    
    }
    
    
    package com.example.helppeople.dto;
    
    public class Meta {
     private Links links;
    
    public Meta(Links links) {
        super();
        this.links = links;
    }
    
    public Links getLinks() {
        return links;
    }
    
    public void setLinks(Links links) {
        this.links = links;
    }
    
    @Override
    public String toString() {
        return "Meta [links=" + links + "]";
        }
    
    }
    
    package com.example.helppeople.dto;
    
    public class Links {
    private String self;
    private String archives;
    
    
    
    public Links(String self, String archives) {
        super();
        this.self = self;
        this.archives = archives;
    }
    
    public String getSelf() {
        return self;
    }
    
    public void setSelf(String self) {
        this.self = self;
    }
    
    public String getArchives() {
        return archives;
    }
    
    public void setArchives(String archives) {
        this.archives = archives;
    }
    
    @Override
    public String toString() {
        return "Links [self=" + self + ", archives=" + archives + "]";
    }
    
    
    
    }
    

    you can use the following site to generate the dto stucture pojo, this is a very simple examples of how you should be using GSON example 1, example 2, example 3

Comments are closed.