国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發實現查詢遠程服務器的工具類QueryUtils完整實例

2019-10-22 18:23:53
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澄江县| 聂荣县| 南宫市| 临武县| 鞍山市| 宜君县| 胶州市| 玉屏| 崇仁县| 高州市| 华池县| 和平区| 承德市| 密山市| 琼中| 汶川县| 娄底市| 香河县| 虞城县| 新蔡县| 桃园市| 揭东县| 霍州市| 普定县| 许昌市| 深圳市| 永宁县| 嵊州市| 正阳县| 仙居县| 肥城市| 林口县| 上高县| 神池县| 大英县| 铁岭县| 赣州市| 托克托县| 睢宁县| 涡阳县| 衡东县|