GET请求
private void okHttpget(String url) {
//创建client对象
OkHttpClient client = new OkHttpClient.Builder()
//设置连接超时时间
.callTimeout(5, TimeUnit.SECONDS)
//设置读取超时时间
.readTimeout(5, TimeUnit.SECONDS)
//创建OkHttpClient对象
.build();
//request对象
Request request = new Request.Builder()
//设置连接网址url
.url(url)
//设置连接方式
.get()
//创建request对象
.build();
//发起连接
Call call = client.newCall(request);
//获得响应体
call.enqueue(new Callback() {
//连接失败
@Override
public void onFailure(Call call, IOException e) {
}
//连接成功
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("##", "onResponse: "+response.body().string());
}
});
}
POST请求
private void okHttpPostRegister(String url,String name,String params) {
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(5, TimeUnit.SECONDS)
.callTimeout(5, TimeUnit.SECONDS)
.build();
//请求体RequsetBody的实现子类
FormBody formBody = new FormBody.Builder()
.add("phone", phone)
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("##", "onResponse: "+response.body().string());
}
});
}
简单封装的OkHttp工具类
OkHttpUtil类
import com.example.h0804_day03okhttp.model.OkHttpListener;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkHttpUtil {
private OkHttpClient client;
//只创建一个client 节省资源
private OkHttpUtil() {
client = new OkHttpClient.Builder()
.callTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.build();
}
//单例模式
private static OkHttpUtil instance;
public static OkHttpUtil getInstance() {
if (instance == null) {
synchronized (OkHttpUtil.class) {
if (instance == null) {
instance = new OkHttpUtil();
}
}
}
return instance;
}
/***
*
* @param url 请求的网址
* @param listener 接口回调获得结果
*/
public void okHttpGet(String url, final OkHttpListener listener) {
Request request = new Request.Builder()
.url(url)
.get()
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
listener.fail(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
listener.success(response.body().string());
}
});
}
/***
*
* @param url 请求的网址
* @param map 请求体的参数
* @param listener 接口回调获得结果
*/
public void okHttpPost(String url, Map map, final OkHttpListener listener) {
FormBody.Builder formBodyBuilder = new FormBody.Builder();
Set keySet = map.keySet();
for (String key : keySet) {
formBodyBuilder.add(key, map.get(key));
}
FormBody formBody = formBodyBuilder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
listener.fail(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
listener.success(response.body().string());
}
});
}
}
/***
*
* @param url 要下载文件的网址
* @param file 要保存的文件
* @param listener 接口回调获得结果
*/
public void okHttpDownload(String url, final String file, final OkHttpListener listener) {
Request request = new Request.Builder()
.url(url)
.get()
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
listener.fail(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//获得流
InputStream is = response.body().byteStream();
FileOutputStream fos = new FileOutputStream(file);
int length = 0;
byte[] bytes = new byte[1024 * 8];
//边读边写
while ((length = is.read(bytes)) != -1){
fos.write(bytes,0,length);
}
if (is != null) {
is.close();
}
if (fos != null) {
raf.close();
}
listener.success("下载成功");
}
});
}
/***
*
* @param url 服务器地址
* @param filePath 要上传文件的路径
* @param fileName 文件上传到服务器之后的名称
* @param type 文件类型
* @param listener 接口回调获得结果
*/
public void okHttpUpload(String url, String filePath, String fileName, String type, final OkHttpListener listener) {
MultipartBody multipartBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", fileName, RequestBody.create(MediaType.parse(type), new File(filePath)))
.build();
Request request = new Request.Builder()
.url(url)
.post(multipartBody)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
listener.fail(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
listener.success("上传成功");
}
});