Demo entry 6343450

None

   

Submitted by anonymous on Jan 09, 2017 at 08:55
Language: Java. Code size: 10.2 kB.

package com.mobogenie.service.m;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mobogenie.common.IpParse;

/**
 * m-market service
 * 
 * @author liuwei_hw
 *
 */
// Spring Bean的标识.
@Component
// 类中所有public函数都纳入事务管理的标识.
@Transactional
public class GameService extends BaseService {

    private static Map<String, String> categoryMap;

    private static Map<String, String> categoryMTypeCodeMap;

    /**
     * get featured data
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Map<String, String>> getFeatured(HttpServletRequest request) {
        String url = "http://" + getLaguageCountry(request.getServerName())
                + "market.voga360.com/json/dynlist?t=gamerecmd&tpl=order_10&psize=50&fromsys=8";
        String jsonStr = httpGetByUrl(url);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> data = objectMapper.readValue(jsonStr, Map.class);
            return (List<Map<String, String>>) data.get("data");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, String>> getPics(HttpServletRequest request) {
        String url = "http://" + getLaguageCountry(request.getServerName())
                + "market.voga360.com/json/list?t=playpic&type=7&fromsys=8";
        String jsonStr = httpGetByUrl(url);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> data = objectMapper.readValue(jsonStr, Map.class);
            List<Map<String, String>> result = (List<Map<String, String>>) data.get("data");
            for (Map<String, String> map : result) {
                String detailUrl = map.get("viewPath");
                if (-1 != detailUrl.indexOf("appid=")) {
                    detailUrl = detailUrl.substring(detailUrl.indexOf("appid=") + 6);
                }
                map.put("apkid", detailUrl);
            }

            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public Map<String, List<Map<String, String>>> getTop(HttpServletRequest request) {
        String countryCode = IpParse.searchbyIP4Country(request);
        String url = "http://" + getLaguageCountry(request.getServerName())
                + "market.voga360.com/frontend/appcustomlist.htm?mtypecode=2&typecode=0&fromsys=8&tpl=order_10&pcountry="
                + countryCode;
        Map<String, List<Map<String, String>>> resultMap = new HashMap<String, List<Map<String, String>>>();
        String jsonStr = httpGetByUrl(url);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> data = objectMapper.readValue(jsonStr, Map.class);
            List<Map<String, Object>> rootList = (List<Map<String, Object>>) data.get("data");
            for (Map<String, Object> topData : rootList) {
                resultMap.put("type" + topData.get("type").toString().replaceAll(" ", "_"),
                        (List<Map<String, String>>) topData.get("list"));
            }
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, String>> getTopAll(HttpServletRequest request, String topName) {
        String url = "http://" + getLaguageCountry(request.getServerName())
                + "market.voga360.com/json/dynlist?t=custom_" + topName
                + "&tpl=order_10&mtypecode=2&typecode=0&psize=50&fromsys=8";
        String jsonStr = httpGetByUrl(url);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> data = objectMapper.readValue(jsonStr, Map.class);
            return (List<Map<String, String>>) data.get("data");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> getCollections(HttpServletRequest request) {
        String url = "http://" + getLaguageCountry(request.getServerName())
                + "market.voga360.com/json/list?t=subjectinfolist&position=2&fromsys=8";
        String jsonStr = httpGetByUrl(url);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> data = objectMapper.readValue(jsonStr, Map.class);
            return (List<Map<String, Object>>) data.get("data");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, String>> getCategories(HttpServletRequest request) {
        String url = "http://" + getLaguageCountry(request.getServerName())
                + "market.voga360.com/json/list?t=category&mtypecode=2&fromsys=8";
        String jsonStr = httpGetByUrl(url);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> data = objectMapper.readValue(jsonStr, Map.class);
            List<Map<String, String>> cats = (List<Map<String, String>>) data.get("data");
            for (Map<String, String> cat : cats) {
                if (cat.get("typeName") == null)
                    continue;
                cat.put("rTypeName", convertName(cat.get("typeName")));
                cat.put("enTypeName", convertName(this.getCateName(String.valueOf(cat.get("typeCode")), "2")));
            }
            fillCategoryMap(cats);
            fillCategoryMTypeCodeMap(cats);
            return cats;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Map<String, String> getCategoryMap(HttpServletRequest request) {
        if (categoryMap == null) {
            fillCategoryMap(getCategories(request));
        }
        return categoryMap;
    }

    public Map<String, String> getCategoryMTypeCodeMap(HttpServletRequest request) {
        if (categoryMTypeCodeMap == null) {
            fillCategoryMTypeCodeMap(getCategories(request));
        }
        return categoryMTypeCodeMap;
    }

    private void fillCategoryMap(List<Map<String, String>> cats) {
        if (categoryMap == null) {
            categoryMap = new HashMap<String, String>();
        } else {
            categoryMap.clear();
        }
        for (Map<String, String> cat : cats) {
            if (cat.get("typeCode") == null)
                continue;
            categoryMap.put(String.valueOf(cat.get("typeCode")), cat.get("typeName"));
        }
    }

    private void fillCategoryMTypeCodeMap(List<Map<String, String>> cats) {
        if (categoryMTypeCodeMap == null) {
            categoryMTypeCodeMap = new HashMap<String, String>();
        } else {
            categoryMTypeCodeMap.clear();
        }
        for (Map<String, String> cat : cats) {
            if (cat.get("typeCode") == null)
                continue;
            categoryMTypeCodeMap.put(String.valueOf(cat.get("typeCode")), String.valueOf(cat.get("mtypeCode")));
        }
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> getCategoriesDetail(HttpServletRequest request, String typecode, String topType) {
        // 4:top 5:new
        int topT = "new".equals(topType) ? 5 : 4;
        String url = "http://" + getLaguageCountry(request.getServerName())
                + "market.voga360.com/json/dynlist?t=custom_" + topT + "&fromsys=8&tpl=order_10&mtypecode="
                + this.getCategoryMTypeCodeMap(request).get(typecode) + "&typecode=" + typecode + "&psize=50";
        String jsonStr = httpGetByUrl(url);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> data = objectMapper.readValue(jsonStr, Map.class);
            return (List<Map<String, Object>>) data.get("data");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> getCategoriesDetail(HttpServletRequest request, String typecode, String topType,
            String nexid) {
        // 4:top 5:new
        int topT = "new".equals(topType) ? 5 : 4;
        String url = "http://" + getLaguageCountry(request.getServerName())
                + "market.voga360.com/json/dynlist?t=custom_" + topT + "&fromsys=8&tpl=order_10&mtypecode=1&typecode="
                + this.getCategoryMTypeCodeMap(request).get(typecode) + "&psize=50&nextid=" + nexid;
        String jsonStr = httpGetByUrl(url);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Map<String, Object> data = objectMapper.readValue(jsonStr, Map.class);
            return (List<Map<String, Object>>) data.get("data");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // private String httpGetByUrl(String url) {
    // // 创建HttpClientBuilder
    // HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    // // HttpClient
    // CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
    // HttpGet httpGet = new HttpGet(url);
    // try {
    // //执行get请求
    // HttpResponse httpResponse = closeableHttpClient.execute(httpGet);
    // //获取响应消息实体
    // HttpEntity entity = httpResponse.getEntity();
    // //响应状态
    // //System.out.println("status:" + httpResponse.getStatusLine());
    // //判断响应实体是否为空
    // if (entity != null) {
    // return EntityUtils.toString(entity, "UTF-8");
    // }
    // } catch (IOException e) {
    // e.printStackTrace();
    // } finally {
    // try {
    // //关闭流并释放资源
    // closeableHttpClient.close();
    // } catch (IOException e) {
    // e.printStackTrace();
    // }
    // }
    // return "";
    // }

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).