- Android 系统概述
- 什么是智能手机?智能手机操作系统有哪些?
- Android(安卓)是什么?
- Android 系统架构(图解)
- Android 7 新特性介绍
- Android 开发环境搭建
- Android 开发环境搭建教程(图解)
- Android SDK 是什么?
- 通过 Android Studio 创建 Android 应用程序(附带解析)
- Android Studio 的基本调试
- Android 应用程序结构:Android 组件和资源详解
- Android 四大核心组件:Activity+Service+BroadcastReceiver+ContentProvider
- Android Activity 组件及其生命周期
- Android 资源(res 和 assets 目录)
- AndroidManifest.xml 配置文件详解
- Android App Widgets 组件详解
- Android 系统的进程和线程
- Android GUI(图形用户界面)开发
- Android View 和 ViewGroup
- Android 使用 XML 文件定义用户界面
- Android 五大布局:FrameLayout、LinearLayout、AbsoluteLayout、RelativeLayout 和 TableLayout
- Android 创建 Widget 组件实例详解
- Android Button:按钮控件
- Android CheckBox:多选按钮控件
- Android RadioGroup:单选按钮控件
- Android TextView:文本框控件
- Android EditText:编辑框控件
- Android Spinner:下拉列表控件
- Android AutoCompleteTextView:自动完成文本框控件
- Android DatePicker 和 TimePicker:时间日期选择器
- Android ProgressBar:进度条控件
- Android ScrollView:滚动视图
- Android SeekBar:拖动条控件
- Android RatingBar:评价条控件
- Android ImageView 和 ImageButton:图片视图和图片按钮
- Android ImageSwitcher 和 Gallery:图片切换器和图库
- Android GridView:网格视图
- Android Tab:面板标签控件
- Android 菜单:选项菜单+上下文菜单+子菜单
- Android Bitmap(点阵图像、绘制图像)
- Android 对话框(AlertDialog+ProgressDialog)
- Android 多窗口模式(分屏模式)
- Android 事件监听器和回调方法
- Android 电话短信拍照开发教程
- Android Intent:不同应用程序之间通信的桥梁
- Android 实现拨打电话功能
- Android 实现接收短信和发送短信功能
- Android 调用相机进行拍照
- Android 多媒体开发
- Android Service 生命周期和使用方法
- Android BroadcastReceiver:接收广播
- Android 音频以及音频播放器开发实例
- Android MediaRecorder 录制音频
- Android 视频以及视频播放器实例
- Android Camera 相机以及相机程序开发实例
- Android MediaRecorder 录制视频
- Android 数据存储(SQLite 数据库、Android App 数据备份和恢复 )
- Android 数据存储之 SharedPreferences
- Android 数据存储之文件存储
- Android 使用 SQLite 数据库存数数据
- Android SQLite 数据库操作实例
- Android ContentProvider(内容提供者)
- Android 使用系统提供的 ContentProvider
- Android 自定义 ContentProvider
- Android 使用自定义 ContentProvider
- Google App Engine 是什么?
- Android APP 数据备份与恢复
- Android BackupAgent 实现数据备份与恢复
- Android BackupAgentHelper 实现数据备份与恢复
- Android 网络编程
- 借助 GPE 让 Android APP 和 Google App Engine 通信
- Android HTTP 通信
- Android HttpURLConnection 访问互联网资源
- Android 通过 GET 方式获取互联网资源
- Android 通过 POST 方式获取互联网资源
- Android Socket 编程(附带实例)
- Bluetooth(蓝牙)是什么?
- Android 蓝牙通信开发教程(详解版)
- Android 蓝牙通信开发实例演示
- WIFI(无线局域网)是什么?
- Android WiFi 开发(WiFi 编程)实例演示
- WIFI Direct(WIFI 直连)是什么?
- Android WIFI Direct 开发教程
- Android WIFI Direct 开发实例演示
- NFC(近场通信)是什么?
- Android NFC 开发教程
- Android USB 开发简介
- Android USB 附件模式开发教程
- Android USB 主机模式开发教程
- Android SIP 开发教程
- Android 智能传感器
- LBS(位置服务)和 GPS(全球定位系统)简介
- Android LBS 位置服务开发简介
- Android GPS 定位开发教程
- Google Map API 开发简介
- Android Google Map API Key 详细申请步骤
- Google Map API 开发 Android 地图应用实例
- Android 传感器开发教程
- Android 运动传感器开发教程
- Android 位置传感器开发教程
- Android 环境传感器开发教程
- Android 绘图(2D 绘图、3D 绘图)
- Android 2D 绘图开发简介
- Android 使用自定义 View 绘图
- Android 使用 Bitmap 绘图
- Android 使用 SurfaceView 绘制静态图像
- Android 使用 SurfaceView 绘制动态图像
- Android Drawable 开发简介
- Android 3D 绘图开发简介
- Android 硬件加速开发简介
- Android RenderScript 开发简介
- Android App 的国际化和本地化
- Android 国际化和本地化开发简介
- Android 手机区域设置详细步骤
- Android 未本地化应用开发教程
- Android 本地化应用开发教程
- Android 文本输入与复制粘贴
- Android 剪贴板框架开发简介
- Android 剪贴板数据转文本开发简介
- Android 复制粘贴开发教程
- Android Content Provider 复制复杂数据开发教程
- Android 一键复制粘贴开发实例
- Android 企业级应用开发
- Android 设备管理 API 概述
- Android 开发设备管理 API 应用教程
- Android 文本语音 API 开发教程
- Android TV 应用开发教程
- Android TV 应用开发实例
- Android 可穿戴设备应用开发教程
- Android 应用程序的发布
- Android 应用程序发布的步骤
- Android 为什么要为应用程序签名?
- Android 导出未签名应用程序开发步骤
- Android 生成签名文件开发教程
- Android 应用程序签名开发教程
- zipalign 工具优化应用程序开发简介
- Google Play Store( Android Market)发布简介
Android HttpURLConnection 访问互联网资源
为了可以通过 AVD 调试网络访问应用程序,首先在本地计算机上架设网络服务器端。使用 Tomcat 做服务器,在其 webapps 目录下建立 android 目录,并在该目录下建立 message.jsp 文件。
<HTTP> <HEAD> <TITLE>HTTP-MESSAGE</TITLE> </HEAD> <BODY> <% OUT.PRINTln("<H1>Http-Message<BR> Android :Hello World</H>"); %> </BODY> </HTML>
由于本地计算机在网络上的 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 指定的网络资源的基本过程的代码如下:
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(); } }
URLConnection 内建对多种网络协议的支持,如 HTTP/HTTPS、File、FTP 等。
在创建连接之前,可以对连接的一些属性进行设置,如下表所示。
属性名称 | 属性描述 |
---|---|
setReadTimeout(3000) | 设置读取数据的超时时间为 3 秒钟 |
setUseCaches(false) | 设置当前连接是否允许使用缓存 |
setDoOutput(true) | 设置当前连接是否允许建立输出流 |
setDoInput(true) | 设置当前连接是否允许建立输入流 |
HttpURLConnection 继承于 URLConnection 类,二者都是抽象类,所以无法直接实例化,其对象主要通过 URL 的 openConnection 方法获得。
URLConnection 可以直接转换成 HttpURLConnection,以便于使用一些 HTTP 连接特定的方法,如 getResponseMessage()、setRequestMethod() 等。
使用 HttpURLConnection 访问网络资源的基本过程的代码如下:
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(); }
需要注意的是,使用 openConnection 方法所创建的 URLConnection 或者 HttpURLConnection 实例不具有重用性,每次调用 openConnection 方法都将创建一个新的实例。
实例 URLDemo 中演示了使用 URL 访问指定资源的过程,运行效果如图 2 所示。
图 2 URLDemo 的运行效果
实例 URLDemo 中 main.xml 的代码如下:
<?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>
实例 URLDemo 中 AndroidManifest.xml 的代码如下:
<?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>
实例 URLDemo 中 MainActivity.java 的具体实现代码如下:
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 地址即可。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论