当前位置: 移动技术网 > IT编程>移动开发>Android > 一起学Android之Http访问

一起学Android之Http访问

2019年04月07日  | 移动技术网IT编程  | 我要评论

韩少的勾心妻,焰色沦落妃,ca4229

概述

在android开发中,一般通过网络进行访问服务器端的信息(存储和检索网络中的数据),如api接口,webservice,网络图片等。今天主要讲解http访问的常用方法,仅供学习分享使用。

涉及知识点

  1. url 表示互联网位置的统一资源标识符。
  2. httpurlconnection 表示一个url的http连接,是一个抽象类,通过url中的 openconnection()实例化一个连接对象。
  3. setconnecttimeout(5000) 设置超时时间,setrequestmethod("get") 设置访问方式。
  4. getresponsecode() 获取返回码,如200表示成功。
  5. getinputstream() 返回读取到数据的字节流; getoutputstream() 返回往指定url写入数据的字节流。
  6. bytearrayoutputstream 数组和输出流之间的转换。
  7. webview 用于显示web page的一个控件,通过loaddatawithbaseurl(string baseurl, string data,string mimetype, string encoding, string historyurl)加载内容。

http访问步骤

  1. 创建一个url对象: url url = new url(https://www.baidu.com);
  2. 调用url对象的openconnection( )来获取httpurlconnection对象实例: httpurlconnection conn = (httpurlconnection) url.openconnection();
  3. 设置http请求使用的方法:get或者post,或者其他请求方式比如:put conn.setrequestmethod("get");
  4. 设置连接超时,读取超时的毫秒数,以及服务器希望得到的一些消息头 conn.setconnecttimeout(6*1000); conn.setreadtimeout(6 * 1000);
  5. 调用getinputstream()方法获得服务器返回的输入流,然后输入流进行读取了 inputstream in = conn.getinputstream();
  6. 最后调用disconnect()方法将http连接关掉 conn.disconnect();

示例图

如下图所示:

核心代码

获取数据类

 1 /**
 2  * created by hex on 2019/4/6.
 3  */
 4 public class getdata {
 5     // 定义一个获取网络图片数据的方法:
 6     public static byte[] getimage(string path) throws exception {
 7         url url = new url(path);
 8         httpurlconnection conn = (httpurlconnection) url.openconnection();
 9         // 设置连接超时为5秒
10         conn.setconnecttimeout(5000);
11         // 设置请求类型为get类型
12         conn.setrequestmethod("get");
13         // 判断请求url是否成功
14         if (conn.getresponsecode() != 200) {
15             throw new runtimeexception("请求url失败");
16         }
17         inputstream instream = conn.getinputstream();
18         byte[] bt = streamtool.read(instream);
19         instream.close();
20         return bt;
21     }
22 
23     // 获取网页的html源代码
24     public static string gethtml(string path) throws exception {
25         url url = new url(path);
26         httpurlconnection conn = (httpurlconnection) url.openconnection();
27         conn.setconnecttimeout(5000);
28         conn.setrequestmethod("get");
29         if (conn.getresponsecode() == 200) {
30             inputstream in = conn.getinputstream();
31             byte[] data = streamtool.read(in);
32             string html = new string(data, "utf-8");
33             return html;
34         }
35         return null;
36     }
37 }

inputstream转换为byte[]功能

 1 /**
 2  * created by administrator on 2019/4/6.
 3  */
 4 public class streamtool {
 5     //从流中读取数据
 6     public static byte[] read(inputstream instream) throws exception {
 7         bytearrayoutputstream outstream = new bytearrayoutputstream();
 8         byte[] buffer = new byte[1024];
 9         int len = 0;
10         while ((len = instream.read(buffer)) != -1) {
11             outstream.write(buffer, 0, len);
12         }
13         instream.close();
14         return outstream.tobytearray();
15     }
16 }

刷新界面赋值

 1 // 用于刷新界面
 2     private handler handler = new handler() {
 3         public void handlemessage(android.os.message msg) {
 4             switch (msg.what) {
 5                 case 0x001:
 6                     hideallwidget();
 7                     imgpic.setvisibility(view.visible);
 8                     imgpic.setimagebitmap(bitmap);
 9                     toast.maketext(mainactivity.this, "图片加载完毕", toast.length_short).show();
10                     break;
11                 case 0x002:
12                     hideallwidget();
13                     scroll.setvisibility(view.visible);
14                     txtshow.settext(detail);
15                     toast.maketext(mainactivity.this, "html代码加载完毕", toast.length_short).show();
16                     break;
17                 case 0x003:
18                     hideallwidget();
19                     webview.setvisibility(view.visible);
20                     webview.loaddatawithbaseurl("", detail, "text/html", "utf-8", "");
21                     toast.maketext(mainactivity.this, "网页加载完毕", toast.length_short).show();
22                     break;
23                 default:
24                     break;
25             }
26         }
27 
28         ;
29     };

如果要访问网络,还需要有相应的权限

1  <uses-permission android:name="android.permission.internet" />

 

备注

关于http访问的相关知识还有很多,本文知识简单粗略的讲解,希望抛砖引玉,共同学习。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网