okhttp GitHub地址:
https://github.com/square/okhttp
最新的OkHttp依赖可以到官方的GitHub上去添加。
目前官方的依赖地址为:
compile ‘com.squareup.okhttp3:okhttp:3.6.0’
直接进入主题:
// 1、拿到OKHttpClient对象
OkHttpClient okHttpClient=new OkHttpClient();
// 2、构造Request
//Request是构造者模式,所以使用的时候要.Builder
Request.Builder builder=new Request.Builder();
Request reqyest=builder.get().url("http://www.baidu.com").build();
// 3、将Request封装为Call
Call call=okHttpClient.newCall(reqyest);
// 4、执行Call
/* Response response=call.execute(); //直接执行 需要抛出异常*/
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//失败以后的方法
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//成功以后回调的方法
String res=response.body().string(); //回调一个字符串
}
});
最后在Manifests中打开网络权限即可使用Get请求请求网络数据了。
get请求头的格式:
例: url+”login?username=chengdu&password=123456”
post请求大体步骤和GET请求差不多,具体如代码所示
//获得一个功能强大的FormBody表单
RequestBody formBody=new FormBody.Builder()
.build();
//2.构造Request
Request.Builder builder=new Request.Builder();
Request request=builder.url("http://www.imooc.com/").post(formBody).build();
// 3、将Request封装为Call
executeRequest(request);
RequestBody的数据格式都要指定Content-Type,常见的有三种:
之后还要加上 charset=utf-8
POST Json例子
public void postJson(){
//获得一个FormBody表单
RequestBody requestBody=RequestBody.
create(MediaType.parse(
"application/json;charset=utf-8"),"{Json字符串}");
//2.构造Request
Request.Builder builder=new Request.Builder();
Request request=builder.url("http://www.imooc.com/").post(requestBody).build();
// 3、将Request封装为Call
executeRequest(request);
}
POST 上传一个图片
public void postFile(){
File file=new File(Environment.getExternalStorageDirectory(),"banana.jpg");
if (!file.exists()){
Log.e("-------","不存在");
return;
}
//获得一个FormBody表单
RequestBody requestBody=RequestBody.
create(MediaType.parse(
"application/octet-stream; charset=utf-8"),file);
//2.构造Request
Request.Builder builder=new Request.Builder();
Request request=builder.url("http://www.imooc.com/").post(requestBody).build();
// 3、将Request封装为Call
executeRequest(request);
}
public void downloadFile(){
Request.Builder builder=new Request.Builder();
Request request=builder
.get()
.url("所要下载文件的文件路径")
.build();
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//失败以后的方法
Log.e("----","失败以后的方法");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream inputStream=response.body().byteStream();
int len=0;
File file=new File(Environment.getExternalStorageDirectory(), "zc.jpg");
byte[] buf =new byte[128];
FileOutputStream fos=new FileOutputStream(file);
while ((len= inputStream.read(buf))!=-1){
fos.write(buf,0,len);
}
fos.flush();
fos.close();
inputStream.close();
}
});
//下载图片并且设置
public void downloadSetImage(){
Request.Builder builder=new Request.Builder();
Request request=builder
.get()
.url("所要下载文件的文件路径")
.build();
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//失败以后的方法
Log.e("----","失败以后的方法");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream inputStream=response.body().byteStream();
final Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
runOnUiThread(new Runnable() {
@Override
public void run() {
mImageView.setImageBitmap(bitmap);
}
});
}
});
}