第一行代码总结:10网络:10.2使用HTTP协议访问网络10.2.1 使用HttpURLConnection10.2使用HTTP协议访问网络 工作原理: 客户端向服务器发出一条HTTP请求,服务器收
2015-11-27 17:50
866 查看
10.2使用HTTP协议访问网络
工作原理:
客户端向服务器发出一条HTTP请求,服务器收到请求之后会返回一些数据给客户端,然后客户端再对这些数据进行解析和处理就可以了。
在Android上发送HTTP请求的方式一般有两种:
HttpURLConnection
HttpClient
10.2.1 使用HttpURLConnection
使用步骤:
1、获取URL对象,传入网络地址;
URL url = newURL("http://www.baidu.com");
2、获取HttpURLConnection对象。
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
3、设置HTTP请求所使用的方法。常用的方法有两个:GET/POST.
connection.setRequestMethod("GET");
4、进行一些自由的定制
connection.setConnectTimeout(8000); //设置连接超时时间
connection.setReadTimeout(8000); //设置读取超时时间
5、调用getInputStream()方法,获取到服务器返回的输入流:
InputStream in = connection.getInputStream();
6、对输入流进行读取:
//下面对获取到的输入流进行读取
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
//从高效缓冲输入流当中读取字符串并拼接
while((line = reader.readLine()) !=
null){
response.append(line);
}
7、最后调用disconnect()方法,将之歌HTTP连接关闭掉;
finally{
if(connection !=
null){
connection.disconnect();
}
}
8、声明网络权限:
uses-permissionandroid:name=”android.permission.INTERNET”
代码示例:
private
void sendRequestWithURLConnection() {
// 开启线程来发起网络请求
new Thread(new Runnable(){
@Override
public
void run() {
HttpURLConnection connection = null;
try {
URL url = new URL("http://www.baidu.com");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in =connection.getInputStream();
//下面对获取到的输入流进行读取
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
//从高效缓冲输入流当中读取字符串并拼接
while((line = reader.readLine()) !=
null){
response.append(line);
}
//将从网络当中读取到的内容返回给主线程:利用异步消息处理机制,handler
Message message = new Message();
message.what =
SHOW_RESPONSE;
//将服务器返回的结果存放到Message中
message.obj = response.toString();
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(connection !=
null){
connection.disconnect();
}
}
}
}).start();
}
工作原理:
客户端向服务器发出一条HTTP请求,服务器收到请求之后会返回一些数据给客户端,然后客户端再对这些数据进行解析和处理就可以了。
在Android上发送HTTP请求的方式一般有两种:
HttpURLConnection
HttpClient
10.2.1 使用HttpURLConnection
使用步骤:
1、获取URL对象,传入网络地址;
URL url = newURL("http://www.baidu.com");
2、获取HttpURLConnection对象。
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
3、设置HTTP请求所使用的方法。常用的方法有两个:GET/POST.
connection.setRequestMethod("GET");
4、进行一些自由的定制
connection.setConnectTimeout(8000); //设置连接超时时间
connection.setReadTimeout(8000); //设置读取超时时间
5、调用getInputStream()方法,获取到服务器返回的输入流:
InputStream in = connection.getInputStream();
6、对输入流进行读取:
//下面对获取到的输入流进行读取
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
//从高效缓冲输入流当中读取字符串并拼接
while((line = reader.readLine()) !=
null){
response.append(line);
}
7、最后调用disconnect()方法,将之歌HTTP连接关闭掉;
finally{
if(connection !=
null){
connection.disconnect();
}
}
8、声明网络权限:
uses-permissionandroid:name=”android.permission.INTERNET”
代码示例:
private
void sendRequestWithURLConnection() {
// 开启线程来发起网络请求
new Thread(new Runnable(){
@Override
public
void run() {
HttpURLConnection connection = null;
try {
URL url = new URL("http://www.baidu.com");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in =connection.getInputStream();
//下面对获取到的输入流进行读取
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder response = new StringBuilder();
String line;
//从高效缓冲输入流当中读取字符串并拼接
while((line = reader.readLine()) !=
null){
response.append(line);
}
//将从网络当中读取到的内容返回给主线程:利用异步消息处理机制,handler
Message message = new Message();
message.what =
SHOW_RESPONSE;
//将服务器返回的结果存放到Message中
message.obj = response.toString();
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(connection !=
null){
connection.disconnect();
}
}
}
}).start();
}
相关文章推荐
- 第一行代码总结:10网络:10.1WebView的用法
- http请求
- HttpClient和HtmlParser配合实现自动CAS单点登录系统抽取页面信息
- 开源安卓Http文件下载框架file-downloader的使用
- 检测网络连接是否正常的方法
- 通过struts2实现Http只允许POST请求
- HTTPS建立连接都发生了什么
- 我的H3CNE学习之路 第一章计算机网络概述
- 浏览器的工作原理:新式网络浏览器幕后揭秘
- 我的H3CNE学习之路
- cocos2dx 2.2.6 使用 HttpClient 可能导致部分新设备崩溃的问题
- OkHttp网络请求的使用
- 安卓Http Post请求封装类
- http://haohaoxuexi.iteye.com/blog/1900568
- 用PHP模拟HTTP中的POST请求JSP网页,提交数据
- 源码五:HTTP网络的使用
- UNIX网络编程——Posix信号量
- delphi 获取网络图片
- XMLHttpRequest cannot load file:///D
- [spring]启动时报错:NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()