我们知道,要实现HTTP网络连接,都必须通过建立连接传输数据的基本步骤,下面我们对一个简单的例子稍做剖析。
A、创建URL实例
URL url = new URL("http://www.ataaw.com");
B、打开链接
URLConnection conn = url.openConnection();
C、获取返回码
int reCode = ((HttpURLConnection) conn).getResponseCode();
D、获取输入流
InputStream is = conn.getInputStream();
E、读取流内容
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer buffer = new ByteArrayBuffer(32);
int read = 0;
while ((read = bis.read()) != -1) {
buffer.append((char) read);
}
F、取出读到的字符串信息
EncodingUtils.getString(buffer.toByteArray(),HTTP.UTF_8);
以上就是Android中通过Get方法获取网络链接流的基本方法,可以看出只需几个方法就可以实现网络连接,对实现网络编程是非常方便的。