【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1562|回复: 0

Android http get/post传递参数

[复制链接]
发表于 2013-1-15 23:05:53 | 显示全部楼层 |阅读模式
本程序介绍如何通过HttpClient模块来创建Http连接,并分别以Http Get和Post方法传递参数,连接之后取回web server的返回网页结果。     注意,在用Post时,传递变量必须用NameValuePais[]数组存储,通过HttpRequest.setEntity()方法来发出http请求。
     此外,也必须通过DefaultHttpClient().execute(httpRequest)添加HttpRequest对象来接收web server的回复,在通过httpResponse.getEntity()取出回复信息。
  1. /*必需引用apache.http相关类别来建立HTTP联机*/
  2. import org.apache.http.HttpResponse;
  3. import org.apache.http.NameValuePair;
  4. import org.apache.http.client.ClientProtocolException;
  5. import org.apache.http.client.entity.UrlEncodedFormEntity;
  6. import org.apache.http.client.methods.HttpGet;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.impl.client.DefaultHttpClient;
  9. import org.apache.http.message.BasicNameValuePair;
  10. import org.apache.http.protocol.HTTP;
  11. import org.apache.http.util.EntityUtils;
  12. /*必需引用java.io 与java.util相关类来读写文件*/
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.regex.Matcher;
  17. import java.util.regex.Pattern;

  18. import android.app.Activity;
  19. import android.os.Bundle;
  20. import android.view.View;
  21. import android.widget.Button;
  22. import android.widget.TextView;

  23. public class EX08_01 extends Activity
  24. {
  25.   /*声明两个Button对象,与一个TextView对象*/
  26.   private Button mButton1,mButton2;
  27.   private TextView mTextView1;
  28.    
  29.   /** Called when the activity is first created. */
  30.   @Override
  31.   public void onCreate(Bundle savedInstanceState)
  32.   {
  33.     super.onCreate(savedInstanceState);
  34.     setContentView(R.layout.main);
  35.      
  36.     /*透过findViewById建构子建立TextView与Button对象*/
  37.     mButton1 =(Button) findViewById(R.id.myButton1);
  38.     mButton2 =(Button) findViewById(R.id.myButton2);
  39.     mTextView1 = (TextView) findViewById(R.id.myTextView1);
  40.      
  41.     /*设定OnClickListener来聆听OnClick事件*/
  42.     mButton1.setOnClickListener(new Button.OnClickListener()
  43.     {
  44.       /*重写onClick事件*/
  45.       @Override
  46.       public void onClick(View v)
  47.       {
  48.         /*声明网址字符串*/
  49.         String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/Post/index.php";
  50.         /*建立HTTP Post联机*/
  51.         HttpPost httpRequest = new HttpPost(uriAPI);
  52.         /*
  53.          * Post运作传送变量必须用NameValuePair[]数组储存
  54.         */
  55.         List <NameValuePair> params = new ArrayList <NameValuePair>();
  56.         params.add(new BasicNameValuePair("str", "I am Post String"));
  57.         try
  58.         {
  59.           /*发出HTTP request*/
  60.           httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
  61.           /*取得HTTP response*/
  62.           HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
  63.           /*若状态码为200 ok*/
  64.           if(httpResponse.getStatusLine().getStatusCode() == 200)  
  65.           {
  66.             /*取出响应字符串*/
  67.             String strResult = EntityUtils.toString(httpResponse.getEntity());
  68.             mTextView1.setText(strResult);
  69.           }
  70.           else
  71.           {
  72.             mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
  73.           }
  74.         }
  75.         catch (ClientProtocolException e)
  76.         {  
  77.           mTextView1.setText(e.getMessage().toString());
  78.           e.printStackTrace();
  79.         }
  80.         catch (IOException e)
  81.         {  
  82.           mTextView1.setText(e.getMessage().toString());
  83.           e.printStackTrace();
  84.         }
  85.         catch (Exception e)
  86.         {  
  87.           mTextView1.setText(e.getMessage().toString());
  88.           e.printStackTrace();  
  89.         }  
  90.          
  91.       }
  92.     });
  93.     mButton2.setOnClickListener(new Button.OnClickListener()
  94.     {
  95.       @Override
  96.       public void onClick(View v)
  97.       {
  98.         // TODO Auto-generated method stub
  99.         /*声明网址字符串*/
  100.         String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/Get/index.php?str=I+am+Get+String";
  101.         /*建立HTTP Get联机*/
  102.         HttpGet httpRequest = new HttpGet(uriAPI);
  103.         try
  104.         {
  105.           /*发出HTTP request*/
  106.           HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
  107.           /*若状态码为200 ok*/
  108.           if(httpResponse.getStatusLine().getStatusCode() == 200)  
  109.           {
  110.             /*取出响应字符串*/
  111.             String strResult = EntityUtils.toString(httpResponse.getEntity());
  112.             /*删除多余字符*/
  113.             strResult = eregi_replace("(/r/n|/r|/n|/n/r)","",strResult);
  114.             mTextView1.setText(strResult);
  115.           }
  116.           else
  117.           {
  118.             mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
  119.           }
  120.         }
  121.         catch (ClientProtocolException e)
  122.         {  
  123.           mTextView1.setText(e.getMessage().toString());
  124.           e.printStackTrace();
  125.         }
  126.         catch (IOException e)
  127.         {  
  128.           mTextView1.setText(e.getMessage().toString());
  129.           e.printStackTrace();
  130.         }
  131.         catch (Exception e)
  132.         {  
  133.           mTextView1.setText(e.getMessage().toString());
  134.           e.printStackTrace();  
  135.         }  
  136.       }
  137.     });
  138.   }
  139.     /* 自定义字符串取代函数 */
  140.     public String eregi_replace(String strFrom, String strTo, String strTarget)
  141.     {
  142.       String strPattern = "(?i)"+strFrom;
  143.       Pattern p = Pattern.compile(strPattern);
  144.       Matcher m = p.matcher(strTarget);
  145.       if(m.find())
  146.       {
  147.         return strTarget.replaceAll(strFrom, strTo);
  148.       }
  149.       else
  150.       {
  151.         return strTarget;
  152.       }
  153.     }
  154. }

复制代码
在androidManifest.xml中必须添加权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-5-6 19:49

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表