0%

Android开发 HttpPost提交无参数名的请求(XML)

Http可以使用Key-Value键值对传送参数,也可以发送xml流。 下面是在Android中使用HttpClient提交的方法,Android中的HttpClient与Apache官方的HttpClient略有不同.

    public static String post(String fullUrl, String xml) throws IOException {
        String html = null;
        HttpPost post = new HttpPost(fullUrl);
        DefaultHttpClient httpclient = new DefaultHttpClient();

        StringEntity se=new StringEntity(xml,HTTP.UTF_8);
        try {
            post.setEntity(se);
            HttpResponse response = httpclient.execute(post);
            if (response.getStatusLine().getStatusCode() == 200) {// 如果状态码为200,就是正常返回
                html = EntityUtils.toString(response.getEntity(),"UTF-8");
                // 得到返回的字符串
                System.out.println(html);
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        return html;
    }