涉及的类有一下几个:URL、URLConnection、HTTPURLConnection
一、URL,利用该类我们可以获取网络资源。如:获取网页源代码、网页图片等等。
例如:http://www:*****.com/china/index.html
(1)协议名:http://
(2)万维网服务器:www:
(3)装有网页服务器的域名或站点服务器的名称:*****.com
(4)访问在服务器上的路径:/china/
(5)访问的内容:index.html
①URL是互联网上的“资源”的唯一地址标识,它由协议名、主机、端口和资源组成。我们通过URL类来获取这些相关的属性。
URL类的常用方法
Public String getProtocol() | 获取URL的协议名 |
Public String getHost() | 获取URL的主机名 |
Public String getPort() | 获取URL的端口号 |
Public String getFile() | 获取URL的文件名 |
Public String getPath() | 获取URL的路径 |
Public String getAuthority() | 获取URL的权限信息 |
Public String getQuery() | 获取URL的查询字符串部分 |
Public final Object getContent() |
获取URL的内容 |
②通过URL类获取网页内容的步骤
(1)先实例化一个URL对象
URL url = new URL("http://www.sohu.com")
(2)利用Public final InputStream openStream()打开URL的连接,再利用InputStreamReader()接收
InputStreamReader inSr = new InputStreamReader(url.openStream();
(3)再构造一个BuffererReader()对象获取资源(如要对网页资源进行解析,则需要使用URI类)
BufferReader br = new BufferReader(inSr);
String str = br.readLine();
二、URLConnection,利用该类我们可以传递HTTP的消息头,对它可以上传参数。可以完成一些删除、查找等功能的应用
不过实现这些操作常用它的子类HTTPURLConnection类。
①URLConnection是抽象类,是实现应用程序和URL之间通信连接的所有类的超类,该类的对象可以对URL所指定的资源进行读写操作
URLConnection常用的访问方法
Public abstract void connect() | 建立到此URL引用的资源的通信连接 |
Public Object getConnect() | 获取URL连接的内容 |
Public InputStream getInputStream() | 返回打开连接的输入流 |
Public OutputStream getOutputStream() | 返回打开链接的输出流 |
Public String getConnectionEncoding | 返回content-encoding头字段的值 |
Public int getContentLength() | 返回content-length头字段的值 |
Public String getContentType() | 返回content-type头字段的值 |
Public long getExpiration() | 返回expres头字段的值 |
Public long getDate() | 返回date头字段的值 |
三、HttpURLConnection、利用该类获取网页内容、文件、向网页发送请求参数以及发送XML数据
① 通过HTTPURLConnection类获取网页内容的步骤
(1)创建一个URL对象
URL url = new URL("http://www.sohu.com") ;
(2)得到HTTPURLConnection对象
HTTPURLConnection con = (HttpURLConnection)url.openConnection();
(3)设置超时连接
con.setConnectTimeout(6000);
(4)对响应码进行判断
if(con.getResponseCode() != 200) throw new RuntimeException("请求失败");
(5)得到网络返回的收入流
InputStream is = con.getInputStream();
String result = readDate(is,"GBK");
con.disconnection();
注:设置超时连接的好处,如果网络不好,Android 系统会在超时默认时间后悔回收资源,在步骤(5)中获取网页内容还有考 虑该网页的具体编码格式。
相关案例:https://blog.csdn.net/weixin_43917449/article/details/85368864
②获取网页文件的步骤
如获取网页内容的形式类似,在定位的时候要指定其文件的URL地址,接受时要根据文件的类型将其解析。
③向网页发送请求参数的步骤
(1)将请求参数储存到byte数组中
String para = new String("username = admin&password = admin");
byte[ ] data = para.getBytes();
(2)建立URL连接对象
URL url = new URL("http://www.sohu.com") ;
(3)获得HTTPURLConnection对象
HTTPURLConnection con = (HttpURLConnection)url.openConnection();
(4)设置允许输出
con.setDoput(true);
(5)设置不使用缓存
con,setUerCaches(false);
(6)设置使用Post方式发送
con.setRequestMethod("POST")
(7)设置维持长度连接
con.setRequestProperty("Connection","Keep - Alive");
(8)设置文件字符集
con.setRequestProperty("Charset","UTF - 8");
(9)设置文件长度
con.setRequestProperty("Content - Length",String.valueOf(data.length));
(10)设置文件类型
con.setRequestProperty("Content - Type","application/x -www - from - urlencoded");
(11)最后一个流的方式输出
con.getOutoutStrem().write(data);
注:使用POST请求必须设置允许输出,建议不要是用缓存,避免出现不该出现的问题,避免出现不该出现的问题。同时只有设置Content-Type为application/x-www-form-urlencoded,服务器才可以直接使用request.getParameter("username")接受所需的信息。
相关案例:https://www.jb51.net/article/125699.htm
④向Intent发送XML数据
(1)将生成的XML问价写入byte 数组中同时设置UTF-8,其他步骤与上面③传递参数一致