HttpURLConnection访问互联网资源 图片看不了?点击切换HTTP 返回上层
为了可以通过 AVD 调试网络访问应用程序,首先在本地计算机上架设网络服务器端。使用 Tomcat 做服务器,在其 webapps 目录下建立 android 目录,并在该目录下建立 message.jsp 文件。
由于本地计算机在网络上的 IP 为 175.168.35.198,因此 message.jsp 的网络 URL 为 http://175.168.35.198:8080/android/message.jsp。将该地址输入 IE 地址栏打开,其运行效果如图 1 所示。

图 1 message.jsp 的运行效果
这样,我们就有了可以通过 AVD 来访问的网络上的资源。
使用 java.net.URLConnection 访问 URL 指定的网络资源的基本过程的代码如下:
URLConnection 内建对多种网络协议的支持,如 HTTP/HTTPS、File、FTP 等。
在创建连接之前,可以对连接的一些属性进行设置,如下表所示。
HttpURLConnection 继承于 URLConnection 类,二者都是抽象类,所以无法直接实例化,其对象主要通过 URL 的 openConnection 方法获得。
URLConnection 可以直接转换成 HttpURLConnection,以便于使用一些 HTTP 连接特定的方法,如 getResponseMessage()、setRequestMethod() 等。
使用 HttpURLConnection 访问网络资源的基本过程的代码如下:
需要注意的是,使用 openConnection 方法所创建的 URLConnection 或者 HttpURLConnection 实例不具有重用性,每次调用 openConnection 方法都将创建一个新的实例。
实例 URLDemo 中演示了使用 URL 访问指定资源的过程,运行效果如图 2 所示。

图 2 URLDemo 的运行效果
实例 URLDemo 中 main.xml 的代码如下:
实例 URLDemo 中 AndroidManifest.xml 的代码如下:
实例 URLDemo 中 MainActivity.java 的具体实现代码如下:
以上代码中,
1 2 3 4 5 6 7 8 9 10 | <HTTP> <HEAD> <TITLE>HTTP-MESSAGE</TITLE> </HEAD> <BODY> <% OUT .PRINTln( "<H1>Http-Message<BR>Android:Hello World</H>" ); %> </BODY> </HTML> |

图 1 message.jsp 的运行效果
使用 java.net.URLConnection 访问 URL 指定的网络资源的基本过程的代码如下:
1 2 3 4 5 6 7 8 9 10 | URL url=new URL( "ftp://mirror.csclub.uwaterloo.ca/index.html" );//建立URL URLConnection urlConnection=url.openConnection();//打开连接 InputStream in =new BufferedInputStream( urlConnection. getlnputStream());//从连接建立输入流 try { readstream ( in ); //读取数据操作 }finally{ in . close (); } } |
在创建连接之前,可以对连接的一些属性进行设置,如下表所示。
属性名称 | 属性描述 |
---|---|
setReadTimeout(3000) | 设置读取数据的超时时间为 3 秒钟 |
setUseCaches(false) | 设置当前连接是否允许使用缓存 |
setDoOutput(true) | 设置当前连接是否允许建立输出流 |
setDoInput(true) | 设置当前连接是否允许建立输入流 |
HttpURLConnection 继承于 URLConnection 类,二者都是抽象类,所以无法直接实例化,其对象主要通过 URL 的 openConnection 方法获得。
URLConnection 可以直接转换成 HttpURLConnection,以便于使用一些 HTTP 连接特定的方法,如 getResponseMessage()、setRequestMethod() 等。
使用 HttpURLConnection 访问网络资源的基本过程的代码如下:
1 2 3 4 5 6 7 8 | 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(); } |
实例 URLDemo 中演示了使用 URL 访问指定资源的过程,运行效果如图 2 所示。

图 2 URLDemo 的运行效果
实例 URLDemo 中 main.xml 的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version= "1.0” encoding=" utf-8 "?> <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:orientation=" vertical " android:layout_width=" fill_parent " android:layout_height=" fill_parent "> <Button android:id=" @+id/Button_HTTP " android:layout_width=" fill_parent " android:layout_height=" wrap_content " android:text=" @string/button_name01 "/> <TextView android:id=" @+id/TextView_HTTP " android:layout_width=" fill_parent " android:layout_height=" wrap_content"/> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?xml version= "l.0" encoding= "utf-8" ?> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "com.android.activity" android:versionCode= "1" android:versionName= "1.0" > <uses-sdk android:minSdkVersion= "4" /> <uses-permission android: name = "android.permission.INTERNET" /> <application android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" > <activity android: name = ".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> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | package introdction.android.URLDemo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputstreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import com.android.activity.R; import android.app.Activity; import android.os.Bundle; import android. view . View ; import android. view . View .OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { /** Called when the activity is first created. */ private TextView textView_HTTP; @Override public void onCreate (Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.main); textView_HTTP= (TextView) findViewById (R.id.TextView_HTTP); Button button_http= (Button) findViewById (R.id.Button_HTTP) ; button_http. setOnClickListener (new OnClickListener () { //给button_http按钮设置监听器 public void onClick ( View v) {//事件处理 String httpUrl= "http://175.168.35.198:8080/android/message.jsp" ; String resultData= "" ;//定义一个resultData用于存储获得的数据 URL url= null ; //定义URL对象 try { url=new URL (httpUrl); //构造一个URL对象时需要使用异常处理 } catch (MalformedURLException e) { System. out .println (e.getMessage ());//打印出异常信息 } if (url != null ) {//如果URL不为空时 try{ //有关网络操作时,需要使用异常处理 HttpURLConnection urlConn= (HttpURLConnection)url.openConnection (); //使用HttpURLConnection打开连接 InputStreamReader in =new InputStreamReader (urlConn.getInputStream());//得到读取的内容 BufferedReader buffer=new BufferedReader ( in );//为输出创建BufferedReader String inputLine= null ; while (((inputLine=buffer.readLine()) != null )) { // 读取获得的数据 resultData+=inputLine+ "\n" ; // 加上 "\n" 实现换行 } in . close ();//关闭InputStreamReader urlConn.disconnect(); //关闭HTTP连接 if (resultData != null ) {//如果获取到的数据不为空 textView_HTTP.setText(resultData) ; } else { textView_HTTP.setText( "Sorry,the content is null" );//获取到的数据为空时显示 } } catch (IOException e) { textView_HTTP.setText (e.getMessage()); //出现异常时,打印异常信息 } } else { textView_HTTP.setText ( "url is null" ); //当url为空时输出 } } }) ; } |
String httpUrl = "http://175.168.35.198:8080/android/message.jsp ";
指定了要访问的网络资源的地址,测试时改成自己本机的IP地址即可。