HttpClient&FastJson

访问URL

http://xxx.xxx.com/xxx-unify-query-web/employeeInfo?clientKey=cpPA8%2Fa%5C4%26%24g&clientId=mwbops-sit&employeeId=13075054

返回报文

{"data":{"employeeId":"xxx","employeeName":"黄xx","orgId":"xxx","orgName":"xxxx","positionId":"xxx","positionName":"开发工程师","ismajorPosition":"X","prefix":"emp"},"serviceResponse":{"status":"complete"}}

获取报文,解析并封装实体。

public EmployeeDto getEmployee(String employeeId, String orgId) throws Exception{
String url = null;
String params = null;
if(StringUtil.isBlank(orgId)){
url = this.urlEmploee;
params = "clientKey="+URLEncoder.encode(this.clientKey, "utf-8")+"&clientId="+this.clientId+"&employeeId="+employeeId;   
}else{
url = this.urlManager;
params = "clientKey="+URLEncoder.encode(this.clientKey, "utf-8")+"&clientId="+this.clientId+"&orgId="+orgId;   
}
HttpClient httpClient = new HttpClient();  
HttpMethod method = getMethod(url, params);  
        httpClient.executeMethod(method);  
    String response = method.getResponseBodyAsString();
    JSONObject jo = JSON.parseObject(response);
    JSONObject sjo = jo.getJSONObject("serviceResponse");
    if("complete".equals(sjo.getString("status"))){
    EmployeeDto emp = jo.getObject("data", EmployeeDto.class);
    return emp;
    }
return null;
} 
private static HttpMethod getMethod(String url,String param) throws IOException{  
        GetMethod getMethod = new GetMethod(url+"?"+param);  
        getMethod.releaseConnection();  
        return getMethod;  
    }


你可能感兴趣的:(HttpClient&FastJson)