参数为json的HttpClients的请求

接口文档:

用户同步

1. 接口地址

http://homesoft.top/oss-api//main/api/tjsynUser

2. 调用方式

通过 post 用户信息,数据格式如下:

[

  {

    "loginFlag": "111",

    "outUserId": "767",

    "realName": "何军",

    "syncMode": 0,

    "userName": "hejun"

  },{

    "loginFlag": "20180932",

    "outUserId": "401",

    "realName": "张建兵",

    "syncMode": 1,

    "userName": "zhangjianbing"

  }

]

3. 字段说明:

loginFlag 快速登陆标识

outUserId 用户ID

realName 真实姓名

syncMode 同步模式,0更新,1新增

userName 用户登陆名

4. 返回值:

{

  "code": 0,

  "content": [

    {

      "loginFlag": "111",

      "outUserId": "767",

      "realName": "何军",

      "success": true,  //此条数据更新成功

      "syncMode": 0,

      "userName": "hejun"

    },

    {

      "loginFlag": "20180932",

      "outUserId": "401",

      "realName": "张建兵",

      "success": false, //此条数据没有新增

      "syncMode": 1,

      "userName": "zhangjianbing"

    }

  ],

  "errorCode": "",

  "message": "已存在外部用户ID为401的用户,不能新增",//结果说明

  "serverTime": "2018-06-14 09:48:34",

  "success": false  //存在有没同步的情况

}


java测试类:


import java.io.IOException;

import java.util.HashMap;
import java.util.Map;


import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.google.gson.Gson;
public class HttpClientUtils {
private final static String CONTENT_TYPE_TEXT_JSON = "text/json";
    
    public static String postRequest(String url, Map<String, Object> param) throws ClientProtocolException, IOException{
        
        CloseableHttpClient client = HttpClients.createDefault();


        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
        
        Gson gson = new Gson();
        String parameter = gson.toJson(param);
System.out.println("0参数:"+parameter);


      parameter="["+parameter+"]";
      System.out.println("1参数:"+parameter);
        StringEntity se = new StringEntity(parameter);
        
        se.setContentType(CONTENT_TYPE_TEXT_JSON);se.setContentEncoding("UTF-8");
        httpPost.setEntity(se);


        CloseableHttpResponse response = client.execute(httpPost);
        HttpEntity entity = response.getEntity();
       // String result = EntityUtils.toString(entity, "UTF-8");
        String charset = "UTF-8";
        String result2 = "";
        if (entity != null) {  
            try {
charset = HttpClientUtils.getContentCharSet(entity);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  
            // 使用EntityUtils的toString方法,传递编码,默认编码是ISO-8859-1   
         result2 = EntityUtils.toString(entity, charset);
        } 
          System.out.println("结果2:"+result2);    
        return result;
    }
    
    public static <T> T jsonToObject(String jsonData, Class<T> type) {
        Gson gson = new Gson();
        T result = gson.fromJson(jsonData, type);
        return result;
    }
    public static String getContentCharSet(final HttpEntity entity)   
            throws Exception {   
            if (entity == null) {   
                throw new IllegalArgumentException("HTTP entity may not be null");   
            }   
            String charset = null;   
            if (entity.getContentType() != null) {    
                HeaderElement values[] = entity.getContentType().getElements();   
                if (values.length > 0) {   
                    NameValuePair param = values[0].getParameterByName("charset" );   
                    if (param != null) {   
                        charset = param.getValue();   
                    }   
                }   
            }   
            if(StringUtils.isEmpty(charset)){  
                charset = "UTF-8";  
            }  
            return charset;   
        }  
    public static void main(String[] args) throws ClientProtocolException, IOException {
    String url="http://homesoft.top/oss-api//main/api/tjsynUser";  
         
         
         Map<String,Object> params=new HashMap<String,Object>();
         params.put("loginFlag", "e10adc3949ba59abbe56e057f20f8838");
         params.put("outUserId", "9782");
         params.put("realName", "梁晓磊2");
         params.put("syncMode", "0");
         params.put("userName", "liangxiaolei");
         
       
         String body = postRequest(url,params);
         System.out.println("交易响应结果:"+params);  
         System.out.println(body);
         System.out.println(body);
    }
 
}

猜你喜欢

转载自blog.csdn.net/qq_23145857/article/details/80707775