android网络连接系列片之(二)----httpurlconnection
来源:互联网 发布:win7 iis php环境搭建 编辑:程序博客网 时间:2024/06/05 22:31
一、这是一个面向连接的协议,当然所有的http协议都是面向连接的
二、适用范围:
An URLConnection
for HTTP (RFC 2616) used to send and receive data over the web. Data may be of any type and length. This class may be used to send and receive streaming data whose length is not known in advance.
这是google sdk上面的一句原话,httpURLConnection是可以传输任何类型的数据以及任何长度的数据,也可以用来传输事先不知道长度的数据。由此可见它的适用范围很广。
三、使用步骤:
Uses of this class follow a pattern:
- Obtain a new
HttpURLConnection
by callingURL.openConnection()
and casting the result toHttpURLConnection
. - Prepare the request. The primary property of a request is its URI. Request headers may also include metadata such as credentials, preferred content types, and session cookies.
- Optionally upload a request body. Instances must be configured with
setDoOutput(true)
if they include a request body. Transmit data by writing to the stream returned bygetOutputStream()
. - Read the response. Response headers typically include metadata such as the response body's content type and length, modified dates and session cookies. The response body may be read from the stream returned by
getInputStream()
. If the response has no body, that method returns an empty stream. - Disconnect. Once the response body has been read, the
HttpURLConnection
should be closed by callingdisconnect()
. Disconnecting releases the resources held by a connection so they may be closed or reused.
四、使用举例:
1、
URL url = new URL("http://www.android.com/"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); readStream(in);
finally { urlConnection.disconnect(); } }
上面是google sdk里面的一段代码,非常简洁。这里只是从服务器获取数据。注意reandStream(InputStream stream)这个函数要自己写2、
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); try { urlConnection.setDoOutput(true); urlConnection.setChunkedStreamingMode(0); OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream()); writeStream(out); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); readStream(in);
finally { urlConnection.disconnect(); } }
这也是google sdk上面的一段源代码,这里使用的是post方式来获取发送数据到服务器。然后再获取服务器发送的数据。
以上就是对httpURLConnection 的基本步骤进行了解,后面会在这个的基础上对它进行封装。
- android网络连接系列片之(二)----httpurlconnection
- android之网络连接方式系列片(一)
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- Android--网络连接之HttpURLConnection和HttpClient
- Android网络连接之HttpURLConnection和HttpClient
- 准确率召回率
- 开心代码————游戏人生
- 第10次c语言上机实验报告
- iOS官方Sample大全
- 符合数据对象
- android网络连接系列片之(二)----httpurlconnection
- 如何防止android程序被kill掉
- MyEclipse下axis2插件的安装
- linux公社大量免费的在线android资料
- ZET V960 root 权限破解终极记录
- 6.21.2013
- 拼出漂亮的表格
- 隐藏tabbar
- 一些书签