作者:项文超
此示例是一个简单的网络示例。演示了android连接到网络是多么简单,将展示如何从web页面发送文本。下面提供了示例所需的代码。
1、首先来看UI部分:
EditText 让用户能够进入一个 Web 页面。
使用一个按钮告诉程序取回 Web 页面文本。
检索回数据后,它将显示在 TextView 中。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <EditText
- android:id="@+id/address"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="textPostalAddress" >
- <requestFocus />
- </EditText>
- <Button
- android:id="@+id/buttonGet"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/buttonName" />
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#ffffff"
- android:textColor="#000000"
- android:text="@string/name" />
- </LinearLayout>
2、android SDK网络包介绍:
包 | 描述 |
java.net | 提供与联网有关的类,包括流和数据包(datagram)sockets、Internet 协议和常见 HTTP 处理。该包是一个多功能网络资源。有经验的 Java 开发人员可以立即使用这个熟悉的包创建应用程序。 |
java.io | 虽然没有提供显式的联网功能,但是仍然非常重要。该包中的类由其他 Java 包中提供的 socket 和连接使用。它们还用于与本地文件(在与网络进行交互时会经常出现)的交互。 |
GetWebPageActivity.java文件代码如下:
- package ss.pku.edu.cn;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.EditText;
- import android.view.View;
- import android.os.Message;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- public class GetWebPageActivity extends Activity {
- /** Called when the activity is first created. */
- public Handler handler;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- final EditText eText = (EditText) findViewById(R.id.address);
- final TextView tView = (TextView) findViewById(R.id.pageText);
- this.handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- // process incoming messages here
- switch (msg.what) {
- case 0:
- tView.append((String) msg.obj);
- break;
- }
- super.handleMessage(msg);
- }
- };
- final Button button = (Button) findViewById(R.id.buttonGet);
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- try{
- tView.setText("");
- // Perform action on click
- URL url = new URL(eText.getText().toString());
- URLConnection conn = url.openConnection();
- // Get the response
- BufferedReader rd = new BufferedReader(new
- InputStreamReader(conn.getInputStream()));
- String line = "";
- Message lmsg;
- lmsg = new Message();
- String allline="";
- while ((line = rd.readLine()) != null) {
- allline+=line;
- }
- lmsg.obj= allline;
- lmsg.what = 0;
- GetWebPageActivity.this.handler.sendMessage(lmsg);
- }
- catch (Exception e){
- }
- }
- });
- }
- }
这些代码可以分解成一些常见的部分。这里使用一些重要(必需)的导入语句来恰当地引用 UI、数据传递以及应用程序中使用的与网络有关的类。所有与网络相关的代码出现在 OnClickListener 的 OnClick 方法中。在选择标签为Get 的按钮之后调用这些代码。
URL 和 URLConnection 类共同提供与用户所选的Web 站点的连接。BufferedReader 的一个实例负责从 Web 站点连接中读取传入的数据。每读取一行代码,文本就被附加到一个 TextView。数据并没有直接指定给 TextView(但是在本例中可以)。我们引入了一种设计模式,即创建一个消息对象并将该对象发送到一个处理程序的实例。这是更新 UI 的一种比较可取的方法,对可能需要同时运行多个线程的应用程序而言尤其如此。
运行程序后,在地址栏输入http://developer.android.com,得到网页数据,页面如下:
原文地址:点击打开链接