SoFunction
Updated on 2025-04-09

Android development complete example of QueryUtils tool class that implements query remote servers

This article describes the Android development tool class QueryUtils that implements query remote servers. Share it for your reference, as follows:

/**
  * Tools for querying remote servers
  * @author
  *
  */
public class QueryUtils {
  private static final String TAG = "CommonUtils";
  private static QueryUtils instance;
  private SharedPreferences sp;
  private QueryUtils(Context context){
    sp = (, Context.MODE_PRIVATE);
  }
  public static QueryUtils getInstance(Context context){
    if (instance == null) {
      synchronized () {
        if (instance == null) {
          instance = new QueryUtils(context);
        }
      }
    }
    return instance;
  }
  /**
    * The request server gets the return value
    *
    * @param keyword
    * @return
    * @throws Exception
    */
  public String getValue(String keyword, String reqType) throws Exception {
    String returnValue = null;
    // Use Map to encapsulate request parameters    Map<String, String> map = new HashMap<String, String>();
    ("reqType", reqType);
    ("localIP", (, ""));
    if (keyword != null && !"".equals(keyword)) {
      ("keyword", keyword);
    }
    String url = "http://" + (, "") + "/ymerp/" + "ServiceDocumentServlet";
    returnValue = (url, map);
    return returnValue;
  }
  /**
    * The request server gets the return value
    *
    * @param keyword
    * @return
    * @throws Exception
    */
  public String queryServer(String keyword, String reqType, String servlet) throws Exception {
    String returnValue = null;
    // Use Map to encapsulate request parameters    Map<String, String> map = new HashMap<String, String>();
    ("reqType", reqType);
    ("localIP", (, ""));
    if (!(keyword)) {
      ("keyword", keyword);
    }
    String url = "http://" + (, "") + "/ymerp/" + servlet;
    returnValue = (url, map);
    return returnValue;
  }
  /**
    * Convert json array to Map object
    *
    * @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 = ();
      HashMap<String, Object> valueMap = new HashMap<String, Object>();
      while (()) {
        key = (String) ();
        value = (key);
        if (key != null && (key) && value != null) {
          String valuestr = ();
          if (() > 15) {
            valuestr = (0, 13) + "...";
            value = valuestr;
          }
        }
        if (key != null && (key)) {
          try {
            if (value != null) {
              Date date = (Date) value;
              value = (date);
            } else {
              (key, "");
            }
          } catch (Exception e) {
          }
        }
        if (key != null && value != null) {
          (key, value);
        }
      }
      return valueMap;
    } catch (JSONException e) {
      ();
    }
    return null;
  }
}

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android database operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android resource operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.