Android Json Parsing with Gson

两盒软妹~` 提交于 2019-12-13 15:05:12

问题


I am tring to map json data using Gson from a URL to my java class. This is the code for the class:

public class Sessions {
public Boolean active;
public String contributor_covu_id;
public String created_at;
public String key;
public String status;

public Boolean getActive() {
    return active;
}
public void setActive(Boolean active) {
    this.active = active;
}
public String getContributor_covu_id() {
    return contributor_covu_id;
}
public void setContributor_covu_id(String contributor_covu_id) {
    this.contributor_covu_id = contributor_covu_id;
}
public String getCreated_at() {
    return created_at;
}
public void setCreated_at(String created_at) {
    this.created_at = created_at;
}
public String getKey() {
    return key;
}
public void setKey(String key) {
    this.key = key;
}
public String getStatus() {
    return status;
}
public void setStatus(String status) {
    this.status = status;
}
public String getType() {
    return type;
}
public void setType(String type) {
    this.type = type;
}
public String type;

}

and this is the code of the class that calls the service and maps json

public static List<Sessions> getSessions(String urlString)
        throws IOException {

    Sessions[] sessions;
    List<Sessions> tempList = null;
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    GsonBuilder gsonb = new GsonBuilder();
    Gson gson = gsonb.create();
    StringBuilder sb = new StringBuilder();
    String line;

    // Response response = null;
    JSONObject j = null;

    if (conn.getResponseCode() != 200) {
        throw new IOException(conn.getResponseMessage());
    }

    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));

    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line);
    }
    bufferedReader.close();
    conn.disconnect();

    try {
        j = new JSONObject(sb.toString());
        sessions = gson.fromJson(j.toString(), Sessions[].class);
        tempList = Arrays.asList(sessions);

        // response = gson.fromJson(j.toString(), Response.class);
        // tempList.add(response);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return tempList;
}

Although the code is correct, I get a Gson exception "Unable to invoke no-args constructor for class com.test.Sessions;. Register an InstanceCreator with Gson for this type may fix this problem"

How do i solve this issue and return the Gson as an ArrayList?


回答1:


Add a constructor to your Sessions class:

public class Sessions {
    public Boolean active;
    public String contributor_covu_id;
    public String created_at;
    public String key;
    public String status;

    void Sessions()
    {
    }
    .
    .
    .
}



回答2:


The solution is:

public class Sessions {

private String status;
private List<Session> sessions;

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public List<Session> getSessions() {
    return sessions;
}

public void setSessions(List<Session> sessions) {
    this.sessions = sessions;
}

public static class Session {
    public Boolean active;
    public String contributor_covu_id;
    public String created_at;
    public String key;
    public String status;
    public String name;

};

}



来源:https://stackoverflow.com/questions/6371764/android-json-parsing-with-gson

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!