okhttp的用途以及使用方法
1.GET请求方法
代码演示
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.callTimeout(1, TimeUnit.MINUTES);
builder.readTimeout(1, TimeUnit.MINUTES);
OkHttpClient client = builder.build();
Request.Builder builder1 = new Request.Builder();
builder1.get();
builder1.url("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703& itemid=2&act=ad_app");
Request request = builder1.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("wjx", "onFailure: "+e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
ResponseBody body = response.body();
String json = body.string();
Log.i("wjx", "onResponse: "+json);
}
});
2.POST请求方法
代码演示
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(1, TimeUnit.MINUTES)
.callTimeout(1, TimeUnit.MINUTES)
.build();
FormBody formBody = new FormBody.Builder()
.add("itemid", "2")
.add("act", "ad_app")
.build();
final Request request = new Request.Builder()
.url("http://api.yunzhancn.cn/api/app.interface.php?siteid=78703")
.post(formBody)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("wjx", "onFailure: "+e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
ResponseBody body = response.body();
String json = body.string();
Log.i("wjx", "onResponse: "+json);
}
});
3.下载文件 使用GET请求
代码演示
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(1, TimeUnit.MINUTES)
.callTimeout(1, TimeUnit.MINUTES)
.build();
final Request request = new Request.Builder()
.url("http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4")
.get()
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("wjx", "onFailure: "+e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
ResponseBody body = response.body();
long l = body.contentLength();
Log.i("wjx", "onResponse: "+l);
InputStream inputStream = body.byteStream();
FileOutputStream fileOutputStream = new FileOutputStream("/sdcard/Movies/aa.mp4");
int len = 0;
int count = 0;
byte[] b = new byte[1024];
while((len = inputStream.read(b)) != -1){
count += len;
fileOutputStream.write(b,0,len);
int cc = (int) ((count / l) * 100);
progress.setProgress(cc);
}
}
});
4.上传文件 使用POST请求
代码演示
OkHttpClient client = new OkHttpClient.Builder()
.callTimeout(1, TimeUnit.MINUTES)
.readTimeout(1, TimeUnit.MINUTES)
.build();
MultipartBody file = new MultipartBody.Builder()
.setType(MultipartBody.FORM).addFormDataPart("file", "wangjiaxu.mp4",
RequestBody.create(MediaType.parse("Movies/mp4"), new File("/sdcard/Movies/aa.mp4")))
.build();
final Request request = new Request.Builder()
.post(file).url("http://169.254.113.244/hfs/")
.build();
Call call = client
.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("wjx", "onFailure: 没传上去");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("wjx", "onResponse: "+response.body().string());
}
});