Android中HttpURLConnection与HttpClient的使用与封装
- HttpURLConnection的使用
HttpURLConnection是Android中自带的一个HTTP客户端库,可以轻松的使用HTTP请求。使用HttpURLConnection发送请求的步骤如下:
(1)创建URL对象
URL url = new URL("http://www.example.com");
(2)创建HttpURLConnection对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
(3)设置请求方式
conn.setRequestMethod("GET");
(4)设置连接超时时间
conn.setConnectTimeout(5000);
(5)设置读取超时时间
conn.setReadTimeout(5000);
(6)连接服务器
conn.connect();
(7)获取响应码
int responseCode = conn.getResponseCode();
(8)获取输入流,读取服务器返回的数据
InputStream inputStream = conn.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferedReader.readLine()) != null) {
Log.d("TAG", line);
}
bufferedReader.close();
inputStream.close();
(9)断开连接
conn.disconnect();
- HttpClient的使用
HttpClient是Apache提供的一个HTTP客户端库,可以轻松地使用HTTP请求。使用HttpClient发送请求的步骤如下:
(1)创建HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
(2)根据URL创建HttpGet或HttpPost对象
HttpGet httpGet = new HttpGet("http://www.example.com");
HttpPost httpPost = new HttpPost("http://www.example.com");
(3)设置参数(如果有)
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", "admin"));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
(4)执行请求,获取HttpResponse对象
HttpResponse httpResponse = httpClient.execute(httpGet);
(5)获取响应码
int responseCode = httpResponse.getStatusLine().getStatusCode();
(6)获取数据流,读取服务器返回的数据
InputStream inputStream = httpResponse.getEntity().getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferedReader.readLine()) != null) {
Log.d("TAG", line);
}
bufferedReader.close();
inputStream.close();
(7)释放资源
httpClient.getConnectionManager().shutdown();
- 封装Http请求
封装Http请求可以减少代码重复,提高代码复用性。可以将HttpURLConnection和HttpClient封装成一个类,提供公共方法供外部调用。
(1)封装HttpURLConnection
```
public class HttpHelper {
public static String requestGet(String URLAddress) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
URL url = new URL(URLAddress);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = conn.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
inputStream.close();
}
conn.disconnect();
return stringBuilder.toString();
}
}
```
(2)封装HttpClient
```
public class HttpHelper {
public static String requestGet(String urlAddress) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urlAddress);
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream inputStream = httpResponse.getEntity().getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
inputStream.close();
}
httpClient.getConnectionManager().shutdown();
return stringBuilder.toString();
}
}
```
- 示例说明
(1)使用HttpURLConnection发送GET请求
new Thread(new Runnable() {
@Override
public void run() {
try {
String result = HttpHelper.requestGet("http://www.example.com");
Log.d("TAG", result);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
(2)使用HttpClient发送GET请求
new Thread(new Runnable() {
@Override
public void run() {
try {
String result = HttpHelper.requestGet("http://www.example.com");
Log.d("TAG", result);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android中HttpURLConnection与HttpClient的使用与封装 - Python技术站