【问题标题】:Programmatically Fetching Google+ status updates以编程方式获取 Google+ 状态更新
【发布时间】:2014-05-09 17:12:02
【问题描述】:

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

【问题讨论】:

    标签: java android google-plus


    【解决方案1】:

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

    访问API时,请use the Google API client as documented here

    【讨论】:

    • 谢谢!正是我想要的!
    【解决方案2】:

    以下代码可用于检索 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;
        }
    }
    

    【讨论】:

    • 感谢您的回复!网上有没有任何官方文档可以做到这一点?
    猜你喜欢
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多