scikitlearn tensorflow实用教材
71.1元
包邮
(需用券)
去购买 >
//接口与对象的条件绑定并返回json数据
public static String executeHttpRequestByGetType(String url,Object obj ) {
HttpClient client = new HttpClient();
StringBuffer sb = new StringBuffer(url);
Map keyValueMap=objectToMap(obj);
PostMethod postMethod = null;
try {
//设置请求参数
if (keyValueMap != null) {
Iterator it = keyValueMap.entrySet().iterator();
if (keyValueMap.size() > 0) {
sb.append("?");
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
sb.append(entry.getKey() + "=" + entry.getValue() + "&");
}
sb.deleteCharAt(sb.length()-1);
}
}
postMethod = new PostMethod(sb.toString());
//log.debug("query uri ===============" + postMethod.getURI());
// 设置成了默认的恢复策略,在发生异常时候将自动重试3次,
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
//设置参数编码为gbk
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
//设置超时时间
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 200000);
//HttpClient第一步中创建好的实例的 executeMethod 方法来执行第二步中创建好的 method 实例
int statusCode = client.executeMethod(postMethod);
if (statusCode != HttpStatus.SC_OK) {
//log.info("request '" + url + "' failed,the status is not 200,status:" + statusCode);
return "";
}
String responseBody = postMethod.getResponseBodyAsString();
return responseBody;
} catch (Exception e) {
//log.error("发生异常!请检查网络和参数", e);
} finally {
if(postMethod!=null){
postMethod.releaseConnection();
}
}
return null;
}
@Test
public void test(){
TestBean be=new TestBean();
InterfaceData da=new InterfaceData();
String url= "http://59.111.44.97:9096/webapi/TakeAll";//接口地址
Map msp=new HashMap();
String s=da.executeHttpRequestByGetType(url,be);
//JSONUtil.deserialize(s);
System.out.println(s);
}
//object转化为Map
public static Map objectToMap(Object obj){
Map map=null;
if(obj==null){
return map;
}else{
try {
map= new HashMap();
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter!=null ? getter.invoke(obj) : null;
if(value!=null){
map.put(key, value.toString());
}
}
return map;
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
原文链接:http://1069843121.iteye.com/blog/2401362
java 11官方入门(第8版)教材
79.84元
包邮
(需用券)
去购买 >