使用OKHttp3 get请求 , post请求 , 多文件上传 , 包看包会
来源:互联网 发布:石油大学网络教育 编辑:程序博客网 时间:2024/06/07 03: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(){
-
- 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();
-
-
- 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 file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/a.jpg");
- RequestBody fileBody1 = RequestBody.create(MediaType.parse("application/octet-stream") , file1);
- String file1Name = "testFile1.txt";
-
-
- File file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/a.jpg");
- RequestBody fileBody2 = RequestBody.create(MediaType.parse("application/octet-stream") , file2);
- String file2Name = "testFile2.txt";
-
-
-
- String boundary = "xx--------------------------------------------------------------xx";
-
- MultipartBody mBody = new MultipartBody.Builder(boundary).setType(MultipartBody.FORM)
-
- .addFormDataPart("p" , "你大爷666")
-
- .addFormDataPart("file" , file1Name , fileBody1)
- .addFormDataPart("file" , file2Name , fileBody2)
- .build();
-
-
- 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();
- }
- });
- }
- });
- }
1 0