使用Post和Get方式传输HTTP参数

关于HTTP传输的介绍请见http://blog.csdn.net/theworldsong/article/details/9107789

以下例子分别以POST和GET方式向网站传输数据,并返回数据将其显示。

package dfzy.EX088;
/*必需引用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; 
import dfzy.EX088.R;
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 EX088 extends Activity 
{ 
  private Button mButton1,mButton2; 
  private TextView mTextView1; 

  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() 
    { 
      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[]储存,这是一种封装,专门用于HTTP的POST传输
        List <NameValuePair> params = new ArrayList <NameValuePair>(); 
        params.add(new BasicNameValuePair("str", "I am Post String")); 
        try 
        { 
          //将要附带的数据包放入,并声明为UTF_8字符格式
          httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 
          //发送后,会等待返回一个HttpResponse
          HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); 
          //若状态码为200
          if(httpResponse.getStatusLine().getStatusCode() == 200)  
          { 
            /*获取字符串*/
            String strResult = EntityUtils.toString(httpResponse.getEntity()); 
            mTextView1.setText(strResult); 
          } 
          else 
          { 
            mTextView1.setText("获取失败"); 
          } 
        } 
        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.dubblogs.cc:8751/Android/Test/API/Post/index.php?str=I+am+Get+String"; 
        /*建立HTTP Get联机*/
        HttpGet httpRequest = new HttpGet(uriAPI); 
        try 
        { 
          /*发送获取的HTTP request,并等待返回一个HttpResponse*/
          HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); 
          /*若状态码为200*/
          if(httpResponse.getStatusLine().getStatusCode() == 200)  
          { 
            //获取字符串
            String strResult = EntityUtils.toString(httpResponse.getEntity());
            mTextView1.setText(strResult); 
          } 
          else 
          { 
            mTextView1.setText("获取失败"); 
          } 
        } 
        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();  
        }  
      } 
    }); 
  }
} 

代码中使用的网站可能失效,但是明白道理就行,就是返回一串字符。

另参考
http://52android.blog.51cto.com/2554429/496621

转载请注明来源,版权归原作者所有,未经同意严禁用于任何商业用途。
微博:http://weibo.com/theworldsong
邮箱:theworldsong@foxmail.com

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值