Android入门:封装一个HTTP请求的辅助类

本文介绍了一个封装的HttpRequestUtil类,提供sendGetRequest和sendPostRequest方法,用于简化HTTP GET和POST请求的实现。通过使用该类,开发者可以更方便地通过传入URL、参数和请求头来执行请求,而无需手动构建URL和处理HTTP头部信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前面的文章中,我们曾经实现了一个HTTP的GET 和 POST 请求;

此处我封装了一个HTTP的get和post的辅助类,能够更好的使用;


类名:HttpRequestUtil

提供的方法:
(1) public static URLConnection sendGetRequest(String url,Map<String, String> params, Map<String, String> headers)

参数:

(1)url:单纯的URL,不带任何参数;

(2)params:参数;

(3)headers:需要设置的HTTP请求头;

返回:

HttpURLConnection

(2)public static URLConnection sendPostRequest(String url,Map<String, String> params, Map<String, String> headers)

参数:

(1)url:单纯的URL,不带任何参数;

(2)params:参数;

(3)headers:需要设置的HTTP请求头;

返回:

HttpURLConnection


package com.xiazdong.network.http.util; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class HttpRequestUtil { public static URLConnection sendGetRequest(String url, Map<String, String> params, Map<String, String> headers) throws Exception { StringBuilder buf = new StringBuilder(url); buf.append("?"); Set<Entry<String, String>> entrys = params.entrySet(); for (Map.Entry<String, String> entry : entrys) { buf.append(entry.getKey()).append("=") .append(URLEncoder.encode(entry.getValue(), "UTF-8")) .append("&"); } buf.deleteCharAt(buf.length() - 1); URL url1 = new URL(buf.toString()); HttpURLConnection conn = (HttpURLConnection) url1.openConnection(); conn.setRequestMethod("GET"); if (headers != null) { entrys = headers.entrySet(); for (Map.Entry<String, String> entry : entrys) { conn.setRequestProperty(entry.getKey(), entry.getValue()); } } conn.getResponseCode(); return conn; } public static URLConnection sendPostRequest(String url, Map<String, String> params, Map<String, String> headers) throws Exception { StringBuilder buf = new StringBuilder(); Set<Entry<String, String>> entrys = params.entrySet(); for (Map.Entry<String, String> entry : entrys) { buf.append(entry.getKey()).append("=") .append(URLEncoder.encode(entry.getValue(), "UTF-8")) .append("&"); } buf.deleteCharAt(buf.length() - 1); URL url1 = new URL(url); HttpURLConnection conn = (HttpURLConnection) url1.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); OutputStream out = conn.getOutputStream(); out.write(buf.toString().getBytes("UTF-8")); if (headers != null) { entrys = headers.entrySet(); for (Map.Entry<String, String> entry : entrys) { conn.setRequestProperty(entry.getKey(), entry.getValue()); } } conn.getResponseCode(); // 为了发送成功 return conn; } }
此后,像前面文章中的例子,我们可以很简单的通过如下方式实现:

Map<String,String> params = new HashMap<String,String>(); params.put("name", name.getText().toString()); params.put("age", age.getText().toString()); HttpURLConnection conn = (HttpURLConnection) HttpRequestUtil.sendGetRequest("http://192.168.0.103:8080/Server/PrintServlet", params, null);
简单了很多,对吧?





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值