//使用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看作浏览器 DefaultHttpClient client = new DefaultHttpClient(); //client.execute(post)发送消息,返回值为服务器返回给浏览器的响应 HttpResponse response = client.execute(post); if( response.getStatusLine().getStatusCode() == 200){ return true; } return false; }
时间: 2024-10-10 09:48:17