Android HttpUrlConnection 发送网络请求步骤总结
来源:互联网 发布:js函数call 编辑:程序博客网 时间:2024/04/29 08:40
1,URL url =
new
URL(path);
2,通过url获取连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
3,设置请求方式:
conn.setRequestMethod(GET);
4,设置连接超时:
conn.setConnectTimeout(
5000
);
5,设置请求头的信息:
conn.setRequestProperty(User-Agent, Mozilla/
5.0
(compatible; MSIE
9.0
; Windows NT
6.1
; Trident/
5.0
));
6,获取响应码:
int
code = conn.getResponseCode();
7,针对不同的响应码,做不同的操作:
请求码200,表明请求成功,获取返回内容的输入流:InputStream is = conn.getInputStream();
将输入流转换成字符串:
public
class
StreamTools {
/**
* 将输入流转换成字符串
*
* @param is
* 从网络获取的输入流
* @return
*/
public
static
String streamToString(InputStream is) {
try
{
ByteArrayOutputStream baos =
new
ByteArrayOutputStream();
byte
[] buffer =
new
byte
[
1024
];
int
len =
0
;
while
((len = is.read(buffer)) != -
1
) {
baos.write(buffer,
0
, len);
}
baos.close();
is.close();
byte
[] byteArray = baos.toByteArray();
return
new
String(byteArray);
}
catch
(Exception e) {
Log.e(tag, e.toString());
return
null
;
}
}
}
若返回值400,则是返回网络异常,做出响应的处理。
1、HttpUrlConnection发送POST请求
/**
* 通过HttpUrlConnection发送POST请求
*
* @param username
* @param password
* @return
*/
public
static
String loginByPost(String username, String password) {
String path = http:
//192.168.0.107:8080/WebTest/LoginServerlet;
try
{
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(
5000
);
conn.setRequestMethod(POST);
conn.setRequestProperty(Content-Type, application/x-www-form-urlencoded);
String data = username= + username + &password= + password;
conn.setRequestProperty(Content-Length, data.length() + );
// POST方式,其实就是浏览器把数据写给服务器
conn.setDoOutput(
true
);
// 设置可输出流
OutputStream os = conn.getOutputStream();
// 获取输出流
os.write(data.getBytes());
// 将数据写给服务器
int
code = conn.getResponseCode();
if
(code ==
200
) {
InputStream is = conn.getInputStream();
return
StreamTools.streamToString(is);
}
else
{
return
网络访问失败;
}
}
catch
(Exception e) {
e.printStackTrace();
return
网络访问失败;
}
}
HttpUrlConnection发送GET请求
/**
* 通过HttpUrlConnection发送GET请求
*
* @param username
* @param password
* @return
*/
public
static
String loginByGet(String username, String password) {
String path = http:
//192.168.0.107:8080/WebTest/LoginServerlet?username= + username + &password= + password;
try
{
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(
5000
);
conn.setRequestMethod(GET);
int
code = conn.getResponseCode();
if
(code ==
200
) {
InputStream is = conn.getInputStream();
// 字节流转换成字符串
return
StreamTools.streamToString(is);
}
else
{
return
网络访问失败;
}
}
catch
(Exception e) {
e.printStackTrace();
return
网络访问失败;
}
}
/**
* 通过HttpUrlConnection发送POST请求
*
* @param username
* @param password
* @return
*/
public
static
String loginByPost(String username, String password) {
String path = http:
//192.168.0.107:8080/WebTest/LoginServerlet;
try
{
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(
5000
);
conn.setRequestMethod(POST);
conn.setRequestProperty(Content-Type, application/x-www-form-urlencoded);
String data = username= + username + &password= + password;
conn.setRequestProperty(Content-Length, data.length() + );
// POST方式,其实就是浏览器把数据写给服务器
conn.setDoOutput(
true
);
// 设置可输出流
OutputStream os = conn.getOutputStream();
// 获取输出流
os.write(data.getBytes());
// 将数据写给服务器
int
code = conn.getResponseCode();
if
(code ==
200
) {
InputStream is = conn.getInputStream();
return
StreamTools.streamToString(is);
}
else
{
return
网络访问失败;
}
}
catch
(Exception e) {
e.printStackTrace();
return
网络访问失败;
}
}
HttpUrlConnection发送GET请求
/**
* 通过HttpUrlConnection发送GET请求
*
* @param username
* @param password
* @return
*/
public
static
String loginByGet(String username, String password) {
String path = http:
//192.168.0.107:8080/WebTest/LoginServerlet?username= + username + &password= + password;
try
{
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(
5000
);
conn.setRequestMethod(GET);
int
code = conn.getResponseCode();
if
(code ==
200
) {
InputStream is = conn.getInputStream();
// 字节流转换成字符串
return
StreamTools.streamToString(is);
}
else
{
return
网络访问失败;
}
}
catch
(Exception e) {
e.printStackTrace();
return
网络访问失败;
}
}
0 0
- Android HttpUrlConnection 发送网络请求步骤总结
- Android用httpURLConnection发送post网络请求并拿到数据
- Android使用HttpURLConnection发送网络请求(笔记)
- HttpURLConnection发送网络请求的用法
- android HttpURLConnection网络请求数据
- android 请求网络数据 HttpURLConnection
- Android基于HttpURLConnection网络请求
- android网络请求HttpURLConnection详解
- HttpClient 发送网络请求步骤
- Android 使用HttpURLConnection发送Post/Get请求
- Android使用HttpURLConnection发送Get请求
- Android上使用HttpURLConnection发送HTTP请求
- android使用HttpURLConnection发送Post/Get请求
- Android使用HttpURLConnection发送HTTP请求
- httpURLConnection发送http请求
- Android中的HttpURLConnection网络请求方式
- 网络请求测试之HttpUrlConnection【Android】
- Android 网络请求(一)HttpURLConnection+OkHttp
- 安卓开发文件缓存方法的具体实现
- 【LeetCode】又开新篇章啦
- 学习笔记 一
- 安装apk到AVD
- 自定义导航转场动画以及更多
- Android HttpUrlConnection 发送网络请求步骤总结
- Android中TextView中部分文字有不同颜色的实现
- php面向对象一些笔记
- widget和Decorator装修器的使用
- Zookeeper学习9_zookeeper项目使用几点小结
- ORACLE SQL性能优化系列 (三)
- JavaEE_JSP_JSTL标签 参考手册
- cocos2d-x开发学习笔记(一)
- Linux中vi编辑器