Android 查询远程服务器的工具QueryUtils

/** * 查询远程服务器的工具 * @author chen.lin * */
public class QueryUtils {
    private static final String TAG = "CommonUtils";
    private static QueryUtils instance;
    private SharedPreferences sp;
    private QueryUtils(Context context){
        sp = context.getSharedPreferences(Constant.CONFIG, Context.MODE_PRIVATE);
    }

    public static QueryUtils getInstance(Context context){
        if (instance == null) {
            synchronized (QueryUtils.class) {
                if (instance == null) {
                    instance = new QueryUtils(context);
                }
            }
        }
        return instance;
    }


    /** * 请求服务器得到返回值 * * @param keyword * @return * @throws Exception */
    public String getValue(String keyword, String reqType) throws Exception {
        String returnValue = null;
        // 使用Map封装请求参数
        Map<String, String> map = new HashMap<String, String>();
        map.put("reqType", reqType);
        map.put("localIP", sp.getString(Constant.NETIP, ""));
        if (keyword != null && !"".equals(keyword)) {
            map.put("keyword", keyword);
        }
        String url = "http://" + sp.getString(Constant.NETURL, "") + "/ymerp/" + "ServiceDocumentServlet";
        returnValue = HttpUtil.postRequest(url, map);
        return returnValue;
    }

    /** * 请求服务器得到返回值 * * @param keyword * @return * @throws Exception */
    public String queryServer(String keyword, String reqType, String servlet) throws Exception {
        String returnValue = null;
        // 使用Map封装请求参数
        Map<String, String> map = new HashMap<String, String>();
        map.put("reqType", reqType);
        map.put("localIP", sp.getString(Constant.NETIP, ""));
        if (!TextUtils.isEmpty(keyword)) {
            map.put("keyword", keyword);
        }
        String url = "http://" + sp.getString(Constant.NETURL, "") + "/ymerp/" + servlet;
        returnValue = HttpUtil.postRequest(url, map);
        return returnValue;
    }

    /** * 将json 数组转换为Map 对象 * * @param jsonString * @return */
    @SuppressLint("SimpleDateFormat")
    public static HashMap<String, Object> getMap(String jsonStr, String title, String timeStr) {
        SimpleDateFormat yymmdd = new SimpleDateFormat("yyyy-MM-dd");
        JSONObject jsonObject = null;
        String key = null;
        Object value = null;
        try {
            jsonObject = new JSONObject(jsonStr);
            Iterator<String> it = jsonObject.keys();
            HashMap<String, Object> valueMap = new HashMap<String, Object>();
            while (it.hasNext()) {
                key = (String) it.next();
                value = jsonObject.get(key);

                if (key != null && title.equals(key) && value != null) {
                    String valuestr = value.toString();
                    if (valuestr.length() > 15) {
                        valuestr = valuestr.substring(0, 13) + "...";
                        value = valuestr;
                    }
                }
                if (key != null && timeStr.equals(key)) {
                    try {
                        if (value != null) {
                            Date date = (Date) value;
                            value = yymmdd.format(date);
                        } else {
                            valueMap.put(key, "");
                        }
                    } catch (Exception e) {
                    }
                }
                if (key != null && value != null) {
                    valueMap.put(key, value);
                }
            }
            return valueMap;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


}

你可能感兴趣的:(android,服务器,查询-json)