OkHttpClientUtil

1_OKHttp简介
1.1_简介
OKHttp是一款高效的HTTP客户端,支持连接同一地址的链接共享同一个socket,通过连接池来减小响应延迟,还有透明的GZIP压缩,请求缓存等优势,其核心主要有路由、连接协议、拦截器、代理、安全性认证、连接池以及网络适配,拦截器主要是指添加,移除或者转换请求或者回应的头部信息

这个库也是square开源的一个网络请求库(okhttp内部依赖okio)。现在已被Google使用在Android源码上了,可见其强大。

关于网络请求库,现在应该还有很多人在使用android-async-http。他内部使用的是HttpClient,但是Google在6.0版本里面删除了HttpClient相关API,可见这个库现在有点过时了。
1.2_下载地址
http://square.github.io/okhttp/
1.3_OKHttp主要功能
1、联网请求文本数据
2、大文件下载
3、大文件上传
4、请求图片

2_原生OKHttp的Get和Post请求思路
a.get请求步骤:

1)获取client对象

2)传入url获取request对象

3)获取response对象

4)利用response对象的body().string()方法获取返回的数据内容

b.post请求步骤:

1)获取client对象

2)传入url获取request对象,对比get方法,其还有在获取request对象的时候加多一个post方法用于传递客户端向服务器端发送的数据。

3)获取response对象

4)利用response对象的body().string()方法获取返回的数据内容

3、上代码

引入jar包

<dependency>
			<groupId>com.squareup.okhttp3</groupId>
			<artifactId>okhttp</artifactId>
			<version>3.10.0</version>
		</dependency>

写java类

package com.xxl.job.executor.usi.utils;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
 * okhttp请求
 * @author
 *
 */
public class OkHttpClientUtil {
	static OkHttpClient client = new OkHttpClient();
	/**
	 * post请求xml入参
	 * @param url
	 * @param data
	 * @return
	 * @throws IOException
	 */
	public static String doPostXml(String url,String data) throws IOException {
   	 MediaType mediaType = MediaType.parse("application/octet-stream");
   	 RequestBody body = RequestBody.create(mediaType, data);
   	 Request request = new Request.Builder()
   	   .url(url)
   	   .post(body)
   	   .build();
   	 Response response = client.newCall(request).execute();
	 return response.body().string();
	}
	/**
	 * post请求json入参
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public static String doPostJson(String url,String data)throws IOException{
		MediaType mediaType = MediaType.parse("application/json");
		RequestBody body = RequestBody.create(mediaType,data);
		Request request = new Request.Builder()
		  .url(url)
		  .post(body)
		  .addHeader("Content-Type", "application/json")
		  .build();
		Response response = client.newCall(request).execute();
		return response.body().string();
	}
	
	/**
     * get请求
     * @param url
     * @return
     */
    public static String httpGet(String url) {
    	Response response =null;
    	String result="";
        Request request = new Request.Builder().url(url).build();
        try {
            response = client.newCall(request).execute();
            result=response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值