Using Google contact api in android application


 

For details about google contact api please refer Google’s developer guide.

To know about getting authToken please refer previous post.  https://capdroid.wordpress.com/2011/05/31/22/.

 

And here is the sample code.

 

            private void getContacts(String authToken) {
		HttpClient httpClient = new DefaultHttpClient();
		try {
			int startIndex = 1;
			byte[] byteArray = new byte[2048];
			
			HttpGet httpGet = new HttpGet(
					"https://www.google.com/m8/feeds/contacts/default/thin?alt=json&max-results="
							+ 999999 + "&start-index=" + startIndex);
			httpGet.setHeader("Authorization", "GoogleLogin auth="+ authToken);
			HttpResponse response = httpClient.execute(httpGet);
			httpGet = null;

			InputStream inputStream = response.getEntity().getContent();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			int len = -1;
			while (-1 != (len = inputStream.read(byteArray))) {
				baos.write(byteArray, 0, len);
			}
			inputStream.close();
			response = null;
			JSONObject jInObject = new JSONObject(baos.toString());
			baos.close();
			baos = null;
			JSONArray entries = jInObject.getJSONObject("feed")
					.getJSONArray("entry");
			for (int i = 0; i < entries.length(); i++) {
				startIndex++;
				try {
                                               System.out.println(
                                                       entries.getJSONObject(i)
							.getJSONArray("gd$email").getJSONObject(0).get("address") + ":" + entries.getJSONObject(i)
							.getJSONObject("title").get("$t"));
				} catch (JSONException e) {
				}
			}
			jInObject = null;
	        } catch (Exception e) {
	        }
        }

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s