Android应用通过HTTP协议向WEB应用传递数据,常用Get和Post两种方式 Get方式传递的数据的小余2K(此大小也因浏览器不同而相异),而使用Post方式向WEB应用传递数据时没有数据大小的限制. 在执行复杂的操作可以选用,如要操作https和cookie以及重定向时可选用Android的提供的开源项目HttpClient来处理(见下) 第一部分:Get方式 注意: 1 Get方式中被传送的数据就跟在URl后面 2 conn.setRequestMethod("GET")中的GET一定要大写 3 sb.append(URLEncoder.encode(entry.getValue(), encoding))对URL进行编码防止中文时乱码.Tomcat默认采用的ISO8859-1 代码如下: public static boolean save(String title, String timelength) throws Exception{ Map<String,String> params = new HashMap<String, String>(); params.put("title", title); params.put("timelength", timelength); params.put("method", "save");//传给WEB应用中的save()方法 String path = "http://192.168.1.100:8080/videoweb/video/manage.do"; return sendHttpClientGETRequest(path, params, "UTF-8"); } private static boolean sendGETRequest(String path, Map<String, String> params, String encoding) throws Exception{ //http://192.168.1.100:8080/videoweb/video/manage.do?method=save&title=xxxx&timelength=45 StringBuilder sb = new StringBuilder(path); sb.append('?'); 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));//URL编码 sb.append('&'); } } sb.deleteCharAt(sb.length() - 1); HttpURLConnection conn = (HttpURLConnection) new URL(sb.toString()).openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET");//必须大写GET if(conn.getResponseCode() == 200){ return true; } return false; } 第二部分:Post方式 注意: 1 Post方式中数据是作为协议的实体数据发送给WEB应用的,而不是跟在uri的后面 在发送实体数据时,用到了输出流OutputStream outStream = conn.getOutputStream();outStream.write(entity); 其中entity为实体数据,类型为字节数组 2 各个数据用&连接如:name=liming&age=16 3 在设置协议的HTTP协议的头字段(即HttpURLConnection)时content-type和content-length绝不能省略 4 可用工具HttpWatch professional进行观测 代码如下: public static boolean save(String title, String timelength) throws Exception{ Map<String,String> params = new HashMap<String, String>(); params.put("title", title); params.put("timelength", timelength); params.put("method", "save"); String path = "http://192.168.1.100:8080/videoweb/video/manage.do"; return sendPOSTRequest(path, params, "UTF-8"); } private static boolean sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{ StringBuilder sb = new StringBuilder(); 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); } byte[] entity = sb.toString().getBytes();//得到实体数据,类型为字节数组 HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("POST"); conn.setDoOutput(true);//允许对外输出数据 // Content-Type: application/x-www-form-urlencoded conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", String.valueOf(entity.length)); OutputStream outStream = conn.getOutputStream(); outStream.write(entity); if(conn.getResponseCode() == 200){ return true; } return false; } 第三部分:使用Android的开源项目来实现GET和POST请求 但是方式的性能不如前面讲的直接使用GET和POST,因为里面封装了许多东西 在进行简单操作的时候没必要使用它.在执行复杂的操作可以选用,如要操作https和cookie以及重定向时可选用 public static boolean save(String title, String timelength) throws Exception{ Map<String,String> params = new HashMap<String, String>(); params.put("title", title); params.put("timelength", timelength); params.put("method", "save"); String path = "http://192.168.1.100:8080/videoweb/video/manage.do"; return sendHttpClientPOSTRequest(path, params, "UTF-8"); } private static boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{ List<NameValuePair> pairs = new ArrayList<NameValuePair>(); if(params!=null && !params.isEmpty()){ for(Map.Entry<String, String> entry : params.entrySet()){ pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } //完成实体数据 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, encoding); HttpPost post = new HttpPost(path); post.setEntity(entity); DefaultHttpClient client = new DefaultHttpClient();//可将DefaultHttpClient看作浏览器 HttpResponse response = client.execute(post);//client.execute(post)发送消息,返回值为服务器返回给浏览器的响应 if( response.getStatusLine().getStatusCode() == 200){ return true; } return false; }