OKHTTP工具类

添加依赖
第一步:

	implementation 'com.squareup.okhttp3:okhttp:3.8.1'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.github.bumptech.glide:glide:4.8.0'
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    implementation 'com.google.code.gson:gson:2.8.5'

第二步工具类

	package com.dingtao.logindemo.http;
	
	import java.io.File;
	import java.io.IOException;
	import java.util.ArrayList;
	import java.util.List;
	import java.util.concurrent.TimeUnit;
	
	import okhttp3.Call;
	import okhttp3.FormBody;
	import okhttp3.MediaType;
	import okhttp3.MultipartBody;
	import okhttp3.OkHttpClient;
	import okhttp3.Request;
	import okhttp3.RequestBody;
	import okhttp3.Response;
	
	public class Utils {
	    public static String get(String urlString){
	
	        OkHttpClient okHttpClient = new OkHttpClient();
	
	        Request request = new Request.Builder().url(urlString).get().build();
	
	        try {
	            Response response = okHttpClient.newCall(request).execute();
	
	            return response.body().string();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return "";
	    }
	
	    public static String postForm(String url,String[] name,String[] value){
	
	        OkHttpClient okHttpClient = new OkHttpClient();
	
	        FormBody.Builder formBuild = new FormBody.Builder();
	        for (int i = 0; i < name.length; i++) {
	            formBuild.add(name[i],value[i]);
	        }
	
	        Request request = new Request.Builder().url(url).post(formBuild.build()).build();
	
	        try {
	            Response response = okHttpClient.newCall(request).execute();
	
	            return response.body().string();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return "";
	    }
	
	    public static String postFile(String url,String[] name,String[] value,String fileParamName,File file){
	
	        OkHttpClient okHttpClient = new OkHttpClient();
	
	        MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
	        if(file != null){
	            // MediaType.parse() 里面是上传的文件类型。
	            RequestBody body = RequestBody.create(MediaType.parse("image/*"), file);
	            String filename = file.getName();
	            // 参数分别为: 文件参数名 ,文件名称 , RequestBody
	            requestBody.addFormDataPart(fileParamName, "jpg", body);
	        }
	        if (name!=null) {
	            for (int i = 0; i < name.length; i++) {
	                requestBody.addFormDataPart(name[i], value[i]);
	            }
	        }
	
	        Request request = new Request.Builder().url(url).post(requestBody.build()).build();
	
	        try {
	            Response response = okHttpClient.newCall(request).execute();
	            if (response.code()==200) {
	                return response.body().string();
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return "";
	    }
	
	    public static String postJson(String url,String jsonString){
	
	        OkHttpClient okHttpClient = new OkHttpClient();
	
	        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),jsonString);
	
	        Request request = new Request.Builder().url(url).post(requestBody).build();
	
	        try {
	            Response response = okHttpClient.newCall(request).execute();
	
	            return response.body().string();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return "";
	    }
	}
OkHttp 是一款基于 Java 的 HTTP 客户端,它使用了连接池、异步 IO 和拦截器等技术来提高性能和灵活性。下面是一个基于 OkHttp 的 Java 工具类,可以用于发送 HTTP 请求和接收响应: ```java import okhttp3.*; import java.io.IOException; public class OkHttpUtils { private static final OkHttpClient client = new OkHttpClient(); public static String get(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) { throw new IOException("Unexpected code " + response); } return response.body().string(); } } public static String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(json, MediaType.get("application/json")); Request request = new Request.Builder() .url(url) .post(body) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) { throw new IOException("Unexpected code " + response); } return response.body().string(); } } } ``` 在上面的代码中,我们使用了 OkHttp 的 `OkHttpClient` 类来创建一个 HTTP 客户端,然后定义了两个方法 `get` 和 `post` 来发送 GET 和 POST 请求。在 `get` 方法中,我们使用 `Request.Builder` 类来构建一个 GET 请求,并使用 `client.newCall(request).execute()` 方法来发送请求并接收响应。在 `post` 方法中,我们使用 `RequestBody` 类来创建一个 JSON 请求体,并将其传递给 `Request.Builder` 类的 `post` 方法来构建一个 POST 请求。最后,我们使用相同的方式发送请求并接收响应。注意,为了避免资源泄漏,我们使用了 try-with-resources 语句来自动关闭响应体。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值