本文出自:http://blog.csdn.net/zhongnan09/article/details/6565384
在AndroidSDK中,有很多包都支持网络编程,下面列举了Android中集成的网络编程相关的包:
包 | 描述 |
java.net | 提供与联网有关的类,包括流和数据包(datagram)sockets、Internet 协议和常见 HTTP 处理。该包是一个多功能网络资源。有经验的 Java 开发人员可以立即使用这个熟悉的包创建应用程序。 |
java.io | 虽然没有提供显式的联网功能,但是仍然非常重要。该包中的类由其他 Java 包中提供的 socket 和连接使用。它们还用于与本地文件(在与网络进行交互时会经常出现)的交互。 |
java.nio | 包含表示特定数据类型的缓冲区的类。适合用于两个基于 Java 语言的端点之间的通信。 |
org.apache.* | 表示许多为 HTTP 通信提供精确控制和功能的包。可以将 Apache 视为流行的开源 Web 服务器。 |
android.net | 除核心 java.net.* 类以外,包含额外的网络访问 socket。该包包括 URI 类,后者频繁用于 Android 应用程序开发,而不仅仅是传统的联网方面。 |
android.net.http | 包含处理 SSL 证书的类。 |
android.net.wifi | 包含在 Android 平台上管理有关 WiFi(802.11 无线 Ethernet)所有方面的类。并不是所有设备都配备了 WiFi 功能,特别是 Android 在 Motorola 和 LG 等手机制造商的 “翻盖手机” 领域获得了成功。 |
android.telephony.gsm | 包含用于管理和发送 SMS(文本)消息的类。一段时间后,可能会引入额外的包来来为非 GSM 网络提供类似的功能,比如 CDMA 或 android.telephony.cdma 等网络。 |
已经集成了Apache的HttpClient模块,在这一节中,我们练习如何应用HttpClient创建连接并从网络上获取数据。
下面是Demo:
首先新建Android工程,并建立启动Activity:
MainActivity.java:
package com.xzq.httpclient;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
private Button mButton1, mButton2;
private TextView mTextView1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.myButton1);
mButton2 = (Button) findViewById(R.id.myButton2);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mButton1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
String uriAPI = "https://passport.sohu.com/web/dispatchAction.action";
//使用post方式
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("str", "I am Post String"));
try
{
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
} else
{
mTextView1.setText("响应错误: "
+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
mButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
String uriAPI = "http://www.baidu.com";
//使用get方式
HttpGet httpRequest = new HttpGet(uriAPI);
try
{
HttpResponse httpResponse = new DefaultHttpClient()
.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
} else
{
mTextView1.setText("错误消息: "
+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
}
}
Post方式的效果:
get方式效果如下:
要注意在main.xml文件中给TextView加上ScrollView标签,否则,当页面数据比较大时,不能出现滚动条,影响使用。另外,同样要在AndroidManifest.xml文件中添加访问网络的权限。