Okhttp
Get请求post请求DownLoadUpLoad
依赖
implementation
'com.squareup.okhttp3:okhttp:3.12.1'
Get请求
buttonGet
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v
) {
OkHttpClient client
= new OkHttpClient.Builder()
.callTimeout(20, TimeUnit
.SECONDS
)
.build();
Request build
= new Request.Builder()
.url("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1")
.get()
.build();
Call call
= client
.newCall(build
);
call
.enqueue(new Callback() {
@Override
public void onFailure(Call call
, IOException e
) {
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "失败...", Toast
.LENGTH_SHORT
).show();
}
});
}
@Override
public void onResponse(Call call
, Response response
) throws IOException
{
ResponseBody body
= response
.body();
final String string
= body
.string();
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "成功" + string
, Toast
.LENGTH_SHORT
).show();
}
});
}
});
}
});
post请求
buttonPost
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v
) {
OkHttpClient client
= new OkHttpClient.Builder()
.callTimeout(20, TimeUnit
.SECONDS
)
.build();
FormBody page
= new FormBody.Builder()
.add("page", "1")
.build();
Request build
= new Request.Builder()
.url("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20")
.post(page
)
.build();
Call call
= client
.newCall(build
);
call
.enqueue(new Callback() {
@Override
public void onFailure(Call call
, IOException e
) {
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "失败", Toast
.LENGTH_SHORT
).show();
}
});
}
@Override
public void onResponse(Call call
, Response response
) throws IOException
{
ResponseBody body
= response
.body();
final String string
= body
.string();
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "成功" + string
, Toast
.LENGTH_SHORT
).show();
}
});
}
});
}
});
DownLoad
buttonDownload
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v
) {
OkHttpClient client
= new OkHttpClient.Builder()
.callTimeout(20, TimeUnit
.SECONDS
)
.build();
Request request
= new Request.Builder()
.url("http://uvideo.spriteapp.cn/video/2019/1030/5db927d9dcc67_wpd.mp4")
.get()
.build();
Call call
= client
.newCall(request
);
call
.enqueue(new Callback() {
@Override
public void onFailure(Call call
, IOException e
) {
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "失败", Toast
.LENGTH_SHORT
).show();
}
});
}
@Override
public void onResponse(Call call
, Response response
) throws IOException
{
ResponseBody body
= response
.body();
long length
= body
.contentLength();
InputStream inputStream
= body
.byteStream();
File file
= new File("/storage/emulated/0/bb.mp4");
FileOutputStream fos
= new FileOutputStream(file
);
int len
= 0;
byte[] bys
= new byte[1024];
int count
= 0;
while ((len
= inputStream
.read(bys
)) != -1) {
fos
.write(bys
, 0, len
);
count
+= len
;
final int progress
= (int) (count
* 100 / length
);
handler
.post(new Runnable() {
@Override
public void run() {
progressDownload
.setProgress(progress
);
}
});
}
}
});
}
});
UpLoad
buttonUpload
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v
) {
OkHttpClient client
= new OkHttpClient.Builder()
.readTimeout(20, TimeUnit
.SECONDS
)
.connectTimeout(20, TimeUnit
.SECONDS
)
.build();
RequestBody requestBody
= RequestBody
.create(MediaType
.parse("media/jpg"), new File("/storage/emulated/0/jin1.PNG"));
MultipartBody file
= new MultipartBody.Builder()
.setType(MultipartBody
.FORM
)
.addFormDataPart("file", "tooth.jpg",requestBody
)
.build();
Request request
= new Request.Builder()
.url(upload_path
)
.post(file
)
.build();
client
.newCall(request
).enqueue(new Callback() {
@Override
public void onFailure(Call call
, IOException e
) {
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "失败", Toast
.LENGTH_SHORT
).show();
}
});
}
@Override
public void onResponse(Call call
, Response response
) throws IOException
{
handler
.post(new Runnable() {
@Override
public void run() {
Toast
.makeText(MainActivity
.this, "成功", Toast
.LENGTH_SHORT
).show();
}
});
}
});
}
});
转载请注明原文地址: https://mac.8miu.com/read-503671.html