0

i have the next situation. I want to obtain atributes from a User class , which contains atributes in the formats: Date, String, Name. Where Name is another class i created before which contains FirstName and LastName Strings. So, i have to do httprequest to my REST server and im doing this

protected static ArrayList<User> obtieneAmigos(int id) {
    // TODO Auto-generated method stub
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet del = new HttpGet("http://192.168.0.19/user/:"+id+"/friends/confirmed" );//Se obtienen los datos de la url del usuario
    del.setHeader("content-type", "application/json");
    ArrayList<User> friends =new ArrayList<User>();
    try {
        HttpResponse resp = httpClient.execute(del);
        String respStr = EntityUtils.toString(resp.getEntity());
        JSONArray respJSON = new JSONArray(respStr);
        for(int i=0; i<respJSON.length();i++){
            JSONObject objeto = respJSON.getJSONObject(i);
            User friend = new User();
            friend.setName(objeto.getString("FirstName"));//Error
            friend.setBirthDate();//I dont know how to parse it to string
            friends.add(friend);
        }

        return friends;


    } catch (Exception ex) {
        Log.e("ServicioRest", "Error!", ex);
    }
    return friends;
}

I need to know some things, first, how to obtain from JSON request in addition to Strings, another objects as my Name class. Second, how to parse or obtain the BirthDate as Date and no as String. And finally, another question. How is it parses Date to String or String to Date?

Clarification: The response comes formated as "user" with the atributes of this class. I want to do something as this: friend.setName(objeto.getName()); or friend.getName().setFirstName(objeto."getObject()."getString("FirstName")); where getObject is invented by me for this clarificatioin. Thanks.

Asier
  • 461
  • 9
  • 19
  • Better use Gson library. – virendrao Mar 02 '15 at 12:15
  • Thanks. With this library, will i can to obtain another objects? Is it totally compatible? – Asier Mar 02 '15 at 12:23
  • This is not i need. GSON library is for parse JSON code to JAVA or reverse, but i need obtain resources from the REST server. The response comes formated as "user" with the atributes of this class. I want to do something as this: friend.setName(objeto.getName()); or friend.getName().setFirstName(objeto."getObject()."getString("FirstName")); – Asier Mar 02 '15 at 13:35

1 Answers1

0

for parse date SimpleDateFormat
for rest api use retrofit + gson

Community
  • 1
  • 1
gadfil
  • 417
  • 4
  • 13
  • Aclaration: The response comes formated as "user" with the atributes of this class. I want to do something as this: friend.setName(objeto.getName()); or friend.getName().setFirstName(objeto."getObject()."getString("FirstName")); where getObject is invented by me for this aclaration. – Asier Mar 02 '15 at 13:38
  • sorry for my english. I'm not sure that I understood you correctly, can you show response from server? – gadfil Mar 02 '15 at 15:31