android 用json格式post数据

来源:互联网 发布:移动宽带没有网络 编辑:程序博客网 时间:2024/05/21 11:12

首先将待输出的数据存储在JSON对象中,然后编码输出,本来是想用HTTPUrlConnection方式post请求的,不知为何一直发不出去...求解,另外delete请求Log显示DELETE donot supper writing 不支持写,有人知道的帮个忙解决下,多谢啊!


public String postName(String baseUrl, String name) {

        String result = "";
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(baseUrl);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", name);
            StringEntity entity = new StringEntity(jsonObject.toString(),"UTF-8");//解码中文
            entity.setContentType("text/json");
            entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            post.setEntity(entity);
            HttpResponse responString = client.execute(post);
            result = convertStreamToString(responString.getEntity()
                    .getContent());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }