Android开发实现查询远程服务器的工具类QueryUtils完整实例
作者:LovooGod 时间:2021-11-06 14:57:10
本文实例讲述了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程序设计有所帮助。
来源:http://blog.csdn.net/lovoo/article/details/51386855
标签:Android,服务器,工具类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#构造函数详解
2023-02-25 22:38:50
Android LuBan与Compressor图片压缩方式
2022-11-29 01:18:41
JDK 7U15在 Windows x86平台下的安装方法
2023-04-09 07:31:08
![](https://img.aspxhome.com/file/2023/4/72324_0s.png)
C#中内联函数的用法介绍
2023-04-18 18:29:18
Java设计模式之java装饰者模式详解
2023-10-27 06:17:04
![](https://img.aspxhome.com/file/2023/6/68726_0s.png)
c#中oracle的to_date函数使用方法
2021-09-06 10:21:17
Java冒泡排序的定义与实例代码
2022-03-29 12:32:31
![](https://img.aspxhome.com/file/2023/2/63022_0s.png)
SpringBoot使用prometheus监控的示例代码
2021-05-29 02:37:41
![](https://img.aspxhome.com/file/2023/0/65590_0s.jpg)
Java客户端利用Jedis操作redis缓存示例代码
2021-05-31 03:43:42
ShardingSphere jdbc实现分库分表核心概念详解
2023-11-24 12:09:45
![](https://img.aspxhome.com/file/2023/3/59763_0s.png)
Android使用phonegap从相册里面获取照片(代码分享)
2023-07-24 18:53:03
java动态口令登录实现过程详解
2022-01-01 10:16:28
C#从画刷创建画笔的方法
2022-11-22 15:49:17
android自定义View实现圆环颜色选择器
2023-11-07 19:16:02
![](https://img.aspxhome.com/file/2023/8/125348_0s.gif)
C#实现剪刀石头布游戏
2021-11-10 05:19:32
![](https://img.aspxhome.com/file/2023/7/91377_0s.jpg)
C#封装的Sqlite访问类实例
2022-04-28 15:38:13
Android库项目中的资源ID冲突的解决方法
2023-11-04 05:32:53
springboot中@Async默认线程池导致OOM问题
2022-12-19 20:07:01
![](https://img.aspxhome.com/file/2023/9/103809_0s.png)
C#调用海康工业相机SDK采集图像并在Halcon窗口中显示方式
2022-06-26 00:48:02
![](https://img.aspxhome.com/file/2023/3/113813_0s.png)
springboot 整合 sa-token简介及入门教程
2023-03-24 01:10:45
![](https://img.aspxhome.com/file/2023/7/83457_0s.png)