I'm kinda having trouble understand how to make the best and map my JASON response from an ASP.NET Web API server into my Java classes.
I use this code to request the server data.
ClientResource res = new ClientResource(ADDRESS_URL + "/exed/students/");
Representation rep = res.get();
try {
Log.d(TAG, rep.getText());
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
I successfuly can retrieve this JSON representation.
{"$id":"1","StudentId":1,"RowVersion":"AAAAAAAAB9M=","User":{"$id":"2","UserId":1,"Username":"1","Password":"1","UserTypeId":1,"FirstName":"George","LastName":"Taskos","RowVersion":"AAAAAAAAB9I=","Student":{"$ref":"1"},"Teacher":null,"UserType":null}}
Now I have these Java classes.
The User.
public class User implements Serializable {
// Private fields
//
private int userId;
private String username;
private String password;
private int userTypeId;
private String firstName;
private String lastName;
private String userType;
private Timestamp rowVersion;
// Setters
// Getters....
}
And the Student.
public class Student extends User implements Serializable {
// Private fields
private List<ClassType> classes;
private List<ExamTaken> examsTaken;
private Timestamp rowVersionStudent;
}
How to retrieve the representation into my objects. I've seen so much and I think I'm so dizzy right now to get a solution. I'm not familiar with Java server side or anything so I guess thats why I get frustrated about stuff I read on the tutorials and I think lots of stuff missing from the site, or not clearly explained.
Any simple example at least on get the values out and get them into my object like I do with SQLite cursors. Deserialization from response would be really great too :)
[EDIT]
After Raw suggestion I ended up with this code.
ClientResource res = new ClientResource(ADDRESS_URL + "/exed/students/");
Representation rep = res.get();
try {
JSONObject response = new JSONObject(rep.getText());
Log.d(TAG, rep.getText());
JSONObject user = response.getJSONObject("User");
Log.d(TAG, "User: " + user.toString());
JSONObject student = response.getJSONObject("Student");
Log.d(TAG, "Student: " + student.toString());
Log.d(TAG, "FirstName: " + user.getString("FirstName"));
} catch (Exception e) {
Log.d(TAG, "Exception: " + e.getMessage());
}
But getting an Exception with this message: Exception: println needs a message
Stacktrace prints this: Exception: android.util.Log.println_native(Native Method)
and the unhandled exception is this:
03-06 13:07:14.668: ERROR/AndroidRuntime(25353): FATAL EXCEPTION: Thread-13908
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:155)
at com.tech.ed.data.access.WebServiceRepository$1.run(WebServiceRepository.java:74)
at java.lang.Thread.run(Thread.java:856)
I don't exactly understand what it means!
Thank you.
Looks like you just have a series of JSONObjects and strings. Just make one JSONObject for the entire results
JSONObject response = new JSONObject(representation);
then extract out the user and student objects
JSONObject user = response.getJSONObject("User");
JSONObject student = response.getJSONObject("Student");
Then you can get any nested strings, ints or whatever from the corresponding objects with something like
String firstName = user.getString("FirstName");