Android開發(fā)實現(xiàn)查詢遠程服務器的工具類QueryUtils完整實例
更新時間:2017年11月11日 12:13:08 作者:LovooGod
這篇文章主要介紹了Android開發(fā)實現(xiàn)查詢遠程服務器的工具類QueryUtils,涉及Android服務器請求發(fā)送、接收、數(shù)據(jù)交互等相關操作技巧,需要的朋友可以參考下
本文實例講述了Android開發(fā)實現(xiàn)查詢遠程服務器的工具類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封裝請求參數(shù)
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封裝請求參數(shù)
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 數(shù)組轉換為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開發(fā)入門與進階教程》、《Android視圖View技巧總結》、《Android編程之a(chǎn)ctivity操作技巧總結》、《Android操作SQLite數(shù)據(jù)庫技巧總結》、《Android操作json格式數(shù)據(jù)技巧總結》、《Android數(shù)據(jù)庫操作技巧總結》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android資源操作技巧匯總》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
您可能感興趣的文章:
- android中Intent傳值與Bundle傳值的區(qū)別詳解
- android教程之intent的action屬性使用示例(intent發(fā)短信)
- android中intent傳遞list或者對象的方法
- Android Intent的幾種用法詳細解析
- Android組件間通信--深入理解Intent與IntentFilter
- Android Intent啟動別的應用實現(xiàn)方法
- 詳解Android中Intent的使用方法
- Android系列之Intent傳遞對象的幾種實例方法
- Android利用Intent啟動和關閉Activity
- Android開發(fā)中超好用的正則表達式工具類RegexUtil完整實例
- Android開發(fā)實現(xiàn)的Intent跳轉工具類實例

