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?
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()
{
}
.
.
.
}
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;
};
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.