java中的httpClient技术调用

public static com.want.userinfo.Employee getEmpByID(String jid){

CloseableHttpClient httpclient = null;  

        CloseableHttpResponse response = null;

Employee emp = new Employee();

try {

httpclient = HttpClients.createDefault();

HttpPost post = new HttpPost(SparkRes.getString("EMP_JSON",SparkRes.getString("JSON_SERVER")));

List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("userId", jid.substring(0,jid.indexOf("@"))));

params.add(new BasicNameValuePair("token", TokenService.getTokenString()));

// params.add(new BasicNameValuePair("token", "8f960466a5a04547a03f102b132e314f"));

post.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));

response = httpclient.execute(post);

// System.out.println(response.toString());

int status=response.getStatusLine().getStatusCode();  

     

    if(status==200){  

        String result=EntityUtils.toString(response.getEntity(), "UTF-8");  

//         System.out.println(result);

        JSONObject json=new JSONObject(result); 

       

        try {

        JSONArray data = (JSONArray) json.get("data");

emp.setEmpEmail(data.getJSONObject(0).getString("empEmail") == null ? "" : data.getJSONObject(0).getString("empEmail"));

emp.setEmpId(data.getJSONObject(0).getString("empId") == null ? "" :data.getJSONObject(0).getString("empId"));

emp.setEmpPhoneExt(data.getJSONObject(0).getString("empPhoneExt") == null ? "" : data.getJSONObject(0).getString("empPhoneExt"));

emp.setJobName(data.getJSONObject(0).getString("jobName") == null ? "" : data.getJSONObject(0).getString("jobName"));

emp.setName(data.getJSONObject(0).getString("empName") == null ? "" : data.getJSONObject(0).getString("empName"));

emp.setOrgName(data.getJSONObject(0).getString("orgName") == null ? "" : data.getJSONObject(0).getString("orgName"));

emp.setPosName(data.getJSONObject(0).getString("posName") == null ? "" : data.getJSONObject(0).getString("posName"));

} catch (Exception e) {

return null;

}

    }  

return emp;

} catch (Exception e) {

Log.error(e.toString(),e);

}finally {

try {

if(httpclient !=null)

httpclient.close();

if(response!=null)

response.close();

} catch (IOException e) {

Log.error(e.toString(),e);

}

}

return null;

}

发布了47 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_34233080/article/details/83655802
今日推荐