JSON序列化后的数据不带类名与名命空间,所以这两个服务端跟客户端可以不对应,需要保证字段对应即可

Asp.net MVC端

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6. namespace cn.fstudio.hessian.dto
  7. {
  8. public class ResponseBase<T>
  9. {
  10. private int code;
  11. private string msg;
  12. private T model;
  13.  
  14. public int Code
  15. {
  16. get { return code; }
  17. set { code = value; }
  18. }
  19. public string Msg
  20. {
  21. get { return msg; }
  22. set { msg = value; }
  23. }
  24. public T Model
  25. {
  26. get { return model; }
  27. set { model = value; }
  28.  
  29. }
  30. }
  31. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using cn.fstudio.hessian.dto;
  7. using cn.fstudio.update.model;
  8. using AA.DAO;
  9. using System.Text;
  10. using EmitMapper;
  11. namespace AA.Web.Controllers
  12. {
  13. public class SoftController : Controller
  14. {
  15. //
  16. // GET: /Soft/
  17. /// <summary>
  18. /// 获取列表
  19. /// </summary>
  20. /// <returns></returns>
  21. public ActionResult Index()
  22. {
  23. var response = new ResponseBase<List<SoftInfo>>();
  24. try
  25. {
  26. using (var ctx = DBCtx.GetCtx())
  27. {
  28. response.Model= ObjectMapperManager.DefaultInstance.GetMapper<List<Base_Soft>,List<SoftInfo>>().Map(ctx.Base_Soft.ToList());
  29. }
  30. response.Code = ;
  31.  
  32. }
  33. catch (Exception ex)
  34. {
  35. response.Code = -;
  36. response.Msg = ex.Message;
  37. }
  38. return Json(response,"text/json",Encoding.UTF8,JsonRequestBehavior.AllowGet );
  39. }
  40.  
  41. public ActionResult GetApp(String id,String v)
  42. {
  43. using (var ctx = DBCtx.GetCtx())
  44. {
  45. int softId=int.Parse(id);
  46. var q = ctx.Base_SoftVersion.Where(ent => ent.SoftId == softId);
  47. if (!String.IsNullOrWhiteSpace(v))
  48. {
  49. q = q.Where(ent => ent.VersionCode == v);
  50. }
  51.  
  52. q = q.OrderByDescending(ent => ent.VersionCode).Take();
  53.  
  54. var it= q.FirstOrDefault();
  55.  
  56. if (it == null)
  57. {
  58. throw new Exception("未找到该软件!");
  59. }
  60. var soft= ctx.Base_Soft.FirstOrDefault(ent => ent.SoftId == softId);
  61.  
  62. return File(Server.MapPath(it.Path), "application/vnd.android", Url.Encode(string.Format("{0}_{1}.apk",soft.FileName,it.VersionCode)));
  63.  
  64. }
  65.  
  66. }
  67. public ActionResult AppVers(String id)
  68. {
  69. var response = new ResponseBase<List<SoftVersionInfo>>();
  70. try
  71. {
  72. using (var ctx = DBCtx.GetCtx())
  73. {
  74. var list= ctx.ExecuteStoreQuery<SoftVersionInfo>("Select SoftId,RecId,VersionName,VersionCode,AddTime From Base_SoftVersion Where SoftId={0} order by versionCode desc",id).ToList();
  75. response.Model = list;
  76. }
  77. response.Code = ;
  78.  
  79. }
  80. catch (Exception ex)
  81. {
  82. response.Code = -;
  83. response.Msg = ex.Message;
  84. }
  85. return Json(response, "text/json", Encoding.UTF8, JsonRequestBehavior.AllowGet);
  86. }
  87. public ActionResult AppLastVer(String id)
  88. {
  89. var response = new ResponseBase<SoftVersionInfo>();
  90. try
  91. {
  92. using (var ctx = DBCtx.GetCtx())
  93. {
  94. var it = ctx.ExecuteStoreQuery<SoftVersionInfo>("Select Top 1 SoftId,RecId,VersionName,VersionCode,AddTime From Base_SoftVersion Where SoftId={0} order by versionCode desc,RecId desc", id).FirstOrDefault();
  95. response.Model = it;
  96. }
  97. response.Code = ;
  98.  
  99. }
  100. catch (Exception ex)
  101. {
  102. response.Code = -;
  103. response.Msg = ex.Message;
  104. }
  105. return Json(response, "text/json", Encoding.UTF8, JsonRequestBehavior.AllowGet);
  106. }
  107.  
  108. }
  109. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6. namespace cn.fstudio.update.model
  7. {
  8. public class SoftInfo
  9. {
  10. private int softId;
  11. private string softName;
  12. private string versionName;
  13. private string versionCode;
  14. private DateTime addTime;
  15. private DateTime lastTime;
  16. private string memo;
  17. private int recId;
  18. private String fileName;
  19.  
  20. public String FileName
  21. {
  22. get { return fileName; }
  23. set { fileName = value; }
  24. }
  25.  
  26. /// <summary>
  27. /// SoftId
  28. /// </summary>
  29. public int SoftId
  30. {
  31. get { return softId; }
  32. set { softId = value; }
  33. }
  34. /// <summary>
  35. /// SoftName
  36. /// </summary>
  37. public string SoftName
  38. {
  39. get { return softName; }
  40. set { softName = value; }
  41. }
  42. /// <summary>
  43. /// VersionName
  44. /// </summary>
  45. public string VersionName
  46. {
  47. get { return versionName; }
  48. set { versionName = value; }
  49. }
  50. /// <summary>
  51. /// VersionCode
  52. /// </summary>
  53. public string VersionCode
  54. {
  55. get { return versionCode; }
  56. set { versionCode = value; }
  57. }
  58. /// <summary>
  59. /// AddTime
  60. /// </summary>
  61. public DateTime AddTime
  62. {
  63. get { return addTime; }
  64. set { addTime = value; }
  65. }
  66. /// <summary>
  67. /// LastTime
  68. /// </summary>
  69. public DateTime LastTime
  70. {
  71. get { return lastTime; }
  72. set { lastTime = value; }
  73. }
  74. /// <summary>
  75. /// Memo
  76. /// </summary>
  77. public string Memo
  78. {
  79. get { return memo; }
  80. set { memo = value; }
  81. }
  82. /// <summary>
  83. /// RecId
  84. /// </summary>
  85. public int RecId
  86. {
  87. get { return recId; }
  88. set { recId = value; }
  89. }
  90. }
  91. }

android端

  1. package cn.fstudio.update;
  2.  
  3. import java.lang.reflect.Type;
  4. import java.util.Date;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. import com.google.gson.FieldNamingPolicy;
  9. import com.google.gson.Gson;
  10. import com.google.gson.GsonBuilder;
  11. import com.google.gson.JsonDeserializationContext;
  12. import com.google.gson.JsonDeserializer;
  13. import com.google.gson.JsonElement;
  14. import com.google.gson.JsonParseException;
  15.  
  16. public class GsonUtil {
  17.  
  18. public static class DateDeserializer implements JsonDeserializer<Date> {
  19.  
  20. @Override
  21. public Date deserialize(JsonElement json, Type typeOfT,
  22. JsonDeserializationContext context) throws JsonParseException {
  23. String JSONDateToMilliseconds = "/Date\\((.*?)\\)/";
  24. Pattern pattern = Pattern.compile(JSONDateToMilliseconds);
  25. String value = json.getAsJsonPrimitive().getAsString();
  26. Matcher matcher = pattern.matcher(value);
  27. String result = matcher.replaceAll("$1");
  28.  
  29. return new Date(new Long(result));
  30. }
  31. }
  32.  
  33. public static Gson getGson() {
  34. GsonBuilder gsonb = new GsonBuilder();
  35. DateDeserializer ds = new DateDeserializer();
  36. gsonb.registerTypeAdapter(Date.class, ds);
  37. gsonb.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);
  38. Gson gson = gsonb.create();
  39. return gson;
  40.  
  41. }
  42. }
  1. package cn.fstudio.util;
  2.  
  3. import java.net.URI;
  4. import java.net.URISyntaxException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.HttpStatus;
  11. import org.apache.http.NameValuePair;
  12. import org.apache.http.client.CookieStore;
  13. import org.apache.http.client.HttpClient;
  14. import org.apache.http.client.methods.HttpGet;
  15. import org.apache.http.client.protocol.ClientContext;
  16. import org.apache.http.client.utils.URIUtils;
  17. import org.apache.http.client.utils.URLEncodedUtils;
  18. import org.apache.http.impl.client.BasicCookieStore;
  19. import org.apache.http.impl.client.DefaultHttpClient;
  20. import org.apache.http.protocol.BasicHttpContext;
  21. import org.apache.http.protocol.HttpContext;
  22. import org.apache.http.util.EntityUtils;
  23.  
  24. import android.R.string;
  25. import android.util.Log;
  26.  
  27. public class HttpClientUtil {
  28.  
  29. private static HttpContext httpContext;
  30. static {
  31. // 创建一个本地Cookie存储的实例
  32. CookieStore cookieStore = new BasicCookieStore();
  33. // 创建一个本地上下文信息
  34. httpContext = new BasicHttpContext();
  35. // 在本地上下问中绑定一个本地存储
  36. httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
  37.  
  38. }
  39. public static String get(String baseUrl,String path,List<NameValuePair> qparams){
  40.  
  41. try {
  42. URI uri=new URI(baseUrl);
  43. return get(uri.getScheme(), uri.getHost(), uri.getPort(), path, qparams);
  44. } catch (URISyntaxException e) {
  45. // TODO Auto-generated catch block
  46. throw new RuntimeException(e);
  47. }
  48.  
  49. }
  50. public static String get(String scheme,String host,int port,String path, List<NameValuePair> qparams){
  51. URI uri=null;
  52. try {
  53.  
  54. if(qparams==null)qparams=new ArrayList<NameValuePair>();
  55. uri = URIUtils.createURI(scheme, host, port, path, URLEncodedUtils.format(qparams, "UTF-8"), null);
  56. Log.d("Test", uri.toString());
  57. return get(uri.toString());
  58. } catch (URISyntaxException e) {
  59. throw new RuntimeException(e);
  60. }
  61.  
  62. }
  63. public static String get(String url){
  64.  
  65. try {
  66.  
  67. // HttpGet连接对象
  68. HttpGet httpRequest = new HttpGet(url);
  69. // 取得HttpClient对象
  70. HttpClient httpClient = new DefaultHttpClient();
  71. // 请求HttpClient,取得HttpResponse
  72. HttpResponse httpResponse = httpClient.execute(httpRequest,httpContext);
  73. // for (org.apache.http.Header h : httpResponse.getAllHeaders()) {
  74. // Log.d("Test",h.getName());
  75. // }
  76.  
  77. // 请求成功
  78. int statusCode=httpResponse.getStatusLine().getStatusCode();
  79. if ( statusCode == HttpStatus.SC_OK) {
  80. HttpEntity entity=httpResponse.getEntity();
  81. // 取得返回的字符串
  82. String strResult = EntityUtils.toString(entity);
  83. entity.consumeContent();
  84. ////httpClient.getConnectionManager().shutdown();
  85. return strResult;
  86. }
  87. throw new RuntimeException("网络请求执行错误,响应码:" +statusCode);
  88.  
  89. } catch (Exception e) {
  90. throw new RuntimeException(e);
  91. }
  92. }
  93. }
  1. package cn.fstudio.update;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Date;
  5.  
  6. public class SoftInfo implements Serializable {
  7. /**
  8. *
  9. */
  10. private static final long serialVersionUID = 1L;
  11. private int softId;
  12. private String softName;
  13. private String versionName;
  14. private String versionCode;
  15. private Date addTime;
  16. private Date lastTime;
  17. private String memo;
  18. private int recId;
  19. private String fileName;
  20. public int getSoftId() {
  21. return softId;
  22. }
  23. public void setSoftId(int softId) {
  24. this.softId = softId;
  25. }
  26. public String getSoftName() {
  27. return softName;
  28. }
  29. public void setSoftName(String softName) {
  30. this.softName = softName;
  31. }
  32. public String getVersionName() {
  33. return versionName;
  34. }
  35. public void setVersionName(String versionName) {
  36. this.versionName = versionName;
  37. }
  38. public String getVersionCode() {
  39. return versionCode;
  40. }
  41. public void setVersionCode(String versionCode) {
  42. this.versionCode = versionCode;
  43. }
  44. public Date getAddTime() {
  45. return addTime;
  46. }
  47. public void setAddTime(Date addTime) {
  48. this.addTime = addTime;
  49. }
  50. public Date getLastTime() {
  51. return lastTime;
  52. }
  53. public void setLastTime(Date lastTime) {
  54. this.lastTime = lastTime;
  55. }
  56. public String getMemo() {
  57. return memo;
  58. }
  59. public void setMemo(String memo) {
  60. this.memo = memo;
  61. }
  62. public int getRecId() {
  63. return recId;
  64. }
  65. public void setRecId(int recId) {
  66. this.recId = recId;
  67. }
  68. public String getFileName() {
  69. return fileName;
  70. }
  71. public void setFileName(String fileName) {
  72. this.fileName = fileName;
  73. }
  74. public static long getSerialversionuid() {
  75. return serialVersionUID;
  76. }
  77. }
  1. package cn.fstudio.update;
  2.  
  3. import java.io.Serializable;
  4.  
  5. public class ResponseBase<T> implements Serializable{
  6.  
  7. private static final long serialVersionUID = 1L;
  8. public int getCode() {
  9. return code;
  10. }
  11. public void setCode(int code) {
  12. this.code = code;
  13. }
  14. public String getMsg() {
  15. return msg;
  16. }
  17. public void setMsg(String msg) {
  18. this.msg = msg;
  19. }
  20. public T getModel() {
  21. return model;
  22. }
  23. public void setModel(T model) {
  24. this.model = model;
  25. }
  26. private int code;
  27. private String msg;
  28. private T model;
  29. }
  1. package cn.fstudio.test;
  2.  
  3. import java.lang.reflect.Type;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.message.BasicNameValuePair;
  9.  
  10. import com.google.gson.Gson;
  11. import com.google.gson.reflect.TypeToken;
  12.  
  13. import android.util.Log;
  14. import cn.fstudio.update.GsonUtil;
  15. import cn.fstudio.update.ResponseBase;
  16. import cn.fstudio.update.SoftInfo;
  17.  
  18. import cn.fstudio.update.SoftVersionInfo;
  19. import cn.fstudio.util.HttpClientUtil;
  20. import junit.framework.TestCase;
  21.  
  22. public class UpdateTest extends TestCase {
  23. final String TAG = "Test";
  24.  
  25. public void testHttpClient() {
  26. List<NameValuePair> qparams = new ArrayList<NameValuePair>();
  27. qparams.add(new BasicNameValuePair("id", ""));
  28. String json = HttpClientUtil.get("http", "122.226.151.4", ,
  29. "/soft/AppLastVer", qparams);
  30.  
  31. Type type = new TypeToken<ResponseBase<SoftVersionInfo>>(){}.getType();
  32. Gson gson=GsonUtil.getGson();
  33. ResponseBase<SoftVersionInfo> response= gson.fromJson(json, type);
  34.  
  35. Log.d(TAG, json);
  36. }
  37.  
  38. public void testSoft() {
  39.  
  40. String json = HttpClientUtil.get("http", "122.226.151.4", ,
  41. "/soft/", null);
  42. Gson gson=GsonUtil.getGson();
  43.  
  44. Type type = new TypeToken<ResponseBase<List<SoftInfo>>>(){}.getType();
  45.  
  46. ResponseBase<List<SoftInfo>> response= gson.fromJson(json, type);
  47. Log.d(TAG, json);
  48. }
  49. public void testAppVers() {
  50. List<NameValuePair> qparams = new ArrayList<NameValuePair>();
  51. qparams.add(new BasicNameValuePair("id", "22a"));
  52. String json = HttpClientUtil.get("http", "122.226.151.4", ,
  53. "/soft/appVers", qparams);
  54. Gson gson=GsonUtil.getGson();
  55.  
  56. Type type = new TypeToken<ResponseBase<List<SoftVersionInfo>>>(){}.getType();
  57.  
  58. ResponseBase<List<SoftVersionInfo>> response= gson.fromJson(json, type);
  59. Log.d(TAG, json);
  60. }
  61. public void testAppLastVer() {
  62. List<NameValuePair> qparams = new ArrayList<NameValuePair>();
  63. qparams.add(new BasicNameValuePair("id", ""));
  64. String json = HttpClientUtil.get("http://122.226.151.4:7086/",
  65. "/soft/appLastVer", qparams);
  66. Gson gson=GsonUtil.getGson();
  67.  
  68. Type type = new TypeToken<ResponseBase<SoftVersionInfo>>(){}.getType();
  69.  
  70. ResponseBase<SoftVersionInfo> response= gson.fromJson(json, type);
  71. Log.d(TAG, json);
  72. }
  73. }

Android Gson 操作的更多相关文章

  1. Android 常用操作

    0.android studios使用介绍 使用介绍 android studio 常用小技巧 网址 1.怎么样添加第三方库 方法一: 第一步:将第三方库以module的形式导入 第二步:选中要导入第 ...

  2. [Android Pro] 完美Android Cursor使用例子(Android数据库操作)

    reference to : http://www.ablanxue.com/prone_10575_1.html 完美 Android Cursor使用例子(Android数据库操作),Androi ...

  3. Android – 学习操作NFC – 2

    在<Android – 学习操作NFC – 1>说明了Android在处理NFC tag的机制.tag dispatch system的运作流程,以及三种ACTION_NDEF_DISCO ...

  4. JSON和GSON操作json数据

    1,JSON操作json import net.sf.json.JSONArray; import net.sf.json.JSONObject; //json操作数据 public static S ...

  5. 【转】Android Gson的使用

    Android Gson 2014 年 05 月 22 日 android 目前的客户端大都有和服务端进行交互,而数据的格式基本就是json了,于是在Android开发中就经常用到json解析,方便的 ...

  6. android 文件操作类简易总结

    android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...

  7. 为什么说android UI操作不是线程安全的

    转载于:http://blog.csdn.net/lvxiangan/article/details/17218409#t2 UI线程及Android的单线程模型原则 使用Worker线程 Commu ...

  8. Android权限操作之uses-permission详解

    本文实例讲述了Android权限操作之uses-permission.分享给大家供大家参考,具体如下: 我们在安装Android软件的时候,系统会提示该软件所需要的权限,相对于其他系统,android ...

  9. Java操作JSON数据(2)--Gson操作JSON数据

    Gson是Google公司发布的一个开发源码的Java库,可用于将Java对象转换为JSON字符串,也可用于将JSON字符串转换为对应的Java对象.本介绍下Gson的基本使用方法,包括序列化和反序列 ...

随机推荐

  1. nats 学习 集群ha 配置

      nats 的ha 是一个mesh 的结构,有两个主要的参数 clusters routers 启动三分节点(单机) 共享变量 SERVERS=nats://127.0.0.1:6222,nats: ...

  2. vs2013突然没有代码提示功能了。

    工具->选项->文本编辑器->C++ ->高级->禁用IntelliSense设置 false 然后选确定.

  3. bzoj 3730 震波——动态点分治+树状数组

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3730 查询一个点可以转化为查询点分树上自己到根的路径上每个点对应范围答案.可用树状数组 f ...

  4. 再记录一次delete出错的经历

    调试的时候进行到delete语句时出现问题,我做的操作是在函数体内用int*申请了N个内存空间,这让我十分纳闷,为什么不能delete呢? 回忆到之前delete出错也遇过一次问题 手动封装OpenC ...

  5. 远程连接Linux虚拟机上的mysql失败的解决方法

    今天在虚拟机Ubuntu上折腾了一晚上mysql,然后试着用java连接,搞了很久都没成功,但是同学配好的Debian上却连接成功了,也就是说我的配置有问题. 折腾了很久,最后还是通过理解异常信息来大 ...

  6. week3-栈和队列

    1.学习总结 2.PTA实验作业 2.1 题目1:7-1 jmu-报数游戏 2.2 设计思路(伪代码或流程图) 2.3 代码截图 2.4 PTA提交列表说明. 答案错误:error少了 !: 非零返回 ...

  7. MySQL锁之二:锁相关的配置参数

    锁相关的配置参数: mysql> SHOW VARIABLES LIKE '%timeout%'; +-----------------------------+----------+ | Va ...

  8. 1021 docker prometheus监控体系

    jmeter plugin监控的信息很少,只有cpu.内存.网络IO,但这些是不够的.例如对于分析mysql数据库的慢查询.最大连接数等更加细密度的信息. 服务端稳定测试的三个前提: 1.应用级别的自 ...

  9. PHP向客户端广播信息

    在网络中数据传播分为:Unicast(单播) , Multicast(多播或者组播) 和 Broadcast(广播).广播和多播仅应用于UDP,它们对需将报文同时传往多个接收者的应用来说十分重要.而 ...

  10. Hive 体系结构

    1.Hive架构与基本组成     下面是Hive的架构图. 图1.1 Hive体系结构     Hive的体系结构可以分为以下几部分:     (1)用户接口主要有三个:CLI,Client 和 W ...