OKHttp的基本使用
来源:互联网 发布:液压自动车床编程 编辑:程序博客网 时间:2024/05/17 02:45
1)Android中网络请求的变化
HttpURLConnection -> HTTPClient -> Volley -> OKHttp
2)OKHttp是什么?
HttpURLConnection 和 HTTPClient 虽然都支持HTTPS、文件上传下载、配置超时、IPV6、连接池。但使用OKHttp可更高效的使用HTTP协议,使应用运行的更快,更节省流量。
OKHttp是一个高效的HTTP库,它的特点为:
-> 支持SPDY。共享同一个Socket来处理同一个服务器的所有请求
-> 可通过连接池减少请求延时
-> 支持GZIP来减少数据流量
-> 缓存相应数据来减少重复的网络请求
-> 可以解决常见的网络连接问题
-> 处理代理服务器问题和SSL握手问题
3)基本使用
-> GET请求
//创建okHttpClient对象
OkHttpClient mOkHttpClient = new OkHttpClient();
//创建一个Request
final Request request = new Request.Builder() .url("https://github.com/hongyangAndroid")
.build();
//new call
Call call = mOkHttpClient.newCall(request);
//请求加入调度
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(final Response response) throws IOException {
//String htmlStr = response.body().string();
}
});
-> POST请求
Request request = buildMultipartFormRequest( url, new File[]{file}, new String[]{fileKey}, null);
FormEncodingBuilder builder = new FormEncodingBuilder(); builder.add("username","张鸿洋");
Request request = new Request.Builder()
.url(url)
.post(builder.build())
.build();
mOkHttpClient.newCall(request).enqueue(new Callback(){});
-> 基于Http的文件上传
File file = new File(Environment.getExternalStorageDirectory(), "balabala.mp4");
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addPart(Headers.of( "Content-Disposition", "form-data; name=\"username\"")
, RequestBody.create(null, "张鸿洋"))
.addPart(Headers.of( "Content-Disposition", "form-data; name=\"mFile\";
filename=\"wjd.mp4\""), fileBody)
.build();
Request request = new Request.Builder()
.url("http://192.168.1.103:8080/okHttpServer/fileUpload")
.post(requestBody)
.build();
Call call = mOkHttpClient.newCall(request); call.enqueue(new Callback() { //... });
onResponse回调的参数是response,一般情况下,比如我们希望获得返回的字符串,可以通过response.body().string()获取;如果希望获得返回的二进制字节数组,则调用response.body().bytes();如果你想拿到返回的inputStream,则调用response.body().byteStream()
看到这,你可能会奇怪,竟然还能拿到返回的inputStream,看到这个最起码能意识到一点,这里支持大文件下载,有inputStream我们就可以通过IO的方式写文件。不过也说明一个问题,这个onResponse执行的线程并不是UI线程。的确是的,如果你希望操作控件,还是需要使用handler等,
4)利用OKHttp来请求图片,并将请求到的图片加载到广告轮播组件中,实现步骤为:
①添加依赖
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'com.squareup.okio:okio:1.5.0'
②建立请求
String url ="http://112.124.22.238:8081/course_api/banner/query?type=1";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
③将请求入队
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
if(response.isSuccessful()){
String json = response.body().string();
try{
JSONArray array = new JSONArray(json);
for(int i = 0; i<array.length(); i++){
JSONObject jsonObject = array.getJSONObject(i);
Banner banner = new Banner();
banner.setName(jsonObject.getString("name"));
banner.setImgUrl(jsonObject.getString("imgUrl"));
banner.setDescription(jsonObject.getString("description"));
banners.add(banner);
}
}catch (Exception e){
e.printStackTrace();
}
//进行相应的操作,但这里是异步的
initSlider();
}
}
});
- OKHttp的基本使用
- OkHttp的基本使用
- OkHttp的基本使用
- 1、Retrofit+Okhttp的基本使用
- OkHttp的基本使用(一)Get请求
- OKHttp的基本使用和简单封装
- OkHttp的基本使用以及二次封装
- okhttp的二次封装(基本使用)
- okhttp基本使用
- OkHttp基本使用方式
- Retrofit+okhttp基本使用
- OkHttp基本使用
- OkHttp基本使用
- OkHttp基本使用
- okHttp基本使用
- Retrofit+Rxjava+okhttp基本使用
- OkHttp的基本使用——替代Apache HttpClient
- OkHttp的基本使用(二)Post请求
- 自己的项目部署到服务器上的部分问题解决办法
- 练习4——图论总结
- NodeJs——(7)request的结构
- 实战Docker到Kubernetes技术系列视频教程
- ORACLE-023:令人烦恼的 ora-01722 无效数字
- OKHttp的基本使用
- gdb加载当前目录下.gdbinit不成功
- 网络协议系列之一:HTTP(1)
- GPIO模拟I2C
- 获取tableview上面所有的cell
- PhysicsEditor downlooad link
- Java中只有值传递,没有引用传递 !
- NodeJs——(8)http.ServerRequest的过程
- LRU Caching