Android HttpConnection 使用
来源:互联网 发布:淘宝在线店招制作 编辑:程序博客网 时间:2024/05/17 17:44
1,从Intent获取网页,发送请求,将网页以流的形式读回来。
1)创建一个URL对象:URL url = new URL("http://www.iteye.com");
2) 获取链接 :HttpURLConnection conn=(HttpURLConnection )url.openConnection();
3) 设置超时时间:conn.setConnectTimeout(6*1000);
4) 设置允许输入输出:conn.setDoInput(true);conn.setDoOutput(true);
5) 设置请求模式:conn.setRequestMethod("POST");//注意,要大写
6)设置读取超时:conn.setReadTimeout(true);
7)得到网络返回的输入输出流:InputStream/OutputStream ios = conn.getInputStream()/getOutputStream();
8)判断是否连接成功:conn.getResponseCode()!=200 throw new RuntimeException("请求url失败");
注意:
--在对大文件的操作时,要将文件写到SDCard上面,不要直接写到手机内存上.
--操作大文件是,要一遍从网络上读,一遍要往SDCard上面写,减少手机内存的使用.这点很重要,面试经常会被问到.
--对文件流操作完,要记得及时关闭.
- public class HttpConnect {
- private boolean isCancel = false;
- public void setCancel(boolean isCancel) {
- this.isCancel = isCancel;
- }
- public byte[] open(String strUrl, String postData) {
- byte[] data = null;
- InputStream is = null;
- OutputStream os = null;
- ByteArrayOutputStream baos = null;
- HttpURLConnection conn = null;
- try {// 为了测试取消连接
- // Thread.sleep(5000);
- // http联网可以用httpClient或java.net.url
- URL url = new URL(strUrl);
- conn = (HttpURLConnection) url.openConnection();
- conn.setDoInput(true);
- conn.setDoOutput(true);
- conn.setConnectTimeout(1000 * 30);
- conn.setReadTimeout(1000 * 30);
- if (Tools.isNull(postData)) {
- conn.setRequestMethod("GET");
- } else {
- conn.setRequestMethod("POST");
- os = conn.getOutputStream();
- byte[] sendData = postData.getBytes();
- os.write(sendData);// 将post数据发出去
- }
- if (isCancel) {
- LogUtil.i("open", "已经取消掉了连接");
- return null;
- }
- int responseCode = conn.getResponseCode();
- if (responseCode == 200) {
- is = conn.getInputStream();
- baos = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024 * 8];
- int size = 0;
- while ((size = is.read(buffer)) >= 1) {
- baos.write(buffer, 0, size);
- }
- data = baos.toByteArray();
- }
- } catch (Exception e) {
- ExceptionUtil.handle(e);
- } finally {
- try {
- if (is != null) {is.close();}
- if (os != null) {os.close();}
- if (baos != null) {baos.close();}
- conn = null;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (isCancel) {
- LogUtil.i("open", "已经取消掉了连接");
- return null;
- }
- return data;
- }
- }
- 参考链接:
1 0
- Android HttpConnection 使用
- Android 使用URL,URLConnection,HttpConnection 不能通信的问题
- Android中使用HttpConnection发送中文到服务器端乱码解决办法
- Android使用Http协议访问网络——HttpConnection
- Android HttpConnection HttpClient
- Android HTTP(HttpClient, HttpConnection)
- HttpConnection
- android应用开发之HttpConnection
- Android Http编程之HttpConnection
- Android之Http通信HttpConnection
- (HttpConnection) Connector.open使用注意
- Android的HttpConnection和HttpClient发送请求
- android 基础 网络交互 HttpConnection交互
- Android中HttpConnection+Servlet实现网络请求
- 14 使用HttpConnection获取网络网页源代码
- Android HttpClient上传文件与Httpconnection知识小结
- android中HttpClient和HttpConnection的区别和应用
- Android HttpClient上传文件与Httpconnection知识小结
- HttpConnection详解
- 堆排序
- hdu 1272 小希的迷宫
- 对汇编中子程序结构的认识
- cv::CascadeClassifier类在多次读取新版本xml模型时的错误及其修正
- Android HttpConnection 使用
- Jprofiler工具安装及使用
- 霍夫曼压缩算法
- n+lua
- Nginx 负载均衡配置和策略
- python 操作excel 读写文件
- svn、git的使用
- Android shape文件属性详解
- 专车管理存在多处漏洞 陷入合法性危机