使用Post和Get方式传输HTTP参数
来源:互联网 发布:视频vip解析接口源码 编辑:程序博客网 时间:2024/05/19 17:27
关于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
- 使用Post和Get方式传输HTTP参数
- http传递参数信息和传递方式post、get
- 判断post和get传输方式
- 判断post和get传输方式
- 网络传输协议(http协议/请求和响应/get方式与post方式)
- HTTP协议传输中GET方式与POST方式区别
- Http GET和Http Post发送方式
- http-使用get和post方式提交数据
- http使用post和get方式提交数据
- java使用post和get方式发送http请求
- http post/get 2种使用方式
- Netty_使用http协议,post&get方式
- HTTP get和post方式的区别
- Http的Get和Post方式详解
- HTTP 请求方式: GET和POST
- 浅谈HTTP 中get 和post方式
- HTTP POST和GET的提交方式
- Http请求get和post方式
- epoll使用详解(精髓)
- 为移动而设计:信息架构篇
- 《浮沉》读书笔记
- java中的线程机制和线程的同步与互斥.
- [OOAD] OOD设计概述
- 使用Post和Get方式传输HTTP参数
- asp 请求两三句
- C100-30 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
- 湖南师范大学第四届大学生计算机程序设计竞赛 记录下水果的三题。。。
- IP头、TCP头、UDP头
- 堆和栈的区别
- ftp安装配置运行(转写)
- 经典设计网站
- 哲理小故事