当前位置:操作系统 > 安卓/Android >>

android中用GET和POST的方法向服务器上传数据

两者的区别如下:

GET上传的数据一般是很小的并且安全性能不高的数据, 而POST上传的数据适用于数据量大,数据类型复杂,数据安全性能要求高的地方

GET和POST的使用方法一般如下:

1.采用GET方式向服务器传递数据的步骤

1.利用Map集合对数据进行获取并进行数据处理
if (params!=null&&!params.isEmpty()) {
 for (Map.Entry<String, String> entry:params.entrySet()) {
   
  sb.append(entry.getKey()).append("=");
  sb.append(URLEncoder.encode(entry.getValue(),encoding));
  sb.append("&");
   }
  sb.deleteCharAt(sb.length()-1);
  
  }
 
2.新建一个StringBuilder对象
  sb=new StringBuilder()
3.新建一个HttpURLConnection的URL对象,打开连接并传递服务器的path
connection=(HttpURLConnection) new URL(path).openConnection();
4.设置超时和连接的方式

 connection.setConnectTimeout(5000);
  connection.setRequestMethod("GET");


2.采用POST方式向服务器传递数据的步骤
 

1.利用Map集合对数据进行获取并进行数据处理
if (params!=null&&!params.isEmpty()) {
 for (Map.Entry<String, String> entry:params.entrySet()) {
   
  sb.append(entry.getKey()).append("=");
  sb.append(URLEncoder.encode(entry.getValue(),encoding));
  sb.append("&");
   }
  sb.deleteCharAt(sb.length()-1);
  
  }
 
2.新建一个StringBuilder对象,得到POST传给服务器的数据
  sb=new StringBuilder()
  byte[] data=sb.toString().getBytes();
3.新建一个HttpURLConnection的URL对象,打开连接并传递服务器的path
connection=(HttpURLConnection) new URL(path).openConnection();
4.设置超时和允许对外连接数据
connection.setDoOutput(true);
5.设置连接的setRequestProperty属性

connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
  connection.setRequestProperty("Content-Length", data.length+"");
6.得到连接输出流
outputStream =connection.getOutputStream();
7.把得到的数据写入输出流中并刷新
 outputStream.write(data);
 outputStream.flush();


 

3.具体实现的过程如下:
1.使用GET方法上传数据
服务器中doGet方法中的代码如下:

 

[java] view plaincopyprint?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
         
        String name =request.getParameter("name"); 
        String age=request.getParameter("age"); 
        System.out.println("--------name:"+name); 
        System.out.println("--------age:"+age); 
         
    } 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  String name =request.getParameter("name");
  String age=request.getParameter("age");
  System.out.println("--------name:"+name);
  System.out.println("--------age:"+age);
  
 }
在客户端实现的代码如下:

 

[java] view plaincopyprint?public class UserSerivce { 
 
    public static boolean save(String getname, String getage) throws Exception { 
 
        String path = "http://10.254.1.62/WebForGETMethod/ServletForGetMethod"; 
 
        Map<String, String> params = new HashMap<String, String>(); 
        params.put("name", getname); 
        params.put("age", getage); 
 
        return sendGETRequest(path, params, "UTF-8"); 
    } 
 
    private static boolean sendGETRequest(String path, 
            Map<String, String> params, String encoding) throws Exception { 
 
        StringBuilder sb = new StringBuilder(path); 
        if (params != null && !params.isEmpty()) { 
            sb.append("?"); 
            for (Map.Entry<String, String> entry : params.entrySet()) { 
 
                sb.append(entry.getKey()).append("="); 
                sb.append(URLEncoder.encode(entry.getValue(), encoding)); 
                sb.append("&"); 
            } 
            sb.deleteCharAt(sb.length() - 1); 
        } 
 
        HttpURLConnection connection = (HttpURLConnection) new URL( 
                sb.toString()).openConnection(); 
        connection.setConnectTimeout(5000); 
        connection.setRequestMethod("GET"); 
        if (connection.getResponseCode() == 200) { 
            return true; 
        } 
 
        return false; 
    } 
 

public class UserSerivce {

 public static boolean save(String getname, String getage) throws Exception {

  String path = "http://10.254.1.62/WebForGETMethod/ServletForGetMethod";

  Map<String, String> params = new HashMap<String, String>();
  params.put("name", getname);
  params.put("age", getage);

  return sendGETRequest(path, params, "UTF-8");
 }

 private static boolean sendGETRequest(String path,
   Map<String, String> params, String encoding) throws Exception {

  StringBuilder sb = new StringBuilder(path);
  if (params != null && !params.isEmpty()) {
   sb.append("?");
   for (Map.Entry<String, String> entry : params.entrySet()) {

    sb.append(entry.getKey()).append("=");
    sb.append(URLEncoder.encode(entry.getValue(), encoding));
 &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,