本文实例讲述了Android开发实现查询远程服务器的工具类QueryUtils。分享给大家供大家参考,具体如下:public class QueryUtils { private static final String TAG = "Co
本文实例讲述了Android开发实现查询远程服务器的工具类QueryUtils。分享给大家供大家参考,具体如下:
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; } 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; } 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; } @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; }}
--结束END--
本文标题: Android开发实现查询远程服务器的工具类QueryUtils完整实例
本文链接: https://lsjlt.com/news/220148.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0