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

首頁 > 系統 > Android > 正文

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

2020-02-21 17:21:35
字體:
來源:轉載
供稿:網友

程序員在編寫程序的時候要花費很多時間進行編寫,但是也會浪費很多時間編寫到重復的編碼,本文是武林技術頻道小編和大家分享的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開發實現查詢遠程服務器的工具類QueryUtils完整實例。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平度市| 鲁山县| 富顺县| 成安县| 丹阳市| 竹溪县| 彰武县| 华池县| 凤台县| 呼图壁县| 鹤庆县| 五原县| 东光县| 洞头县| 同德县| 甘孜县| 揭东县| 广饶县| 陆良县| 寿光市| 宁南县| 枣阳市| 新宾| 新乡市| 定安县| 乡宁县| 赣州市| 名山县| 玛多县| 祁东县| 乾安县| 姜堰市| 镶黄旗| 黑水县| 孙吴县| 吕梁市| 阳山县| 乡宁县| 洱源县| 图木舒克市| 黎平县|