Android开发使用json实现服务器与客户端数据的交互功能示例
本文实例讲述了Android开发使用json实现服务器与客户端数据的交互功能。分享给大家供大家参考,具体如下:
第一步:写一个远程查询工具类,使用单例模式
/**
* 查询远程服务器的工具
* @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 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 = postRequest(url, map);
return returnValue;
}
}
/**
* 请求远程服务器,并封装参数信息
* @param url
* @param rawParams
* @return
* @throws Exception
*/
public static String postRequest(String url, Map<String, String> rawParams) throws Exception {
// 创建HttpPost对象。
HttpPost post = new HttpPost(url);
// 如果传递参数个数比较多的话可以对传递的参数进行封装
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (String key : rawParams.keySet()) {
// 封装请求参数
params.add(new BasicNameValuePair(key, rawParams.get(key)));
}
//Logger.i(TAG, "params------------------->" + params);
// 设置请求参数
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 15000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
// 发送POST请求
HttpResponse httpResponse = httpClient.execute(post);
// 如果服务器成功地返回响应
String result = null;
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 获取服务器响应字符串
result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
Logger.i(TAG, "result-------->" + result);
}
return result;
}
第二步:使用软引用把远程得到的数据缓存到手机,如果服务器有数据更新,重新查询
/**
* 使用这个需要注意,一切都必须与服务器上的字段一一对应,大小写一致 为了保持一致,所有的实体都必须小写,远程数据库上的字段也得小写
*
* @author chen.lin
*
*/
@SuppressWarnings({ "unchecked", "deprecation" })
public class BaseManager {
private static BaseManager instance;
private QueryUtils queryUtils;
private SharedPreferences sp;
private Context context;
private BaseManager(Context context) {
this.context = context;
queryUtils = QueryUtils.getInstance(context);
sp = context.getSharedPreferences(Constant.CONFIG, Context.MODE_PRIVATE);
}
public static BaseManager getInstance(Context context) {
if (instance == null){
synchronized (BaseManager.class) {
if (instance == null) {
instance = new BaseManager(context);
}
}
}
return instance;
}
private static Map<String, List<?>> LISTCACHE;//
static {
// 16M,如果不足<16M(模拟器)
// 32M,真机
if (MemoryManager.hasAcailMemory()) {
LISTCACHE = new HashMap<String, List<?>>();
} else {
LISTCACHE = new SoftMap<String, List<?>>();
}
}
private static Map<String, Object> DOCCACHE;//
static {
// 16M,如果不足<16M(模拟器)
// 32M,真机
if (MemoryManager.hasAcailMemory()) {
DOCCACHE = new HashMap<String, Object>();
} else {
DOCCACHE = new SoftMap<String, Object>();
}
}
public <T> List<T> queryListByCache(Class<T> clazz, String key, String reqType, String servlet) throws Exception {
List<T> list = null;
// 一旦创建过,重用
// 判断是否创建了——曾经创建过的界面需要存储
if (LISTCACHE != null && LISTCACHE.containsKey(key)) {
// 创建了,重用
list = (List<T>) LISTCACHE.get(key);
if (list == null || list.isEmpty()) {
// 有时候查询 的数据过大,viewcache中放置不了那么多数据,就会被垃圾回收站回收,得重新查询远程数据库
list = getListFromServer(clazz, key, reqType, servlet);
LISTCACHE.put(key, list);
}
} else {
// 否则,创建
list = getListFromServer(clazz, key, reqType, servlet);
LISTCACHE.put(key, list);
}
return list;
}
public <T> List<T> getListFromServer(Class<T> clazz, String keyword, String reqType, String servlet)
throws Exception {
List<T> list = new ArrayList<T>();
String returnValue = queryUtils.queryServer(keyword, reqType, servlet);
if (!TextUtils.isEmpty(returnValue)) {
Gson gson = new Gson();
JsonParser jsonParser = new JsonParser();
JsonArray jsonArray = jsonParser.parse(returnValue).getAsJsonArray();
if (jsonArray != null) {
T t = null;
// 循环记录数(多少条)
for (JsonElement json : jsonArray) {
if (json != null) {
t = gson.fromJson(json, clazz);
list.add(t);
}
}
}
}
return list;
}
public <T> T queryDocByCache(Class<T> clazz, String key, String reqType, String servlet) throws Exception {
T t = null;
// 一旦创建过,重用
// 判断是否创建了——曾经创建过的界面需要存储
if (DOCCACHE != null && DOCCACHE.containsKey(key)) {
// 创建了,重用
t = (T) DOCCACHE.get(key);
if (t == null) {
// 有时候查询 的数据过大,viewcache中放置不了那么多数据,就会被垃圾回收站回收,得重新查询远程数据库
t = getDocFromServer(clazz, key, reqType, servlet);
DOCCACHE.put(key, t);
}
} else {
// 否则,创建
t = getDocFromServer(clazz, key, reqType, servlet);
DOCCACHE.put(key, t);
}
return t;
}
public <T> T getDocFromServer(Class<T> clazz, String keyword, String reqType, String servlet) throws Exception {
String returnValue = queryUtils.queryServer(keyword, reqType, servlet);
if (!TextUtils.isEmpty(returnValue)) {
Gson gson = new Gson();
T t = gson.fromJson(returnValue, clazz);
return t;
}
return null;
}
/**
* 查询判断客户是否已经添加
*
* @param keyword
* @param dialog
* @return
* @throws Exception
*/
public boolean isAccountExist(String keyword) throws Exception {
String returnValue = queryUtils.queryServer(keyword, "queryaccountExist", "AccountDocumentServlet");
if (!TextUtils.isEmpty(returnValue) && "true".equals(returnValue.trim())) {
return true;
}
return false;
}
/**
* 更新服务器上的数据
* @param context
* @param params
* @param servlet
* @return
* @throws Exception
*/
public void updateServer(final RequestParams params, String servlet) {
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://" + sp.getString(Constant.NETURL, "") + "/ymerp/" + servlet;
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
String returnValue = new String(responseBody);
JSONObject jsonObject = new JSONObject(returnValue);
if ("success".equalsIgnoreCase(jsonObject.getString("result"))) {
if (params.has("sendread")) {
Logger.i("update", "更新成功!");
}else {
Toast.makeText(context, "更新成功!", Toast.LENGTH_SHORT).show();
}
}else {
if (params.has("sendread")) {
Logger.i("update", "更新失败!");
}else {
Toast.makeText(context, "更新失败!", Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context, "json格式数据有误!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Toast.makeText(context, "网络错误!错误信息:" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
第三步:使用事例-客户信息查询
public class SearchActivity extends CommonActivity implements OnClickListener {
private BaseManager mManager;
private ListView mListView ;
private Button mBtnQuery;
private QueryAccountAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
// 初始化组件
initView();
// 进出动画效果
overridePendingTransition(R.anim.push_bottom_in, R.anim.push_bottom_out);
}
}
private void initView() {
mManager = BaseManager.getInstance(this);
mListView = (ListView) findViewById(R.id.lv_search);
mButton = (Button)findViewById(R.id.bt_query);
mButton.setOnClickListener(this);
mAdapter = new QueryAccountAdapter(this, mAccounts);
mListView.setAdapter(mAdapter);
}
@Override
public void onClick(View v) {
if(v == mBtnQuery){
mAccounts = mManager.getListFromServer(Account.class, query, "queryAccountByKey", "QueryServlet");
}
}
}
客户实体类:
/**
* 客户信息
*
* @author chen.lin
* @createtime 20150217
*/
public class Account implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String sname;// 客户名称
private String scode;// 客户编码
private String contact;// 联系人
private String idcontact;// 联系人id
private String emtype;// 客户分类
private String xsly;// 客户来源
private String xsbh;// 线索编号
private String xs;// 线索名称
private String idlead;// 线索id
private String leadscode;
private String idzh;// 相关展会
private String srcpath;// 来源途径
private String emindustry;// 行业
private String idarea; // 行政区域
private String saddress;// 客户地址
private String shdz;// 收货地址
private String cclx;// 乘车路线
private String spostcode;// 邮编
private String stel;// 手机
private String telcode;// 电话号码
private String sfax;// 传真
private String semail;// 邮箱
private String swebsite;// 站点主页
private String iddep;// 负责人部门
private String idowner;// 负责人
private String created;// 新建时间
private String createdby;// 新建人
private String updated;// 编辑时间
private String updatedby;// 编辑人
public String getIdcontact() {
return idcontact;
}
public void setIdcontact(String idcontact) {
this.idcontact = idcontact;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getIdlead() {
return idlead;
}
public void setIdlead(String idlead) {
this.idlead = idlead;
}
public String getLeadscode() {
return leadscode;
}
public void setLeadscode(String leadscode) {
this.leadscode = leadscode;
}
public String getTelcode() {
return telcode;
}
public void setTelcode(String telcode) {
this.telcode = telcode;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getScode() {
return scode;
}
public void setScode(String scode) {
this.scode = scode;
}
public String getEmtype() {
return emtype;
}
public void setEmtype(String emtype) {
this.emtype = emtype;
}
public String getXsly() {
return xsly;
}
public void setXsly(String xsly) {
this.xsly = xsly;
}
public String getXsbh() {
return xsbh;
}
public void setXsbh(String xsbh) {
this.xsbh = xsbh;
}
public String getXs() {
return xs;
}
public void setXs(String xs) {
this.xs = xs;
}
public String getIdzh() {
return idzh;
}
public void setIdzh(String idzh) {
this.idzh = idzh;
}
public String getSrcpath() {
return srcpath;
}
public void setSrcpath(String srcpath) {
this.srcpath = srcpath;
}
public String getEmindustry() {
return emindustry;
}
public void setEmindustry(String emindustry) {
this.emindustry = emindustry;
}
public String getIdarea() {
return idarea;
}
public void setIdarea(String idarea) {
this.idarea = idarea;
}
public String getSaddress() {
return saddress;
}
public void setSaddress(String saddress) {
this.saddress = saddress;
}
public String getShdz() {
return shdz;
}
public void setShdz(String shdz) {
this.shdz = shdz;
}
public String getCclx() {
return cclx;
}
public void setCclx(String cclx) {
this.cclx = cclx;
}
public String getSpostcode() {
return spostcode;
}
public void setSpostcode(String spostcode) {
this.spostcode = spostcode;
}
public String getStel() {
return stel;
}
public void setStel(String stel) {
this.stel = stel;
}
public String getSfax() {
return sfax;
}
public void setSfax(String sfax) {
this.sfax = sfax;
}
public String getSemail() {
return semail;
}
public void setSemail(String semail) {
this.semail = semail;
}
public String getSwebsite() {
return swebsite;
}
public void setSwebsite(String swebsite) {
this.swebsite = swebsite;
}
public String getIddep() {
return iddep;
}
public void setIddep(String iddep) {
this.iddep = iddep;
}
public String getIdowner() {
return idowner;
}
public void setIdowner(String idowner) {
this.idowner = idowner;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getCreatedby() {
return createdby;
}
public void setCreatedby(String createdby) {
this.createdby = createdby;
}
public String getUpdated() {
return updated;
}
public void setUpdated(String updated) {
this.updated = updated;
}
public String getUpdatedby() {
return updatedby;
}
public void setUpdatedby(String updatedby) {
this.updatedby = updatedby;
}
public Account(String id, String sname, String scode, String idowner) {
this.id = id;
this.sname = sname;
this.scode = scode;
this.idowner = idowner;
}
public Account(String id, String sname, String scode, String emtype, String xsly, String xsbh, String xs,
String idzh, String srcpath, String emindustry, String idarea, String saddress, String shdz, String cclx,
String spostcode, String stel, String sfax, String semail, String swebsite, String iddep, String idowner,
String created, String createdby, String updated, String updatedby) {
this.id = id;
this.sname = sname;
this.scode = scode;
this.emtype = emtype;
this.xsly = xsly;
this.xsbh = xsbh;
this.xs = xs;
this.idzh = idzh;
this.srcpath = srcpath;
this.emindustry = emindustry;
this.idarea = idarea;
this.saddress = saddress;
this.shdz = shdz;
this.cclx = cclx;
this.spostcode = spostcode;
this.stel = stel;
this.sfax = sfax;
this.semail = semail;
this.swebsite = swebsite;
this.iddep = iddep;
this.idowner = idowner;
this.created = created;
this.createdby = createdby;
this.updated = updated;
this.updatedby = updatedby;
}
public Account() {
super();
}
}
第四步:服务器端 queryAccountByKey就是从客户端传过来的值
/**
*
* @author chen.lin
*/
public class QueryServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String reqType = request.getParameter("reqType");
String localIP = request.getParameter("localIP");
EJBClientLocal.setServerip(localIP);
/**
* 根据关键字查询服务单
*/
if (reqType != null && "queryAccountByKey".equalsIgnoreCase(reqType)) {
// 根据查询关键字查找对应用户列表
String keyword = request.getParameter("keyword");
System.out.println("keyword ----------------------:" + keyword);
try {
List<JSONObject> list = EJBClientLocal.getMetaCRMIntegratedSessionBeanLocal().queryAccountByKey(keyword);
System.out.println(" queryAccountByKey list:" + list);
if (list != null) {
JSONArray json = JSONArray.fromObject(list);
// 输出响应
out.println(json.toString());
out.flush();
}
} catch (Exception ex) {
System.out.println("queryAccountByKey error:" + ex.getMessage());
}
}
}
@Override
public String getServletInfo() {
return "Short description";
}
}
第五步:查询数据库
@Stateless
public class MetaCRMIntegratedSessionBean implements MetaCRMIntegratedSessionBeanRemote, MetaCRMIntegratedSessionBeanLocal {
@Resource
SessionContext ctx;
@PersistenceContext(unitName = "meta_crm_ejbPU")
private EntityManager em;
private DBMSqlServer2005 dbm = null;
private Statement statement = null;
private PreparedStatement pStatement = null;
SimpleDateFormat yyyymmdd = new SimpleDateFormat("yyyy-MM-dd");
@AroundInvoke
public Object log(InvocationContext ctx)
throws Exception {
String className = ctx.getTarget().getClass().getName();
String mothodName = ctx.getMethod().getName();
String target = className + "." + mothodName + "()";
System.out.println("开始调用 " + target + "方法");
long start = System.currentTimeMillis();
try {
Object localObject1 = ctx.proceed();
long time;
return localObject1;
} catch (Exception e) {
return null;
} finally {
long time = System.currentTimeMillis() - start;
System.out.print(target + "方法执行完毕用时 " + time + " ms");
}
}
/**
* 查询客户信息
* @param keyword
* @return
*/
public List<JSONObject> queryAccountByKey(String keyword) {
List<JSONObject> list = new ArrayList<JSONObject>();
String sql = "select acc.id, acc.sname, acc.scode, "
+ " con.sname as contact , emp1.sname as createdby, acc.created, acc.saddress, con.id "
+ " from account acc "
+ " left join org_employee emp1 on emp1.id = acc.createdby "//申请人名称
+ " left join contact con on acc.id=con.idaccount "//联系人
+ " where 1=1 ";
if (keyword != null) {
System.out.println("keyword----------------------->" + keyword);
sql += " and acc.sname like '%" + keyword + "%'";
sql += " or emp1.sname like '%" + keyword + "%'";
}
sql += " order by acc.created desc";
System.out.println("sql----------------------->" + sql);
Connection conn = getConn();
ResultSet rs = null;
Statement st = null;
try {
st = conn.createStatement();
rs = st.executeQuery(sql);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
while (rs.next()) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", rs.getObject(1) != null ? rs.getObject(1) : "");
jsonObject.put("sname", rs.getObject(2) != null ? rs.getObject(2) : "");
jsonObject.put("scode", rs.getObject(3) != null ? rs.getObject(3) : "");
jsonObject.put("contact", rs.getObject(4) != null ? rs.getObject(4) : "");
jsonObject.put("createdby", rs.getObject(5) != null ? rs.getObject(5) : "");
setTime(rs, sdf, jsonObject, "created", 6);
jsonObject.put("saddress", rs.getObject(7) != null ? rs.getObject(7) : "");
jsonObject.put("idcontact", rs.getObject(8) != null ? rs.getObject(8) : "");
list.add(jsonObject);
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
dbm.closeConn();
}
return list;
}
}
PS:这里再为大家推荐几款比较实用的json在线工具供大家参考使用:
在线JSON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在线格式化工具:
http://tools.jb51.net/code/jsonformat
在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson
json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat
C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程之activity操作技巧总结》、《Android文件操作技巧汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
相关文章
详解Flutter Image组件如何处理图片加载过程中的错误
在Flutter中,Image组件可以通过监听加载过程中的错误来处理图片加载过程中的错误,本文小编将给大家详细介绍了Flutter Image组件是如何处理图片加载过程中的错误,文中有详细的代码示例供大家参考,需要的朋友可以参下2023-10-10
解决Android Studio 格式化 Format代码快捷键问题
这篇文章主要介绍了解决Android Studio 格式化 Format代码快捷键问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-03-03
Android开发之图片旋转功能实现方法【基于Matrix】
这篇文章主要介绍了Android开发之图片旋转功能实现方法,结合实例形式分析了Android基于matrix矩阵操作图形变换的相关实现技巧,需要的朋友可以参考下2017-09-09
Android学习笔记--使用剪切板在Activity中传值示例代码
相对于getText和setText而言,利用ClipData对象来传递数据,更符合面向对象的思想,而且所能传递的数据类型也多样化了2013-06-06
Android使用 PopupWindow 实现底部弹窗功能
这篇文章主要介绍了Android使用 PopupWindow 实现底部弹窗功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-12-12


最新评论