华拓科技网
您的当前位置:首页HttpPostGet传参--http连接示范

HttpPostGet传参--http连接示范

来源:华拓科技网
Http Post/Get 传参--http连接示范 2010-04-07 13:16

1.分别使用HttpPost对象与HttpGet对象来发出HttpRequest,其中注意的事post运行,传递变量必须使用NameValuePair[]数组存储,通过HttpRequest.setEntity()方法来发送HTTP请求.

2.此外,也必须通过 DafaultHttpClient().execute(httpRequest)添加

HttpResponse对象来接收WebServer的回复,在通过HttpResponse.getEntity取出回复信息,最后将网页的运行结果取回,完成http连接程序. package com.test;

import java.io.IOException; import java.util.ArrayList; import java.util.List;

import java.util.regex.Matcher; import java.util.regex.Pattern;

import org.apache.http.HttpRequest; 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 android.app.Activity; import android.os.Bundle; import android.view.View;

import android.widget.Button; import android.widget.TextView; public class test extends Activity { private Button get; private Button post;

private TextView textview;

/** Called when the activity is first created. */ @Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.main);

get = (Button) findViewById(R.id.get); post = (Button) findViewById(R.id.post);

textview = (TextView) findViewById(R.id.textview); get.setOnClickListener(getButtonOnClickListener); post.setOnClickListener(postButtonOnClickListener); }

private Button.OnClickListener postButtonOnClickListener = new Button.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub // 声明网址字符串 String uriAPI =

\"http://www.dubblogs.cc:8751/Android/Test/API/Post/index.php\"; /* 创建http post连接 */

HttpPost httpRequest=new HttpPost(uriAPI); /*

* post运行,传递变量必须使用NameValuePair[]数组存储,通过HttpRequest.setEntity()方法来发送HTTP请求. */

List params=new ArrayList(); params.add(new BasicNameValuePair(\"str\我是post请求\")); try {

/* 发送HTTP request */ 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()); textview.setText(strResult); }else{

textview.setText(\"Error Response: \"

+ httpResponse.getStatusLine().toString());

}

} catch (Exception e) { // TODO: handle exception e.printStackTrace(); }

} };

private Button.OnClickListener getButtonOnClickListener = new Button.OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub // TODO Auto-generated method stub /*声明网址字符串*/ String uriAPI =

\"http://www.dubblogs.cc:8751/Android/Test/API/Get/index.php?str=I+am+Get+String\";

/*创建HTTP Get连接*/

HttpGet httpRequest = new HttpGet(uriAPI); try {

/*发送 HTTP request */

HttpResponse httpResponse = new DefaultHttpClient() .execute(httpRequest); /* 获取状态码为 200 ok */

if (httpResponse.getStatusLine().getStatusCode() == 200) { /*取出答应字符串*/

String strResult = EntityUtils.toString(httpResponse .getEntity()); /*删除多余字符串*/

strResult = eregi_replace(\"(\\r\\n|\\r|\\n|\\n\\r)\ strResult);

textview.setText(strResult); } else {

textview.setText(\"Error Response: \"

+ httpResponse.getStatusLine().toString()); }

} catch (ClientProtocolException e) {

textview.setText(e.getMessage().toString());

e.printStackTrace();

} catch (IOException e) {

textview.setText(e.getMessage().toString()); e.printStackTrace(); } catch (Exception e) {

textview.setText(e.getMessage().toString()); e.printStackTrace(); } }

};

/*自定义字符串替换函数*/

public String eregi_replace(String strFrom, String strTo, String strTarget) {

// TODO Auto-generated method stub String strPattern = \"(?i)\"+strFrom;

Pattern p = Pattern.compile(strPattern); Matcher m = p.matcher(strTarget); if(m.find()) {

return strTarget.replaceAll(strFrom,strTo); } else {

return strTarget; } } }

一般来说,要开发Android与Internet连接的程序,都是通过Java.net中的 HttpURLConnection,以及org.apache.commons.httpclient。中的HttpClient来实现Http连接。其实在Android SDK中,还有几个类似的API可以使用,列举如下:

Android.net.http.*的AndroidHttpClient(主要使用Apache HttpClient);

com.google.Android.net.* 的GoogleHttpClient(主要使用AndroidHttpClient).

其中GoogleHttpClient是地图程序(MapActivity)在后台的主要Http Request 引擎,另外,http安装应用程序的CheckinService,也是用GoogleHttpClient.

因篇幅问题不能全部显示,请点此查看更多更全内容