目录
1.Get请求(同步和异步)
2.POST请求表单(key-value)
3.POST请求提交(JSON/String/文件等)
4.文件下载
5.请求超时设置
说明:基于okhttp3.8.0
1.Get请求(同步)
2、Get请求(异步)
3、post请求表单
4、post提交json数据
5、post提交file文件
6、文件下载
7、超时设置
8、源码
public class MainActivityextends AppCompatActivity {
private static OkHttpClientclient =new OkHttpClient();
private Stringurl ="";
private Requestrequest;
/*
* 超时设置*
*/
static {
client.newBuilder().connectTimeout(10, TimeUnit.SECONDS);
client.newBuilder().readTimeout(10,TimeUnit.SECONDS);
client.newBuilder().writeTimeout(10,TimeUnit.SECONDS);
}
private TextViewtext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = ((TextView) findViewById(R.id.textView));
}
/*
* 同步Get
* */
public void syncGet(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
request =new Request.Builder().url(url).build();
Response response =client.newCall(request).execute();
final String message = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
text.setText(message);
}
});
}catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
/*
* 异步Get
* */
public void asyncGet(View view) {
new Thread(new Runnable() {
@Override
public void run() {
request =new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call,final Response response)throws IOException {
if (response.isSuccessful()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String string =null;
try {
string =response.body().string();
text.setText(string);
}catch (IOException e) {
e.printStackTrace();
}
Log.d("MainActivity", string);
}
});
}
}
});
}
}).start();
}
/*
* post表单提交* */
public void postForm(View view) {
//表单请求体
FormBody body =new FormBody.Builder().add("type","1").build();
request =new Request.Builder().url(url).post(body).build();
new Thread(new Runnable() {
@Override
public void run() {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response)throws IOException {
if (response.isSuccessful()) {
text.setText("提交成功");
}
}
});
}
}).start();
}
/*
* 下载* */
public void download(View view) {
request =new Request.Builder().url(url).build();
new Thread(new Runnable() {
@Override
public void run() {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response)throws IOException {
if (response.isSuccessful()) {
//转换字节流
InputStream inputStream = response.body().byteStream();
//文件输出流
FileOutputStream outputStream =new FileOutputStream(new File("sdcard/xxx.txt"));
//字节数组
byte[] bytes =new byte[2048];
int len =0;
//循环读
while ((len = inputStream.read(bytes)) != -1){
//写文件
outputStream.write(bytes,0,len);
}
//关闭流
outputStream.flush();
}
}
});
}
}).start();
}
/*
* 提交json
* */
public void postJson(View view) {
//设置类型
MediaType jsonType = MediaType.parse("application/json; charset=utf-8");
String json ="{\\\"username\\\":\\\"lisi\\\",\\\"nickname\\\":\\\"李四\\\"}";
RequestBody body = RequestBody.create(jsonType,json);
//创建request
request =new Request.Builder().url(url).post(body).build();
new Thread(new Runnable() {
@Override
public void run() {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response)throws IOException {
}
});
}
}).start();
}
/*
* 提交文件* */
public void postFile(View view) {
//设置类型
MediaType fileType = MediaType.parse("File/*");
//文件
File file =new File("path");
RequestBody body = RequestBody.create(fileType,file);
//创建request
request =new Request.Builder().url(url).post(body).build();
new Thread(new Runnable() {
@Override
public void run() {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response)throws IOException {
}
});
}
}).start();
}
}
网友评论