国产探花免费观看_亚洲丰满少妇自慰呻吟_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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金寨县| 威宁| 姜堰市| 漾濞| 姜堰市| 芜湖县| 汝阳县| 湄潭县| 鄱阳县| 叶城县| 沙洋县| 监利县| 垣曲县| 德庆县| 榆中县| 黄骅市| 淮南市| 晋城| 页游| 天等县| 凤庆县| 泾阳县| 泰来县| 铜山县| 齐齐哈尔市| 宁国市| 锡林郭勒盟| 延长县| 洛宁县| 泾阳县| 托克逊县| 县级市| 漠河县| 东明县| 芜湖市| 东丰县| 循化| 林甸县| 志丹县| 广德县| 镶黄旗|