首页 > 开发 > Android > 正文

使用URL访问网络资源详细介绍以及使用

2016-05-23 22:48:33  来源:慕课网
  URL 统一资源定位器
protocol://host:port/resourceName
http://www.crazyit.org/index.php
  URL类提供了多个构造器用于创建URL对象,一旦获得了URL对象后,就可以调用如下常用方法访问该URL对应的资源
String getFile():获取此URL的资源名
String getHost():获取此URL的主机名
String getPath():获取此URL的路径部分
int getPort():获取此URL的端口号
String getProtocol():获取此URL的协议名称
String getQuery():获取此URL的查询字符串部分
  URLConnection openConnection():返回一个URLConnection对象,它表示到URL所引用的远程对象的连接
InputStream openStream(): 打开与此URL的连接,并返回一个用于读取该URL资源的InputStream
public class URLTest extends Activity{ ImageView show; // 代表从网络下载得到的图片 Bitmap bitmap; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if(msg.what == 0x123) { // 使用ImageView显示该图片 show.setImageBitmap(bitmap); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); show = (ImageView) findViewById(R.id.show); new Thread() { public void run() { try { // 定义一个URL对象 URL url = new URL("http://www.crazyit.org/" + "attachments/month_1008/20100812_7763e970f" + "822325bfb019ELQVym8tW3A.png"); // 打开该URL对应的资源的输入流 InputStream is = url.openStream(); // 从InputStream中解析出图片 bitmap = BitmapFactory.decodeStream(is); // 发送消息、通知UI组件显示该图片 handler.sendEmptyMessage(0x123); is.close(); // 再次打开URL对应的资源的输入流 is = url.openStream(); // 打开手机文件对应的输出流 OutputStream os = openFileOutput("crazyit.png" , MODE_WORLD_READABLE); byte[] buff = new byte[1024]; int hasRead = 0; // 将URL对应的资源下载到本地 while((hasRead = is.read(buff)) > 0) { os.write(buff, 0 , hasRead); } is.close(); os.close(); } catch (Exception e) { e.printStackTrace(); } } }.start(); }}<!-- 授权访问网络 --><uses-permission android:name="android.permission.INTERNET"/>