How do I correctly use generics?

Posted by ninjasense on Stack Overflow See other posts from Stack Overflow or by ninjasense
Published on 2010-12-28T06:43:34Z Indexed on 2010/12/28 8:54 UTC
Read the original article Hit count: 227

Filed under:
|
|
|

I basically am making webrequests and recieving a JSON response. Depending on the request, I am parsing JSON request into objects I have created. The parsing is pretty much the same no matter what the object Im parsing into looks like. So I have a bunch of methods doing the same work only with different objects, I was wondering how I could accomplish this with generics? Here is an example

   public static ArrayList<Contact> parseContacts(String responseData) {
ArrayList<Contact> Contacts = new ArrayList<Contact>();
try {

    JSONArray jsonContacts = new JSONArray(responseData);
    if (!jsonContacts.isNull(0)) {
        for (int i = 0; i < jsonContacts.length(); i++) {
            Contacts.add(new Contact(jsonContacts.getJSONObject(i)));
        }

    }

} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {

}

return Contacts;

}

© Stack Overflow or respective owner

Related posts about java

Related posts about android