- 【Android】Android 封装 Http 请求工具
- 工具类
- 使用案例
- 微信公众号
干货直接上代码
工具类import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
public class HttpUtils {
public static void getRequest(String url, Map params, String encode, OnResponseListener listener) {
StringBuffer sb = new StringBuffer(url);
sb.append("?");
if (params != null && !params.isEmpty()) {
//增强for遍历循环添加拼接请求内容
for (Map.Entry entry : params.entrySet()) {
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
sb.deleteCharAt(sb.length() - 1);
if (listener != null) {
try {
URL path = new URL(sb.toString());
if (path != null) {
HttpURLConnection con = (HttpURLConnection) path.openConnection();
//设置请求方式
con.setRequestMethod("GET");
//链接超时3秒
con.setConnectTimeout(3000);
con.setDoOutput(true);
con.setDoInput(true);
OutputStream os = con.getOutputStream();
os.write(sb.toString().getBytes(encode));
os.close();
//应答码200表示请求成功
if (con.getResponseCode() == 200) {
onSuccess(encode, listener, con);
}
}
} catch (Exception error) {
error.printStackTrace();
onError(listener, error);
}
}
}
}
public static void postRequest(String url, Map params, String encode, OnResponseListener listener) {
StringBuffer sb = new StringBuffer();
if (params != null && !params.isEmpty()) {
for (Map.Entry entry : params.entrySet()) {
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
sb.deleteCharAt(sb.length() - 1);
}
if (listener != null) {
try {
URL path = new URL(url);
if (path != null) {
HttpURLConnection con = (HttpURLConnection) path.openConnection();
con.setRequestMethod("POST"); //设置请求方法POST
con.setConnectTimeout(3000);
con.setDoOutput(true);
con.setDoInput(true);
byte[] bytes = sb.toString().getBytes();
OutputStream outputStream = con.getOutputStream();
outputStream.write(bytes);
outputStream.close();
if (con.getResponseCode() == 200) {
onSuccess(encode, listener, con);
}
}
} catch (Exception e) {
e.printStackTrace();
onError(listener, e);
}
}
}
private static void onError(OnResponseListener listener, Exception onError) {
listener.onError(onError.toString());
}
private static void onSuccess(String encode, OnResponseListener listener, HttpURLConnection con) throws IOException {
InputStream inputStream = con.getInputStream();
//创建内存输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[] bytes = new byte[1024];
if (inputStream != null) {
while ((len = inputStream.read(bytes)) != -1) {
baos.write(bytes, 0, len);
}
String str = new String(baos.toByteArray(), encode);
listener.onSuccess(str);
}
}
}
public interface OnResponseListener {
void onSuccess(String response);
void onError(String error);
}
使用案例
Map微信公众号map = new HashMap<>(); map.put("extract", encrypt); HttpUtils.getRequest("http://tellsea.4kb.cn/arcFace/arcFace", map, "utf-8", new OnResponseListener() { @Override public void onSuccess(String response) { System.out.println(response); AjaxResult result = new Gson().fromJson(response, AjaxResult.class); if (IntegerUtils.eq(result.getCode(), 200)) { // 处理逻辑业务 } else { // 识别失败 } } @Override public void onError(String error) { System.out.println("请求失败:" + error); } });



