实例--post请求,完成一个抽奖程序概率的测试
2016-10-26 20:37
232 查看
一个web项目测试,测试抽奖概率的正确性,写了一个小代码,验证概率
post和get请求的一个工具类
package kankan_interface; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; /** * json转换方面的工具类,全部为静态方法 * */ public class JsonUtil { // private static final Logger logger = logger.getLogger(JsonUtil.class); private static ObjectMapper mapper = new ObjectMapper(); static{ //设置序列化配置,为null的属性不加入到json中 mapper.setSerializationInclusion(Include.NON_NULL); } /** * 将对象转换成json字符串,如果转换失败则返回null * @param o 需要转换为json的对象 * @return String 转换后的json字符串 * * * */ public static String write2JsonStr(Object o){ String jsonStr = ""; try { jsonStr = mapper.writeValueAsString(o); } catch (JsonProcessingException e) { //logger.error("|JsonProcessingException|", e); } return jsonStr; } /** * 将json转换为对象 如果对象模版为内部类会出现问题,所以不要使用内部类 * @param json 要转换的json * @param clazz 要映射的对象 * @return 转换成的目标对象,如果转换失败返回null * */ public static Object json2Object(String json,Class<?> clazz){ try { return mapper.readValue(json,clazz); } catch (JsonParseException e) { e.printStackTrace(); // logger.error("|JsonParseException|异常字符串|" + json, e); } catch (JsonMappingException e) { e.printStackTrace(); // logger.error("|JsonMappingException|异常字符串|" + json, e); } catch (IOException e) { e.printStackTrace(); // logger.error("|IOException|异常字符串|" + json, e); } return null; } /** * 将json字符串转换为Map * @param json 需要转换为Map的json字符串 {}开头结尾的 * @return 转换后的map 如果转换失败返回null * */ @SuppressWarnings("unchecked") public static Map<String,Object> json2Map(String json){ try { if(json==null || json.length()==0) { return new HashMap<String,Object>() ; } return mapper.readValue(json,Map.class); } catch (JsonParseException e) { System.out.print(e); //logger.error("|JsonParseException|异常字符串|" + json, e); } catch (JsonMappingException e) { System.out.print(e); //logger.error("|JsonMappingException|异常字符串|" + json, e); } catch (IOException e) { System.out.print(e); //logger.error("|IOException|异常字符串|" + json, e); } return new HashMap<String,Object>() ; } /** * 将json数组转换为List<Map<String,Object>> json数组格式[{},{}] * @param jsonArray 需要转换的json数组 * @return 转换后的列表 如果转换失败返回null * */ @SuppressWarnings("unchecked") public static List<Map<String,Object>> jsonArray2List(String jsonArray){ // try { // return mapper.readValue(jsonArray, List.class); // } catch (JsonParseException e) { // logger.error("|JsonParseException|异常字符串|" + jsonArray, e); // } catch (JsonMappingException e) { // logger.error("|JsonMappingException|异常字符串|" + jsonArray, e); // } catch (IOException e) { // logger.error("|IOException|异常字符串|" + jsonArray, e); // } return new ArrayList<Map<String,Object>>(); } }
模拟请求,并进行抽奖换算概率
public class Lottery { /** * @author 张君 * @deprecated 抽奖并统计概率 * @throws JSONException * @throws ParseException */ // 将Json对象转换成Map public static void main(String[] args) throws JSONException, ParseException { // TODO Auto-generated method stub Map<String, String> ma=new HashMap<String, String>(); HttpClicent HTTP=new HttpClicent(); JsonUtil jsonUtil=new JsonUtil(); Map<String,Integer> map2=new HashMap<String,Integer>(); for(int i=0 ; i<10000 ;i++){ //1万次的抽奖概率 String json=HTTP.dopost("XXX/?actId=NationalDay2016&loginUserId=1009406073",ma);//模拟的一个post请求 json=json.substring(9);//取返回的json字符串,因为我们这个请求返回回来,和普通json不一样, json=json.substring(0,json.length()-1); Map<String ,Object>map=jsonUtil.json2Map(json); //使用map就是方便现在统计有几种奖品,每个奖品的概率 if(null!=map&&null!=map.get("rtn")&&map.get("rtn").toString().equals("0")){ map=(Map<String, Object>) map.get("data"); if(map.get("valid").toString().equals("1")){//判断是哪个奖品 String key=map.get("presentId").toString(); if(null!=map2.get(key)){ map2.put(key, map2.get(key)+1); }else{ map2.put(key, 1); } } } } for (Entry<String, Integer> entry : map2.entrySet()) { System.out.println("key = " + entry.getKey() + " and value = " + entry.getValue()); } } }
相关文章推荐
- 使用layer的alert函数完成根据post请求结果弹出一个提示,然后刷新本页面
- 微信小程序 POST请求的实例详解
- C# 做一个指定概率的抽奖程序
- 关于web程序调试相关工具谷歌访问助手,和测试 POST 请求客户端工具,Advanced REST Client的下载和安装
- 一个简单的抽奖(概率)小程序
- 一个使用HttpClient get post请求的实例
- Loadrunner 11 测试API java Vuser 模拟Post,Get 请求实例
- 微信小程序 POST请求(网络请求)详解及实例代码
- linux入手小程序——任何一个程序的运行只有借助于操作系统才能得以顺利完成的小实例
- 微信小程序 POST请求(网络请求)详解及实例代码
- Jmeter压力测试-http的post请求实例
- GET请求和POST请求有什么区别?GET请求的参数在URL的问号后面显示,而POST参数不在URL上;POST可以比GET请求更大的数据…一般的回答都是这样。 但是作为一个高端大气上档次的程序
- 一个好用的测试服务器接口的工具(post请求,xml,json等数据格式)
- 使用postMan工具测试post请求出现400错误的一个坑,记录下!
- 一个可以设置中奖概率的抽奖程序[转]
- 一个简单的SWT程序实例及详解
- 一个很好的在线测试编辑器(可以在线运行很多程序)
- 一个用Spring AOP实现异常处理和记录程序执行时间的实例(二)
- Android 开发中,如何将多个程序打包成一个apk文件,但是安装完成后显示多个程序?
- 如何在.Net Mvc中让Get,Post请求访问同一个Action的方法