google-http-java-client(android学习篇2源码)
| package com.google.api.services.samples.googleplus.cmdline.simple; |
| import com.google.api.client.http.GenericUrl; |
| import com.google.api.client.http.HttpRequest; |
| import com.google.api.client.http.HttpRequestFactory; |
| import com.google.api.client.http.HttpRequestInitializer; |
| import com.google.api.client.http.HttpResponse; |
| import com.google.api.client.http.HttpResponseException; |
| import com.google.api.client.http.HttpTransport; |
| import com.google.api.client.http.javanet.NetHttpTransport; |
| import com.google.api.client.json.GenericJson; |
| import com.google.api.client.json.JsonFactory; |
| import com.google.api.client.json.JsonObjectParser; |
| import com.google.api.client.json.jackson2.JacksonFactory; |
| import com.google.api.client.util.Key; |
| import java.io.IOException; |
| import java.util.List; |
| /** |
| * Simple example that demonstrates how to use <a |
| * href="code.google.com/p/google-http-java-client/">Google HTTP Client Library for Java</a> with |
| * the <a href="https://developers.google.com/+/api/">Google+ API</a>. |
| * |
| * <p> |
| * Note that in the case of the Google+ API, there is a much better custom library built on top of |
| * this HTTP library that is much easier to use and hides most of these details for you. See <a |
| * href="http://code.google.com/p/google-api-java-client/wiki/APIs#Google+_API">Google+ API for |
| * Java</a>. |
| * </p> |
| * |
| * @author Yaniv Inbar |
| */ |
| publicclassGooglePlusSample{ |
| privatestaticfinalString API_KEY = |
| "Enter API Key from https://code.google.com/apis/console/?api=plus into API_KEY"; |
| privatestaticfinalString USER_ID ="116899029375914044550"; |
| privatestaticfinalint MAX_RESULTS =3; |
| staticfinalHttpTransport HTTP_TRANSPORT =newNetHttpTransport(); |
| staticfinalJsonFactory JSON_FACTORY =newJacksonFactory(); |
| /** Feed of Google+ activities. */ |
| publicstaticclassActivityFeed{ |
| /** List of Google+ activities. */ |
| @Key("items") |
| privateList<Activity> activities; |
| publicList<Activity> getActivities(){ |
| return activities; |
| } |
| } |
| /** Google+ activity. */ |
| publicstaticclassActivityextendsGenericJson{ |
| /** Activity URL. */ |
| @Key |
| privateString url; |
| publicString getUrl(){ |
| return url; |
| } |
| /** Activity object. */ |
| @Key("object") |
| privateActivityObject activityObject; |
| publicActivityObject getActivityObject(){ |
| return activityObject; |
| } |
| } |
| /** Google+ activity object. */ |
| publicstaticclassActivityObject{ |
| /** HTML-formatted content. */ |
| @Key |
| privateString content; |
| publicString getContent(){ |
| return content; |
| } |
| /** People who +1'd this activity. */ |
| @Key |
| privatePlusOners plusoners; |
| publicPlusOners getPlusOners(){ |
| return plusoners; |
| } |
| } |
| /** People who +1'd an activity. */ |
| publicstaticclassPlusOners{ |
| /** Total number of people who +1'd this activity. */ |
| @Key |
| privatelong totalItems; |
| publiclong getTotalItems(){ |
| return totalItems; |
| } |
| } |
| /** Google+ URL. */ |
| publicstaticclassPlusUrlextendsGenericUrl{ |
| publicPlusUrl(String encodedUrl){ |
| super(encodedUrl); |
| } |
| @SuppressWarnings("unused") |
| @Key |
| privatefinalString key = API_KEY; |
| /** Maximum number of results. */ |
| @Key |
| privateint maxResults; |
| publicint getMaxResults(){ |
| return maxResults; |
| } |
| publicPlusUrl setMaxResults(int maxResults){ |
| this.maxResults = maxResults; |
| returnthis; |
| } |
| /** Lists the public activities for the given Google+ user ID. */ |
| publicstaticPlusUrl listPublicActivities(String userId){ |
| returnnewPlusUrl( |
| "https://www.googleapis.com/plus/v1/people/"+ userId +"/activities/public"); |
| } |
| } |
| privatestaticvoid parseResponse(HttpResponse response)throwsIOException{ |
| ActivityFeed feed = response.parseAs(ActivityFeed.class); |
| if(feed.getActivities().isEmpty()){ |
| System.out.println("No activities found."); |
| }else{ |
| if(feed.getActivities().size()== MAX_RESULTS){ |
| System.out.print("First "); |
| } |
| System.out.println(feed.getActivities().size()+" activities found:"); |
| for(Activity activity : feed.getActivities()){ |
| System.out.println(); |
| System.out.println("-----------------------------------------------"); |
| System.out.println("HTML Content: "+ activity.getActivityObject().getContent()); |
| System.out.println("+1's: "+ activity.getActivityObject().getPlusOners().getTotalItems()); |
| System.out.println("URL: "+ activity.getUrl()); |
| System.out.println("ID: "+ activity.get("id")); |
| } |
| } |
| } |
| privatestaticvoid run()throwsIOException{ |
| HttpRequestFactory requestFactory = |
| HTTP_TRANSPORT.createRequestFactory(newHttpRequestInitializer(){ |
| @Override |
| publicvoid initialize(HttpRequest request){ |
| request.setParser(newJsonObjectParser(JSON_FACTORY)); |
| } |
| }); |
| PlusUrl url =PlusUrl.listPublicActivities(USER_ID).setMaxResults(MAX_RESULTS); |
| url.put("fields","items(id,url,object(content,plusoners/totalItems))"); |
| HttpRequest request = requestFactory.buildGetRequest(url); |
| parseResponse(request.execute()); |
| } |
| publicstaticvoid main(String[] args){ |
| if(API_KEY.startsWith("Enter ")){ |
| System.err.println(API_KEY); |
| System.exit(1); |
| } |
| try{ |
| try{ |
| run(); |
| return; |
| }catch(HttpResponseException e){ |
| System.err.println(e.getMessage()); |
| } |
| }catch(Throwable t){ |
| t.printStackTrace(); |
| } |
| System.exit(1); |
| } |
| } |
google-http-java-client(android学习篇2源码)的更多相关文章
- Netty学习篇⑥--ByteBuf源码分析
什么是ByteBuf? ByteBuf在Netty中充当着非常重要的角色:它是在数据传输中负责装载字节数据的一个容器;其内部结构和数组类似,初始化默认长度为256,默认最大长度为Integer.MAX ...
- Android学习笔记——从源码看Handler的处理机制
可能是出于性能的考虑,Android的UI操作是非线程安全的. 也就是说,如果你在一个新开的线程中直接操作UI是会引发异常的. 但是,Android又规定,不要去阻塞UI线程!否则,轻者引起程序卡顿, ...
- ESA2GJK1DH1K基础篇: Android实现MQTT封装源码使用说明
说明 这一节说明一下基础篇APP源码里面MyMqttCilent.java这个文件的使用 新建工程 安装MQTT的jar包 implementation 'org.eclipse.paho:org.e ...
- [Android FrameWork 6.0源码学习] View的重绘过程之WindowManager的addView方法
博客首页:http://www.cnblogs.com/kezhuang/p/关于Activity的contentView的构建过程,我在我的博客中已经分析过了,不了解的可以去看一下<[Andr ...
- Java中常用的七个阻塞队列第二篇DelayQueue源码介绍
Java中常用的七个阻塞队列第二篇DelayQueue源码介绍 通过前面两篇文章,我们对队列有了了解及已经认识了常用阻塞队列中的三个了.本篇我们继续介绍剩下的几个队列. 本文主要内容:通过源码学习De ...
- Android -- 带你从源码角度领悟Dagger2入门到放弃(二)
1,接着我们上一篇继续介绍,在上一篇我们介绍了简单的@Inject和@Component的结合使用,现在我们继续以老师和学生的例子,我们知道学生上课的时候都会有书籍来辅助听课,先来看看我们之前的Stu ...
- android 近百个源码项目【转】
http://www.cnblogs.com/helloandroid/articles/2385358.html Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多 ...
- Android -- 带你从源码角度领悟Dagger2入门到放弃
1,以前的博客也写了两篇关于Dagger2,但是感觉自己使用的时候还是云里雾里的,更不谈各位来看博客的同学了,所以今天打算和大家再一次的入坑试试,最后一次了,保证最后一次了. 2,接入项目 在项目的G ...
- Android -- 带你从源码角度领悟Dagger2入门到放弃(一)
1,以前的博客也写了两篇关于Dagger2,但是感觉自己使用的时候还是云里雾里的,更不谈各位来看博客的同学了,所以今天打算和大家再一次的入坑试试,最后一次了,保证最后一次了. 2,接入项目 在项目的G ...
随机推荐
- MonoRail学习-入门实例篇
1.到官方网站下载安装文件,地址如下: http://www.castleproject.org/index.php/Castle:Download目前最新版本Beta5(您也可以不需要下载,直接使用 ...
- 推荐一款好用轻便的在线UML画图工具
刚接触UML时间不长,看了N多教学视频,下载好了几个软件各种不习惯 当我遇见了ProcessOn 从此我彻底“爱上”了它! http://www.processon.com/ UML各类例图它几乎全 ...
- C语言实例代码
绘制余弦曲线和直线 #include #include int main() { double y; int x,m,n,yy; for(yy=0;yy<=20;yy++) {y=0.1*yy; ...
- OpenCV之响应鼠标(四):在图像上绘制出矩形并标出起点的坐标
涉及到两方面的内容:1. 用鼠标画出矩形.2.在图像上绘制出点的坐标 用鼠标绘制矩形,涉及到鼠标的操作,opencv中有鼠标事件的介绍.需要用到两个函数:回调函数CvMouseCallback和注册回 ...
- APP store 上架过程中碰到的那些坑&被拒的各种奇葩原因整理&审核指南中文版
苹果官方发布的十大常见被拒原因 1.崩溃次数和Bug数量.苹果要求开发者在将应用提交给App Store之前彻查自己的应用,以尽量避免Bug的存在. 2.链或错误的链接.应用中所有的链接必须是真实且有 ...
- ubuntu16.04操作练习&问题解决
1. 安装更新时提示/boot空间不足: boot文件夹里存放的是系统引导文件和内核的一些东西,旧内核的东西需要手动删除,释放空间.所以: step1:查看 dpkg --get-selections ...
- 2016 - 1- 19 利用多线程优化从网上加载图片的Demo
// // ZZTableViewController.m // 多图片下载 // // Created by Mac on 16/1/19. // Copyright © 2016年 Mac. Al ...
- SmartZoneOCR识别控件免费下载地址
SmartZone™光学字符识别工具包,使开发人员能够进行带状区域光学字符识别,带状区域在表单处理应用程序中经常使用.本产品所包含的.NET控件以及ActiveX COM组件在内部使用两种单独的识别技 ...
- C#操作txt问件,进行清空添加操作
//把txt清空 FileStream stream = File.Open(Adr,FileMode.OpenOrCreate,FileAccess.Write); stream.Seek(, Se ...
- 数据结构 《5》----二叉搜索树 ( Binary Search Tree )
二叉树的一个重要应用就是查找. 二叉搜索树 满足如下的性质: 左子树的关键字 < 节点的关键字 < 右子树的关键字 1. Find(x) 有了上述的性质后,我们就可以像二分查找那样查找给定 ...