Google以网络搜索引擎白手起家,通过创意与不断地研发努力,已经成为网络世界的巨头。而出自网络引擎之手的android平台,与Internet的接轨与应用,自然更不在话下。当人们提到Internet时,首先想到的就是对于HTTP protocol的支持能力,在android SDK中,已经继承了Apache的HttpClient模块。有了这个模块,要写出与HTTP协议相关的程序就不是难事。
要注意的是,在android中,所继承的HttpClient并非是常见的Jakarta Commons HttpClient 3.x(也就是org.apache.commons.httpclient.* package),而是HttpClient 4.0(org.apache.http.*)。
下面的程序介绍如何通过HttpClient模块来创建Http连接,并分别以GET与POST方法来传递参数,连接之后取回Web Server的返回网页结果。
Activity程序代码如下:
public class MainActivity extends Activity
{
private Button button1=null;
private Button button2=null;
private TextView show=null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
//获得组件对象
this.button1=(Button)super.findViewById(R.id.button1);
this.button2=(Button)super.findViewById(R.id.button2);
this.show=(TextView)super.findViewById(R.id.show);
//设置按钮监听
this.button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
//声明网址字符串
String uriStr="http://www.dubblogs.cc:8751//Android/Test/API/Post/index.php";
//创建HTTP post连接
HttpPost httpRequest=new HttpPost(uriStr);
//Post运行传送变量必须用NameValuePair[]数组封装
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("str","I am a Post string"));
//发出HTTP Request
try
{
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//取得HTTP Response
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//状态码为200代表OK
if(httpResponse.getStatusLine().getStatusCode()==200)
{
//取出应答字符串
String strResult=EntityUtils.toString(httpResponse.getEntity());
MainActivity.this.show.setText(strResult);
}
else
{
MainActivity.this.show.setText("Error Response: "+httpResponse.getStatusLine().toString());
}
}
catch (ClientProtocolException e)
{
MainActivity.this.show.setText(e.getMessage().toString());
e.printStackTrace();
}
catch(IOException e)
{
MainActivity.this.show.setText(e.getMessage().toString());
e.printStackTrace();
}
catch(Exception e)
{
MainActivity.this.show.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
this.button2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
//声明网址字符串
String uriStr="http://www.dubblogs.cc:8751/Android/Test/API/Get/index.php?str=I+am+Get+string";
//创建HTTP Get连接
HttpGet httpRequest=new HttpGet(uriStr);
try
{
//发出Http Request并获得Response
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//若状态码为200
if(httpResponse.getStatusLine().getStatusCode()==200)
{
//取出应答字符串
String strResult=EntityUtils.toString(httpResponse.getEntity());
//删除多余字符
//strResult=eregi_
MainActivity.this.show.setText(strResult);
}
else
{
MainActivity.this.show.setText("Error Response: "+httpResponse.getStatusLine().toString());
}
}
catch (ClientProtocolException e)
{
MainActivity.this.show.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (IOException e)
{
MainActivity.this.show.setText(e.getMessage().toString());
e.printStackTrace();
}
catch(Exception e)
{
MainActivity.this.show.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
}
}
页面布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="40sp"
android:textSize="20sp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="以post取回网站数据" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="以get取回网站数据" />
</LinearLayout>
注意,为了能让程序顺利连接网络,我们需要在Manifest.xml文件中添加权限:
<uses-permission
android:name="android.permission.INTERNET" />
除了上面提到的之外,我们还应该注意一下几点:
- Post运行传送变量必须用NameValuePair[]数组封装
- 状态码200代表找到资源或接收到返回的信息
- 注意try/catch语句中各个catch语句的顺序,非常重要