[转帖]在 Android 应用程序中使用 Internet 数据_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2890 | 回复: 0   主题: [转帖]在 Android 应用程序中使用 Internet 数据        下一篇 
shuangqiang.xing
注册用户
等级:上尉
经验:737
发帖:64
精华:0
注册:2013-10-30
状态:离线
发送短消息息给shuangqiang.xing 加好友    发送短消息息给shuangqiang.xing 发消息
发表于: IP:您无权察看 2013-11-4 9:18:29 | [全部帖] [楼主帖] 楼主

北京联动北方科技有限公司

Android 应用程序必须访问位于 Internet 上的数据,而 Internet 数据可以有几种不同的格式。本文将介绍在 Android 应用程序中如何使用三种数据格式:

  • XML
  • JSON
  • Google 的 protocol buffers

首先开发一个 Web 服务,将 CSV 数据转换成 XML、JSON 和 protocol-buffers 格式。然后构建一个样例 Android 应用程序,可以从 Web 服务中以任何一种格式提取数据并将其解析并显示给用户。

要进行本文中的练习,您需要最新的 Android SDK(参见 参考资料)和 Android 2.2 平台。SDK 还要求您安装一个 Java™ 开发包(JDK);本文中使用了 JDK 1.6.0_17。您不需要有 Android 物理设备;所有代码都将在 SDK 的 Android 仿真器中运行。本文并没有教您如何进行 Android 开发,因此建议您熟悉 Android 编程。当然,只凭借 Java 编程语言的知识也可以完成本文的学习。

您还需要一个 Java web 应用程序服务器来运行 Android 应用程序使用的 Web 服务。此外,也可以将服务器端代码部署到 Google App Engine。参见 下载 部分获得完整的源代码。

Day Trader 应用程序

您将开发一个简单的 Android 应用程序,叫做 Day Trader。Day Trader 允许用户输入一个或更多的股票代码并获取其所代表股票的最新价格信息。用户可以指定数据使用的格式:XML、JSON 或 protocol buffers。实际的 Android 应用程序通常不会提供此选择,但是通过实现此功能,您可以了解如何让您的应用程序处理每一种格式。图 1 展示了 Day Trader 用户界面:

 

     

文本框及其旁边的 Add Stock 按钮允许用户输入感兴趣的每支股票的代码。用户按下 Download Stock Data 按钮后,会从服务器请求所有这些股票的数据,在应用程序中解析并显示在屏幕上。默认情况下,获取的是 XML 数据。通过菜单,您可以在 XML、JSON 或 protocol buffers 数据格式间切换。

清单 1 显示用于创建 图 1 中所示 UI 的布局 XML:


清单 1. Day Trader 布局 XML

           

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <LinearLayout android:orientation="horizontal" 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="wrap_content">
  10.         <EditText android:id="@+id/symbol" android:layout_width="wrap_content"
  11.             android:layout_height="wrap_content" android:width="120dip"/>
  12.         <Button android:id="@+id/addBtn" android:layout_width="wrap_content"
  13.             android:layout_height="wrap_content" 
  14.             android:text="@string/addBtnLbl"/>
  15.     </LinearLayout>
  16.     <LinearLayout android:orientation="horizontal" 
  17.         android:layout_width="fill_parent" 
  18.         android:layout_height="wrap_content">
  19.         <TextView android:layout_width="wrap_content" 
  20.             android:layout_height="wrap_content" android:id="@+id/symList" />
  21.         <Button android:id="@+id/dlBtn" android:layout_width="wrap_content" 
  22.             android:layout_height="wrap_content" 
  23.             android:text="@string/dlBtnLbl"
  24.         />
  25.        </LinearLayout>
  26.     <ListView android:id="@android:id/list" 
  27.         android:layout_height="fill_parent" android:layout_width="fill_parent"
  28.         android:layout_weight="1"
  29.         />
  30. </LinearLayout>

     清单 1 中的大部分代码都简单明了。可以看到几个小部件创建了 图 1 所示的输入和按钮。还会看到一个 ListView,Android 小部件中真正的瑞士军刀。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论