以编程方式获取Google+状态更新

时间:2014-05-09 17:12:03

标签: java android google-plus

有没有办法以编程方式获取用户个人资料的Google+更新?我在https://developers.google.com/+/api/latest/peoplehttp://developer.android.com/reference/com/google/android/gms/plus/model/people/Person.html的文档中似乎找不到有关获取状态的信息。我想通过发出HTTP请求来获取数据,或者如果有某种类型的Android SDK可以帮助我,那就可以了。

2 个答案:

答案 0 :(得分:1)

以下代码可用于检索Http响应。

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);
    }

}

状态类:

 public class Status {

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

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

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

答案 1 :(得分:1)

您要查找的API是plus.activities.list。这将列出相当于Facebook状态更新的Google+。引用页面包含示例代码,可帮助您入门。

访问API时,您应该use the Google API client as documented here

相关问题