java中http请求调用接口 传参为json格式

时间:2025-03-18 15:44:20

直接上代码

调用接口传参为application/json格式

 public String updateAqzxUser(TPolice tPolice, String Authorization,String depids,String id ) throws IOException {

        String uplevelArr = "";
        String con = "";
        for (String s : ()) {
            uplevelArr += con + "\"" + s + "\"";
            con = ",";
        }


        final String CONTENT_TYPE_TEXT_JSON = "text/json";
        DefaultHttpClient client = new DefaultHttpClient();
        String jsonParams = "{  \"id\": \""+id+"\",\"name\":\"" + () + "\",\"department\":\"" + () + "\",\"contact\":\"" + () + "\",\"depids\":[ \""+depids+"\"],\"deviceCode\":\"\",\"idCard\":\"" + () + "\",\"password\":\"97047690c82e15930277c2ddba97647b\",\"pcard\":\"" + () + "\",\"photo\":\"\",\"roleids\":[" + uplevelArr + "],\"state\":1,\"featureList\":[],\"job\":[{\"code\":\"200001\",\"name\":\"民警\"}],\"isAdmin\":0,\"politicsStatusCode\":\"\",\"personalInformation\":\"\",\"jobDescription\":\"\"}";
        String url =  aqzxurl + "/user/editUser";
        //调用音频组地址
        HttpPost httpPost = new HttpPost(url);
        ("Authorization", Authorization);
        ("Content-Type", "application/json;charset=UTF-8");
        StringEntity se = new StringEntity(jsonParams, "UTF-8");
        (CONTENT_TYPE_TEXT_JSON);

        (se);

        HttpResponse response2 = null;

        response2 = (httpPost);
        HttpEntity entity2 = null;
        entity2 = ();

        String s2 = (entity2, "UTF-8");
//        JSONObject js01 = (JSONObject) (s2);
//        List<Map<String, Object>> list = (List<Map<String, Object>>) ("data");

        ("===="+s2);
        return s2;
    }

这里传参有点多 通过字符串转义将参数转为application/json。