转载:

http://blog.csdn.net/flying_tao/article/details/6553601

本程序介绍如何通过HttpClient模块来创建Http连接,并分别以Http Get和Post方法传递参数,连接之后取回web server的返回网页结果。

注意,在用Post时,传递变量必须用NameValuePais[]数组存储,通过HttpRequest.setEntity()方法来发出http请求。

此外,也必须通过DefaultHttpClient().execute(httpRequest)添加HttpRequest对象来接收web server的回复,在通过httpResponse.getEntity()取出回复信息

/*必需引用apache.http相关类别来建立HTTP联机*/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; /*必需引用java.io 与java.util相关类来读写文件*/import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher;import java.util.regex.Pattern;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class EX08_01 extends Activity {   /*声明两个Button对象,与一个TextView对象*/  private Button mButton1,mButton2;   private TextView mTextView1;      /** Called when the activity is first created. */   @Override   public void onCreate(Bundle savedInstanceState)   {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);          /*透过findViewById建构子建立TextView与Button对象*/     mButton1 =(Button) findViewById(R.id.myButton1);     mButton2 =(Button) findViewById(R.id.myButton2);    mTextView1 = (TextView) findViewById(R.id.myTextView1);          /*设定OnClickListener来聆听OnClick事件*/    mButton1.setOnClickListener(new Button.OnClickListener()     {       /*重写onClick事件*/      @Override       public void onClick(View v)       {         /*声明网址字符串*/        String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/Post/index.php";        /*建立HTTP Post联机*/        HttpPost httpRequest = new HttpPost(uriAPI);         /*         * Post运作传送变量必须用NameValuePair[]数组储存        */        List <NameValuePair> params = new ArrayList <NameValuePair>();         params.add(new BasicNameValuePair("str", "I am Post String"));         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());             mTextView1.setText(strResult);           }           else           {             mTextView1.setText("Error Response: "+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)       {         // 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);            mTextView1.setText(strResult);           }           else           {             mTextView1.setText("Error Response: "+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();          }        }     });   }    /* 自定义字符串取代函数 */    public String eregi_replace(String strFrom, String strTo, String strTarget)    {      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;      }    }} 

在androidManifest.xml中必须添加权限:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

更多相关文章

  1. Android(安卓)studio配置输出的apk文件名
  2. android之JSON解析(二)
  3. Android(安卓)drawable 可绘制资源总结
  4. Android的HTTP协议
  5. Android的国际化与本地化
  6. TextView组件改变部分文字的颜色
  7. Android(安卓)Service的两种启动方式,你知道吗?
  8. Android的Window底层原理
  9. Android(安卓)listView+CheckBox的实现

随机推荐

  1. android studio更改module名字
  2. Android实现简单短信发送器
  3. AndroidX迁移——弃用support库指南
  4. android 图片设置圆角
  5. Android之fragment的两种创建方式
  6. 【黑马Android】(02)短信发送器/布局演示
  7. Android原生应用的CTS测试步骤
  8. Android RecyclerView添加分隔线 Divider
  9. Android SDK Android NDK 官方下载地址
  10. Android Timer编写方式