| | |
| | | |
| | | import java.io.PrintWriter; |
| | | import java.lang.reflect.Type; |
| | | import java.math.BigDecimal; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | |
| | | // 默认为应用宝 |
| | | channel = "qq"; |
| | | } |
| | | adConfig = new Gson().fromJson(jsonObject.optJSONObject(channel).toString(), ADConfig.class); |
| | | JSONObject channelAd = jsonObject.optJSONObject(channel); |
| | | adConfig = new Gson().fromJson(channelAd.toString(), ADConfig.class); |
| | | if (version >= adConfig.getVersion()) { |
| | | adConfig = null; |
| | | } |
| | | //格式如下: |
| | | //{"typeProperties":{"csj":{"weight":1,"pid":"123123"},"gdt":{"weight":1,"pid":"123123"}}} |
| | | if (adConfig != null && channelAd.optJSONObject("typeProbability") != null) { |
| | | JSONObject properties = channelAd.optJSONObject("typeProbability"); |
| | | |
| | | BigDecimal total = new BigDecimal(0); |
| | | for (Iterator<String> its = properties.keys(); its.hasNext(); ) { |
| | | String k = its.next(); |
| | | total = total.add(new BigDecimal(properties.optJSONObject(k).optString("weight"))); |
| | | } |
| | | BigDecimal random = total.multiply(new BigDecimal(Math.random())); |
| | | total = new BigDecimal(0); |
| | | for (Iterator<String> its = properties.keys(); its.hasNext(); ) { |
| | | String k = its.next(); |
| | | total = total.add(new BigDecimal(properties.optJSONObject(k).optString("weight"))); |
| | | if (random.compareTo(total) < 0) { |
| | | adConfig.setPid(properties.optJSONObject(k).optString("pid")); |
| | | adConfig.setType(k); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | return adConfig; |
| | | } |
| | |
| | | public void setPid(String pid) { |
| | | this.pid = pid; |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | } |