Android普通方式的DoPost请求提交数据



    /** 
     * 普通方式的DoPost请求提交数据 
     * @param map 传递进来的数据,以map的形式进行了封装 
     * @param path 要求服务器servlet的地址 
     * @return 返回的boolean类型的参数 
     * @throws Exception 
     */  
    public Boolean submitDataByDoPost(String str, String path) throws Exception {  
        // 注意Post地址中是不带参数的,所以newURL的时候要注意不能加上后面的参数  
        URL Url = new URL(path);  
        // Post方式提交的时候参数和URL是分开提交的,参数形式是这样子的:name=y&age=6  
        //StringBuilder sb = new StringBuilder();  
        // sb.append("?");  
//        for (HashMap.Entry<String, String> entry : map.entrySet()) {  
//            sb.append(entry.getKey()).append("=").append(entry.getValue());  
//            sb.append("&");  
//        }  
//        sb.deleteCharAt(sb.length() - 1);  
 //       String str = sb.toString();  
  
        HttpURLConnection HttpConn = (HttpURLConnection) Url.openConnection();
        HttpConn.setRequestMethod("POST");  
        HttpConn.setReadTimeout(5000);  
        HttpConn.setDoOutput(true);  
        HttpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
        HttpConn.setRequestProperty("Content-Length", String.valueOf(str.getBytes().length));  
        OutputStream os = HttpConn.getOutputStream();  
        os.write(str.getBytes());  
        if (HttpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {  
            return true;  
        }  
        return false;  
    } 



转自: http://keeponmoving.iteye.com/blog/1528472

你可能感兴趣的:(Android普通方式的DoPost请求提交数据)