android企业级技术之第2讲okhttp.doc
文本预览下载声明
第2讲_OKHttp的使用
讲师:尚硅谷-杨光福
谷粉第46群:252915839
1_OKHttp简介
1.1_简介
OKHttp是一款高效的HTTP客户端,支持连接同一地址的链接共享同一个socket,通过连接池来减小响应延迟,还有透明的GZIP压缩,请求缓存等优势,其核心主要有路由、连接协议、拦截器、代理、安全性认证、连接池以及网络适配,拦截器主要是指添加,移除或者转换请求或者回应的头部信息这个库也是square开源的一个网络请求库(okhttp内部依赖okio)。现在已被Google使用在Android源码上了,可见其强大。关于网络请求库,现在应该还有很多人在使用android-async-http。他内部使用的是HttpClient,但是Google在6.0版本里面删除了HttpClient相关API,可见这个库现在有点过时了。http://square.github.io/okhttp/
1.3_OKHttp主要功能
1、联网请求文本数据2、大文件下载3、大文件上传4、请求图片/PageSubArea/TrailerList.api
?xml version=1.0 encoding=utf-8?LinearLayout xmlns:android=/apk/res/android xmlns:tools=/tools android:layout_width=match_parent android:layout_height=match_parent android:paddingBottom=@dimen/activity_vertical_margin android:paddingLeft=@dimen/activity_horizontal_margin android:paddingRight=@dimen/activity_horizontal_margin android:orientation=vertical android:paddingTop=@dimen/activity_vertical_margin tools:context=com.atguigu.okhttpsample.MainActivity Button android:id=@+id/btn_get android:layout_width=match_parent android:layout_height=wrap_content android:text=get请求 / Button android:id=@+id/btn_post android:layout_width=match_parent android:layout_height=wrap_content android:text=post请求 / TextView android:id=@+id/tv_result android:layout_width=match_parent android:layout_height=wrap_content android:text=显示请求数据 //LinearLayout
1.2_点击事件
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_get = (Button) findViewById(R.id.btn_get); btn_post = (Button) findViewById(R.id.btn_post); tv_result = (TextView) findViewById(R.id.tv_result); //设置点击事件 btn_get.setOnClickListener(this); btn_post.setOnClickListener(this);}@Overridepublic void onClick(View v) { switch (v.getId()){
显示全部