URL(Uniform Resource Locator)对象代表统一资源定位器,它是指向互联网"资源"的指针。资源可以是简单的文件或目录,也可以是对更复杂的对象的引用,例如对数据库或搜索引擎的查询。通常情况而言,URL可以由协议名、主机、端口和资源组成。即满足如下格式:
protocol://host:port/resourceName
例如如下的URL地址:
http://www.baidu.com/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
例程:使用URL读取网络资源
AndroidManifest.xml——主要添加访问网络权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.url"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.url.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
MainActivity.javapackage com.example.url;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class MainActivity extends Activity {
private ImageView show;
private Bitmap bitmap;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123){
//使用ImageView显示图片
show.setImageBitmap(bitmap);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (ImageView)this.findViewById(R.id.imageView);
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
//定义一个URL对象
URL url = new URL("http://news.c-ps.net/uploads/141024/18-141024152F62P.jpg");
Log.e("URL资源名", url.getFile());
Log.e("URL主机名", url.getHost());
Log.e("URL路径部分", url.getPath());
Log.e("URL端口号", ""+url.getPort());
//打开该URL对应的资源的输入流
InputStream inputStream = url.openStream();
//从InputStream中解析出图片
bitmap = BitmapFactory.decodeStream(inputStream);
//发消息通知UI组件显示此bitmap对应的图片
handler.sendEmptyMessage(0x123);
inputStream.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}
注意:
URL url = new URL("http://news.c-ps.net/uploads/141024/18-141024152F62P.jpg");
其中http://news.c-ps.net/uploads/141024/18-141024152F62P.jpg是图片地址,而不是显示此图片的网络地址。【右击网络图片】->【复制图片地址】->【得到网络图片的地址】