Android学习 - get和post的简单实现
来源:互联网 发布:手动按摩器小型淘宝 编辑:程序博客网 时间:2024/05/22 03:44
不多说直接代码看看就明白了。
/** * 在Android SDK中提供了Apache HttpClient(org.apache.http.*)模块。 * 在这个模块中涉及到两个重要的类:HttpGet和HttpPost,他们有共性也有不同。 * * HttpGet和HttpPost创建方式相同: * 1、创建HttpGet(或HttpPost)对象,将要请求的URL通过构造方法传入HttpGet(或HttpPost)对象中; * 2、使用DefaultHttpClient类的execute方法发送HTTP GET或HTTP POST 请求,并返回HttpResponse对象; * 3、通过HttpResponse接口的getEntity方法返回响应信息。 * * HttpGet和HttpPost不同点,HttpPost在使用是需要传递参数 ,使用List<NameValuePair>添加参数。 * * @author wolflz * */public class HttpDemo extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}/** * post方式 */public void httPostMethod() {BufferedReader in = null;try {HttpClient client = new DefaultHttpClient();HttpPost request = new HttpPost("http://mysomewebsite.com/services/doSomething.do");List<NameValuePair> postParameters = new ArrayList<NameValuePair>();postParameters.add(new BasicNameValuePair("username", "test"));postParameters.add(new BasicNameValuePair("password", "test1234"));UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);request.setEntity(formEntity);HttpResponse response = client.execute(request);in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));StringBuffer sb = new StringBuffer("");String line = "";String NL = System.getProperty("line.separator");while ((line = in.readLine()) != null) {sb.append(line + NL);}in.close();String result = sb.toString();System.out.println(result);} catch (Exception e) {e.printStackTrace();} finally {if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}}/** * get方式 */public void httpGetMethod() {BufferedReader in = null;try {HttpClient client = new DefaultHttpClient();HttpGet request = new HttpGet("http://www.baidu.com");HttpResponse response = client.execute(request);in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));StringBuffer sb = new StringBuffer("");String line = "";String NL = System.getProperty("line.separator");while ((line = in.readLine()) != null) {sb.append(line + NL);}in.close();String page = sb.toString();System.out.println(page);} catch (Exception e) {e.printStackTrace();} finally {if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}}}
POST和GET区别
1、GET请求的数据会附在URL之后(就是把数据放置在HTTP协议头中),以?分割URL和传输数据,参数之间以&相连,如:login.action?name=hyddd&password=idontknow&verify=%E4%BD%A0%E5%A5%BD。如果数据是英文字母/数字,原样发送,如果是空格,转换为+,如果是中文/其他字符,则直接把字符串用BASE64加密,得出如:%E4%BD%A0%E5%A5%BD,其中%XX中的XX为该符号以16进制表示的ASCII。
POST把提交的数据则放置在是HTTP包的包体中。
2、GET方式提交的数据最多只能是1024字节,理论上POST没有限制,可传较大量的数据。
3、在PHP中,可以用$_GET和$_POST分别获取GET和POST中的数据,而$_REQUEST则可以获取GET和POST两种请求中的数据。
4、很多人贪方便,更新资源时用了GET,因为用POST必须要到FORM(表单),这样会麻烦一点。
5、POST的安全性要比GET的安全性高。注意:这里所说的安全性和上面GET提到的“安全”不是同个概念。上面“安全”的含义仅仅是不作数据修改,而这里安全的含义是真正的Security的含义,比如:通过GET提交数据,用户名和密码将明文出现在URL上。因为1)登录页面有可能被浏览器缓存。2)其他人查看浏览器的历史纪录,那么别人就可以拿到你的账号和密码了,除此之外,使用GET提交数据还可能会造成Cross-site request forgery攻击。
总结一下,Get是向服务器发索取数据的一种请求,而Post是向服务器提交数据的一种请求,在FORM(表单)中,Method默认为"GET",实质上,GET和POST只是发送机制不同,并不是一个取一个发!
- Android学习 - get和post的简单实现
- android的HttpURLConnection实现post和get
- 4.安卓中get和post的简单实现
- android上面的OkHttp的简单get和post demo
- Android Volley框架的简单get和post demo
- c#简单实现GET和POST请求
- Android get/ post 方法 传递网络数据 -简单的翻译功能实现(GET)
- Android 实现Http get 和post操作
- Android使用URLConnection实现post和get
- Get和Post的区别,简单解析。
- 简单的POST请求和get请求
- php的curl实现get和post
- php的curl实现get和post
- php的curl实现get和post
- java实现http的post和get
- 学习笔记 post和get的区别
- Android POST和GET传输的区别
- HttpUrlConnection的简单使用--get和post的简单使用
- 编程算法之试探法
- RubyMine 启动调试出错: permission denied
- fork exec对子进程继承父进程信号处理机制的影响--APUE
- easyui-dialog禁止拖动的办法
- 【Java二十周年】我也是java程序员
- Android学习 - get和post的简单实现
- 我的.NET学习之路一——out、ref、params参数
- The Right Time To Make A Career Change
- android-viewbadger为你的Android app中的view添加角标
- JAVA基础——泛型、一些常用的类
- 关于C语言中的未初始化变量的值
- 学习Java第二天
- [Leetcode] Largest Number
- EasyUI自定义边框分割线样式