Android数据与服务器交互的GET,POST,HTTPGET,HTTPPOST的使用
2014-09-13 22:54
627 查看
Android有这几种方式,可以提交数据到服务器,他们是怎么使用的呢,这里我们来探讨一下。
这里的例子用的都是提交客户端的用户名及密码,同时本节用到的StreamTools.readInputStream(is);作用是把输入流转化为字符串,是一个公共类。我在前面介绍过了。/article/5240264.html,这里就不在重复的说明了。
第一种:GET
关键部分是:
首先我们用URL包装访问的路径,由于是get请求,在学习javaWEB的时候我们就以及知道了,路径中要把提交的参数拼装。
然后我们openConnection(),其返回的是HttpURLConnection对象,拿到这个对象我们就可以进行相关参数,及超时的设置,以及获取服务器的返回码了。
example:
第二种:Post
在网页的get与post的请求的对比中我们知道post比get多了几个参数,所以这里我们也是要对相关参数进行设置的,
首先我们要设置的是Content-type与Cotent-Length,
其次,post的方式,实际上是浏览器把数据写给服务器。所以我们要设置相关参数,让服务器允许我们有写的权限,
example:
第三种:httpclientget
使用步骤是,我们首先要实例化一个HttpClient的对象,同时也要实例一个HttpGet的对象,让HttpClient的对象去execute(HttpGet的对象),这时我们可以得到HttpResponse的对象,二者就是服务器的返回的对象,所以区别就在这里了。
这里的返回码是这样获得的response.getStatusLine().getStatusCode();使用中要注意。
example:
第四种:httpclientpost
采用HttpClientPost的方式与HttpClientGet的方式的区别是HttpPost要指定提交的数据实体,而这里的实现就像是map,所以对于参数较多的时候,这种方式是最为便捷的。
example:
作者:Darren
微博:@IT_攻城师
出处:http://www.cnblogs.com/fengtengfei/
这里的例子用的都是提交客户端的用户名及密码,同时本节用到的StreamTools.readInputStream(is);作用是把输入流转化为字符串,是一个公共类。我在前面介绍过了。/article/5240264.html,这里就不在重复的说明了。
第一种:GET
关键部分是:
首先我们用URL包装访问的路径,由于是get请求,在学习javaWEB的时候我们就以及知道了,路径中要把提交的参数拼装。
然后我们openConnection(),其返回的是HttpURLConnection对象,拿到这个对象我们就可以进行相关参数,及超时的设置,以及获取服务器的返回码了。
URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); int code = conn.getResponseCode();
example:
public static String loginByGet(String username,String password) { //提交数据到发服务器 String path = null; try { path = "http://192.168.254.100:8080/web/LoginServlet?username="+URLEncoder.encode(username,"utf-8")+"&password="+password; } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); int code = conn.getResponseCode(); if(code==200){ //请求成功 InputStream is = conn.getInputStream(); String result = StreamTools.readInputStream(is); return result; }else{ return null; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }
第二种:Post
在网页的get与post的请求的对比中我们知道post比get多了几个参数,所以这里我们也是要对相关参数进行设置的,
首先我们要设置的是Content-type与Cotent-Length,
String data = "username="+URLEncoder.encode(username,"utf-8")+"&password="+password; conn.setRequestProperty("Content-Type:", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length:", data.length()+"");
其次,post的方式,实际上是浏览器把数据写给服务器。所以我们要设置相关参数,让服务器允许我们有写的权限,
conn.setDoOutput(true); OutputStream os = conn.getOutputStream(); os.write(data.getBytes());
example:
public static String loginByPost(String username,String password){ //POST提交数据到发服务器 String path = "http://192.168.254.100:8080/web/LoginServlet"; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("POST"); //准备数据,计算其长度 String data = "username="+URLEncoder.encode(username,"utf-8")+"&password="+password; conn.setRequestProperty("Content-Type:", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length:", data.length()+""); //post的方式,实际上是浏览器把数据写给服务器。 //允许写数据 conn.setDoOutput(true); OutputStream os = conn.getOutputStream(); os.write(data.getBytes()); System.out.println("2"); int code = conn.getResponseCode(); if(code==200){ //请求成功 InputStream is = conn.getInputStream(); String result = StreamTools.readInputStream(is); return result; }else{ return null; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }
第三种:httpclientget
使用步骤是,我们首先要实例化一个HttpClient的对象,同时也要实例一个HttpGet的对象,让HttpClient的对象去execute(HttpGet的对象),这时我们可以得到HttpResponse的对象,二者就是服务器的返回的对象,所以区别就在这里了。
这里的返回码是这样获得的response.getStatusLine().getStatusCode();使用中要注意。
example:
public static String loginByHttpClientGet(String username,String password){ try { HttpClient client = new DefaultHttpClient(); String path = "http://192.168.254.100:8080/web/LoginServlet?username=" +URLEncoder.encode(username)+"&password="+URLEncoder.encode(password); HttpGet httpGet = new HttpGet(path); //执行操作。敲回车 HttpResponse response = client.execute(httpGet); int code = response.getStatusLine().getStatusCode(); if(code==200){ //请求成功 InputStream is = response.getEntity().getContent(); String result = StreamTools.readInputStream(is); return result; }else{ return null; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }
第四种:httpclientpost
采用HttpClientPost的方式与HttpClientGet的方式的区别是HttpPost要指定提交的数据实体,而这里的实现就像是map,所以对于参数较多的时候,这种方式是最为便捷的。
List<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("username", username)); parameters.add(new BasicNameValuePair("password", password));
example:
public static String loginByHttpClientPost(String username,String password){ try { HttpClient client = new DefaultHttpClient(); String path = "http://192.168.254.100:8080/web/LoginServlet"; HttpPost post = new HttpPost(); //指定要提交的数据实体 List<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("username", username)); parameters.add(new BasicNameValuePair("password", password)); post.setEntity(new UrlEncodedFormEntity(parameters, "utf-8")); HttpResponse response = client.execute(post); int code = response.getStatusLine().getStatusCode(); if(code==200){ //请求成功 InputStream is = response.getEntity().getContent(); String result = StreamTools.readInputStream(is); return result; }else{ return null; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }
作者:Darren
微博:@IT_攻城师
出处:http://www.cnblogs.com/fengtengfei/
相关文章推荐
- android get或post及HttpClient与服务器数据交互
- 程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成
- Android之使用HttpPost提交数据到服务器(Android手机客户端和后台服务器交互)
- android(21)(使用HttpURLConnection,HttpClient和AsyncHttpClient分别实现post和get请求数据)
- Android使用HttpPost向服务器发送Json数据
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android使用HttpPost向服务器发送Json数据
- android中使用get、post和HttpClient方式与服务器交互
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- android中的http通信---(3)通过get、post方式传数据给服务器
- Android使用HTTP GET、HTTP POST获取网络数据
- jQuery的ajax()检验用户名;通过jQuery的load()/get()/post()方法实现;使用XMLHTTPRequest对象来进行AJAX的异步数据交互
- Android使用HttpPost向服务器发送Json数据
- Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- android 使用get和post将数据提交到服务器
- Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交
- Android利用HttpClient和Post与Get请求与服务器交互