johns4ta
johns4ta

Reputation: 896

Programmatically Fetching Google+ status updates

Is there a way to programmatically fetch Google+ updates for a user's profile? I can't seem to find much in the documentation at https://developers.google.com/+/api/latest/people and http://developer.android.com/reference/com/google/android/gms/plus/model/people/Person.html about fetching statuses. I would like to fetch the data by making an HTTP request or if there is some sort of SDK for Android that will help me, that would work to.

Upvotes: 0

Views: 181

Answers (2)

class
class

Reputation: 8681

The API you are looking for is plus.activities.list. This will list the Google+ equivalent of Facebook status updates. The referenced page has example code to get you started.

When accessing the API, you should use the Google API client as documented here.

Upvotes: 1

ravikumar
ravikumar

Reputation: 893

The following code will be useful to retrieve the Http responses.

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import java.util.zip.GZIPInputStream;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GooglePlusStatusHelper {

    public GooglePlusStatusHelper() {
    }


    public static void main(String... args) {

        GooglePlusStatusHelper googlePlusStatusHelper = new GooglePlusStatusHelper();
        try {
            googlePlusStatusHelper.tagsUsed();
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }


    private void tagsUsed() throws IOException {
        URL url = createQuery("users");
        Type dataType = new TypeToken<Wrapper<Status>>(){}.getType();
        Status status = executeQuery(url, dataType);

        System.out.println(status);
    }

    private URL createQuery(String inputParam) throws MalformedURLException {
        String baseUrl = "http://api.example.com/" + inputParam ;
        System.out.println(baseUrl);
        URL url = new URL(baseUrl);
        return url;
    }

    private Status executeQuery(URL url, Type clz) throws IOException {

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.connect();
        System.out.println("Response Code:" + conn.getResponseCode());
        System.out.println("Response Message:" + conn.getResponseMessage());
        System.out.println("TYPE:" + conn.getContentType());

        InputStream content = conn.getInputStream();
        String encoding = conn.getContentEncoding();
        if (encoding != null && encoding.equals("gzip")) {
            content = new GZIPInputStream(content);
        }
        String result = new Scanner(content, "UTF-8").useDelimiter("\\A").next();
        content.close();

        Gson gson = new Gson();
        return gson.fromJson(result, clz);
    }

}

Status class :

 public class Status {

    private int count;
    private String status;
    ......

    public String toString() {
        String result = "\ncount: " + count +
                "\status:" + status;

        result = result + "\n------------";
        return result;
    }
}

Upvotes: 1

Related Questions