首页 > 其他分享 >Android 查询远程服务器的工具QueryUtils

Android 查询远程服务器的工具QueryUtils

时间:2023-03-06 10:32:59浏览次数:36  
标签:QueryUtils String keyword map value key 服务器 Android null


/**
* 查询远程服务器的工具
* @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;
}


}


标签:QueryUtils,String,keyword,map,value,key,服务器,Android,null
From: https://blog.51cto.com/u_12516227/6102743

相关文章

  • Android intent跳转工具类
    /***进行页面跳转的工具**@authorchen.lin**/publicclassIntentUtil{privatestaticfinalStringIMAGE_TYPE="image/*";privatestaticfinalStr......
  • 高并发场景下,如何优化服务器的性能
    摘要:tcp_nodelay参数主要是对TCP套接字来说的,那对于服务器硬件,如果要使其能够支撑上百万甚至上千万的并发,我们该如何对其进行优化呢?本文分享自华为云社区《​​【高并发】高......
  • 在linux服务器/系统上编译运行VTM
    在linux服务器运行VTM本文介绍的是如何将windows系统的已配置完成的项目放在linux系统上运行我使用的是featurize服务器做的,如果需要直接在linux系统或虚拟机上做的话,步......
  • 租用澳门服务器需要备案吗
     澳门的服务器需要备案吗?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。不需要,澳门属于海外国家,中国......
  • 用MiniPC搭建个人服务器
    最近突然对小型电子产品产生的兴趣,经过一段时间调查,最终选择从迷你PC下手。因为类似树莓派的产品,还是有一定的上手门槛的。开发板类的产品也更偏硬件一点。而迷你PC除了......
  • 云服务器搭建frp+docker+nginx转发到本机Nas以及docker服务
    #云服务器搭建frp+docker+nginx转发到本机Nas以及docker服务如果觉得样式不好:跳转即可(md文件复制过来有些样式会不一样)原文地址:https://www.firstsaofan.top/archives......
  • 说一下在写Android APP时遇到的具体问题
    问题一:总是显示出nosuchtable错误不要担心这个问题(该担心还是得担心一下的哈),以我出错多次的经验来看,只需要在运行APP之后,回到虚拟机的主界面,然后找到相应的APP虚拟软件,......
  • Android学习-每日打卡APP-实现浏览功能
    接着写每日打卡App现在实现了浏览功能packagecom.example.clockappliction;importandroidx.appcompat.app.AppCompatActivity;importandroid.content.Intent;im......
  • 用MiniPC搭建个人服务器
    最近突然对小型电子产品产生的兴趣,经过一段时间调查,最终选择从迷你PC下手。因为类似树莓派的产品,还是有一定的上手门槛的。开发板类的产品也更偏硬件一点。而迷你PC除了小......
  • 3月5号Android学习
    <?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools......