使用OKHttp3 get请求 , post请求 , 多文件上传 , 包看包会
来源:互联网 发布:战舰世界峰风数据 编辑:程序博客网 时间:2024/05/31 04:03
1 首先需要创建一个OKHttpClient
可以直接new一个
OkHttpClient client = new OkHttpClient()
更多的是用builder构造一个 (那个addInterceptor方法是添加拦截器 , 可以不写,具体的baidu, bing, sogou)
private void buildHttpClient(){ this.client = new OkHttpClient.Builder() .addInterceptor(new Interceptor() { public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed(request); return response; } }) .connectTimeout(4000, TimeUnit.MILLISECONDS) .readTimeout(4000,TimeUnit.MILLISECONDS) .writeTimeout(4000, TimeUnit.MILLISECONDS) .build(); }
2 GET 请求
<strong> </strong>private void get(){ /* 如果需要参数 , 在url后边拼接 : ?a=aaa&b=bbb..... */ Request request = new Request.Builder().url("http://192.168.10.117:8080/test").build(); client.newCall(request).enqueue(new Callback() { public void onResponse(Call call, final Response response) throws IOException { final String result = response.body().string(); final boolean ok = response.isSuccessful(); runOnUiThread(new Runnable() { public void run(){ Toast.makeText(OKHttpActivity.this, result, Toast.LENGTH_SHORT).show(); } }); } public void onFailure(Call call, IOException e) { runOnUiThread(new Runnable() { public void run() { Toast.makeText(OKHttpActivity.this, "error", Toast.LENGTH_SHORT).show(); } }); } }); }
3 POST 请求
private void post(){ FormBody.Builder builder = new FormBody.Builder(); /* 添加两个参数 */ builder.add("p","我勒个去").add("a","hello"); FormBody body = builder.build(); Request request = new Request.Builder().url("http://192.168.10.117:8080/test").post(body).build(); /* 下边和get一样了 */ client.newCall(request).enqueue(new Callback() { public void onResponse(Call call, Response response) throws IOException { final String bodyStr = response.body().string(); final boolean ok = response.isSuccessful(); runOnUiThread(new Runnable() { public void run() { if(ok){ Toast.makeText(OKHttpActivity.this, bodyStr, Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(OKHttpActivity.this, "server error : " + bodyStr, Toast.LENGTH_SHORT).show(); } } }); } public void onFailure(Call call,final IOException e) { runOnUiThread(new Runnable() { public void run() { Toast.makeText(OKHttpActivity.this, "error : "+e.toString(), Toast.LENGTH_SHORT).show(); } }); } }); }
4 多文件上传
private void upFile(){ /* 第一个要上传的file */ File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/a.jpg"); RequestBody fileBody1 = RequestBody.create(MediaType.parse("application/octet-stream") , file1); String file1Name = "testFile1.txt"; /* 第二个要上传的文件,这里偷懒了,和file1用的一个图片 */ File file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/a.jpg"); RequestBody fileBody2 = RequestBody.create(MediaType.parse("application/octet-stream") , file2); String file2Name = "testFile2.txt"; /* form的分割线,自己定义 */ String boundary = "xx--------------------------------------------------------------xx"; MultipartBody mBody = new MultipartBody.Builder(boundary).setType(MultipartBody.FORM) /* 上传一个普通的String参数 , key 叫 "p" */ .addFormDataPart("p" , "你大爷666") /* 底下是上传了两个文件 */ .addFormDataPart("file" , file1Name , fileBody1) .addFormDataPart("file" , file2Name , fileBody2) .build(); /* 下边的就和post一样了 */ Request request = new Request.Builder().url("http://192.168.10.117:8080/test").post(mBody).build(); client.newCall(request).enqueue(new Callback() { public void onResponse(Call call, Response response) throws IOException { final String bodyStr = response.body().string(); final boolean ok = response.isSuccessful(); runOnUiThread(new Runnable() { public void run() { if(ok){ Toast.makeText(OKHttpActivity.this, bodyStr, Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(OKHttpActivity.this, "server error : " + bodyStr, Toast.LENGTH_SHORT).show(); } } }); } public void onFailure(Call call, final IOException e) { runOnUiThread(new Runnable() { public void run() { Toast.makeText(OKHttpActivity.this, e.toString(), Toast.LENGTH_SHORT).show(); } }); } }); }
简单封装了一个 : https://github.com/yangzhongxu/OkHttp
4 2
- 使用OKHttp3 get请求 , post请求 , 多文件上传 , 包看包会
- 使用OKHttp3 get请求 , post请求 , 多文件上传 , 包看包会
- 快捷使用Retrofit网络请求框架(get请求,post请求,上传文件)
- Http get、post请求、文件上传
- okhttp3封装 get post 请求 下载上传文件 Post请求发送JSON数据 判断下载目录是否存在......并返回绝对路径 从下载连接中解析出文件名
- 09 - get请求、post请求、httpclient、文件上传
- post请求和get请求的区别(文件上传)
- OKhttp:Get同步请求丶Get异步请求丶Post异步请求丶上传文件
- OKhttp:Get同步请求丶Get异步请求丶Post异步请求丶上传文件
- 使用HttpUrlConnection进行post请求上传文件
- 使用HttpUrlConnection进行post请求上传文件
- 使用HttpUrlConnection进行post请求上传文件
- 使用HttpUrlConnection进行post请求上传文件
- iOS开发-使用AFN实现GET/POST请求,以及文件下载/文件(图片)上传
- 【andorid】基于okhttp3网络请求帮助类(GET、POST)
- okhttp3简单封装GET和POST请求工具类
- 网络编程之get,post请求,文件上传及下载
- WP8.1发送Post或Get请求顺带文件上传
- 深入解析 ES6:Generator
- Python 3.x常见问题
- 推荐算法工程师
- 学习和使用ImageLoader、 Picasso、Fresco 图像加载
- 安卓EditText有时点击后无法自动弹出键盘的解决办法;TextWatcher多次调用问题
- 使用OKHttp3 get请求 , post请求 , 多文件上传 , 包看包会
- 无法执行添加/移除操作,因为代码元素**是只读的
- 试试看
- 如何设计一个 iOS 控件?(iOS 控件完全解析)
- Redis快速入门:初识Redis
- firefox浏览器接口调试工具
- ROS XmlrpcNode 接口测试
- Making a Standard Request
- js 点击图片,将图片地址复制到粘贴板