Android Open Source - ContactsManager Rest Contact Repository






From Project

Back to project page ContactsManager.

License

The source code is released under:

Apache License

If you think the Android project ContactsManager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package silverhillapps.com.contactsmanager.rest;
//from  w  w  w  . j a v a 2s.c om
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

import silverhillapps.com.contactsmanager.conf.Constants;
import silverhillapps.com.contactsmanager.loader.ContactRepository;
import silverhillapps.com.contactsmanager.loader.RepositoryReceiver;
import silverhillapps.com.contactsmanager.manager.ManagerInterface;
import silverhillapps.com.contactsmanager.model.Contact;

/**
 * This is the rest loader. it generates the http calls to rest api for any of the methods needed in the app.
 * It uses http-request library.
 * @author salva
 *
 */
public class RestContactRepository extends ContactRepository{

    private Gson g;

    /**
     * This method obtains the contact list through the rest endpoint
     * @param receiver
     * @param manager
     */
    @Override
    public void getContactList(final RepositoryReceiver receiver, final ManagerInterface manager) {

          ContactsRestClient.get(Constants.RestApiConstants.CONTACTS_LIST_METHOD, null,
                  new AsyncHttpResponseHandler() {
                      @Override
                      public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                          String str = new String(responseBody);

                          List<Contact> contactList = parseJsonContacts(str);

                          if(manager!=null){ //we can run the rest client without needing an intermediate manager
                              manager.getResults(contactList, Constants.PassingParamConstants.REST_RETURN_LIST_CONTACTS);
                          }

                          receiver.getResults(contactList, Constants.PassingParamConstants.REST_RETURN_LIST_CONTACTS);
                      }

                      @Override
                      public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                          //TODO manage the error in the connection
                      }

                  }
          );
          }

    /**
     * This method parses the http response in order to create the list of model objects.
     * @param response
     * @return
     */
    private List<Contact> parseJsonContacts(String response) {
        g = new Gson(); //TODO dagger - could be injected by dagger also.
        List<Contact> contactList = new ArrayList<Contact>();
        contactList = g.fromJson(response, new TypeToken<List<Contact>>(){}.getType());
        return contactList;
    }
}




Java Source Code List

silverhillapps.com.contactsmanager.ApplicationTest.java
silverhillapps.com.contactsmanager.CMApplication.java
silverhillapps.com.contactsmanager.ContactDetailActivity.java
silverhillapps.com.contactsmanager.ContactListActivity.java
silverhillapps.com.contactsmanager.adapter.ContactListAdapter.java
silverhillapps.com.contactsmanager.conf.Constants.java
silverhillapps.com.contactsmanager.db.DatabaseHandler.java
silverhillapps.com.contactsmanager.loader.ContactRepositoryModule.java
silverhillapps.com.contactsmanager.loader.ContactRepository.java
silverhillapps.com.contactsmanager.loader.RepositoryReceiver.java
silverhillapps.com.contactsmanager.manager.LoaderManagerFacade.java
silverhillapps.com.contactsmanager.manager.ManagerInterface.java
silverhillapps.com.contactsmanager.manager.ManagerModule.java
silverhillapps.com.contactsmanager.model.Contact.java
silverhillapps.com.contactsmanager.rest.ContactsRestClient.java
silverhillapps.com.contactsmanager.rest.RestContactRepository.java
silverhillapps.com.contactsmanager.utils.ConnectionUtils.java