博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net hessian + android hessdroid
阅读量:6069 次
发布时间:2019-06-20

本文共 9898 字,大约阅读时间需要 32 分钟。

做android开发时你还在为gson,json而人肉序列化与反序列化吗,上传文件时你还在使用UrlConnection或者HttpClient吗?
下面提供了asp.net 服务端与 android 客户端通过hessdroid (hessian 的android版) 通信解决方案,从此你不用再为上面的问题发愁了
中小应用,使用.net 的EF(数据库优先)或linq,那是相当的方便,做个管理页面也只要拖拖控件就好了(不拖都对不起自己尤其是自己从头整到尾的项目), 当然你需要使用EmitMapper一类的东西将dto映射到EF实体类上。
服务端定义实体类

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace cn.fstudio.hessian.dto{    public class ResponseBase
{ private int code; private string msg; private T model; public int Code { get { return code; } set { code = value; } } public string Msg { get { return msg; } set { msg = value; } } public T Model { get { return model; } set { model = value; } } }}
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Web;using cn.fstudio.hessian.dto;namespace aa.model{        public class UserListResponse:ResponseBase
> { private DateTime? addTime; public DateTime? AddTime { get { return addTime; } set { addTime = value; } } private byte[] fileDate; public byte[] FileDate { get { return fileDate; } set { fileDate = value; } } private bool? isAdmin; public bool? IsAdmin { get { return isAdmin; } set { isAdmin = value; } } }}
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace aa.model{    public class UserInfo    {        private string userNo ;        public String UserNo        {            get            {                return userNo;            }            set            {                userNo = value;            }        }        private string username;        public string Username        {            get { return username; }            set { username = value; }        }        private string password;        public string Password        {            get { return password; }            set { password = value; }        }        private string mobile;        public string Mobile        {            get { return mobile; }            set { mobile = value; }        }        private string userType;        public string UserType        {            get { return userType; }            set { userType = value; }        }        private int recId;        public int RecId        {            get { return recId; }            set { recId = value; }        }        private int userLevel;        public int UserLevel        {            get { return userLevel; }            set { userLevel = value; }        }    }}
View Code

实体类定义不要写成public String Field{get;set;} //因为只动生成的字段名称可能是FieldName_<>K,hessian反射时会出错

服务端接口定义与实现

using System;using System.Collections.Generic;using System.Linq;using System.Text;using aa.model;namespace aa.service{    public interface UserService    {        UserListResponse getUserlist(UserListResponse res);        string hello(string name);         List
getUsers(); AInfo getAInfo(); }}
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using aa.service;using aa.model;using hessiancsharp.server;namespace AA.ServiceImpl{    public class UserServiceImpl:CHessianHandler, UserService    {        #region UserService 成员        public UserListResponse getUserlist(UserListResponse res)        {            var response = res;            UserInfo u = new UserInfo();            u.Username = "张三";            u.UserLevel = 88;            response.AddTime = DateTime.Now;            response.Model.Add(u);            response.Code = 1;            response.Msg = "终于可以了吗吗吗吗?";            return response;        }        #endregion        #region UserService 成员        public string hello(string name)        {            return DateTime.Now + "->" + name;        }        public List
getUsers() { UserInfo u = new UserInfo(); u.Username = "张三"; u.UserLevel = 88; return new List
() { u, u, u, u, u, u, u }; } #endregion #region UserService 成员 public AInfo getAInfo() { return new AInfo() { id = 18,name="还是中文" }; } #endregion }}
View Code

注意:mvc项目需要在routeConfig中加上 routes.IgnoreRoute("{hessian}.hessian/{*pathInfo}");

web.config中添加
   <httpHandlers>
      <add verb="*" path="UserService.hessian" type="AA.ServiceImpl.UserServiceImpl, AA.ServiceImpl" />
    </httpHandlers>

 

 客户端实体类:

package cn.fstudio.hessian.dto;import java.io.Serializable;public class ResponseBase
implements Serializable{ private static final long serialVersionUID = 1L; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getModel() { return model; } public void setModel(T model) { this.model = model; } private int code; private String msg; private T model;}
View Code
package aa.model;import java.io.Serializable;import java.util.Date;import java.util.List;import aa.model.UserInfo;import cn.fstudio.hessian.dto.ResponseBase;public class UserListResponse extends ResponseBase
> implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Date addTime; private byte[] fileDate; private Boolean isAdmin; public Date getAddTime() { return addTime; } public void setAddTime(Date addTime) { this.addTime = addTime; } public byte[] getFileDate() { return fileDate; } public void setFileDate(byte[] fileDate) { this.fileDate = fileDate; } public Boolean getIsAdmin() { return isAdmin; } public void setIsAdmin(Boolean isAdmin) { this.isAdmin = isAdmin; } }
View Code
package aa.model;import java.io.Serializable;public class UserInfo implements Serializable{    /**     *      */    private static final long serialVersionUID = 1L;    public String getUserNo() {        return userNo;    }    public void setUserNo(String userNo) {        this.userNo = userNo;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getMobile() {        return mobile;    }    public void setMobile(String mobile) {        this.mobile = mobile;    }    public String getUserType() {        return userType;    }    public void setUserType(String userType) {        this.userType = userType;    }    public int getRecId() {        return recId;    }    public void setRecId(int recId) {        this.recId = recId;    }    public int getUserLevel() {        return userLevel;    }    public void setUserLevel(int userLevel) {        this.userLevel = userLevel;    }    public static long getSerialversionuid() {        return serialVersionUID;    }    private String userNo;    private String username;    private String password;    private String mobile;    private String userType;    private int recId;    private int userLevel;    }
View Code

客户端接口定义

package aa.service;import java.util.List;import aa.model.AInfo;import aa.model.UserInfo;import aa.model.UserListResponse;public interface UserService {     public UserListResponse getUserlist(UserListResponse req);     public String hello(String name);     List
getUsers(); AInfo getAInfo();}
View Code
String url = "http://122.xxx.xxx.x:7086/UserService.hessian";                HessianProxyFactory factory = new HessianProxyFactory();                try {                                                            UserListResponse req=new UserListResponse();                    req.setAddTime(new Date());                    req.setCode(11);                    req.setFileDate(new byte[]{
3,2,1}); req.setIsAdmin(true); req.setMsg("这里我传了一些中文上去"); UserInfo u=new UserInfo(); u.setUsername("一个鸟人"); u.setMobile("13616761237"); u.setUserLevel(18); List
list=new ArrayList
(); for(int i=0;i<1000;i++) { list.add(u); } req.setModel(list); factory.setDebug(true); factory.setReadTimeout(5000 * 100); UserService basic = (UserService)factory.create(UserService.class, url,getClassLoader()); String response=basic.hello("xxx"); UserListResponse response2=basic.getUserlist(req); //Toast.makeText(FullscreenActivity.this, "调用结果:"+ response2.getCode(), Toast.LENGTH_LONG).show(); String msg=response2.getModel().get(response2.getModel().size() -1).getUsername() + "," + response2.getModel().size(); new AlertDialog.Builder(FullscreenActivity.this).setTitle(response2.getFileDate().length + "").setMessage(msg).show(); } catch (Exception e) { new AlertDialog.Builder(FullscreenActivity.this).setTitle("Error").setMessage(e.getMessage()).show(); }
View Code

程序参考华位网盘[软件任务与测试]

转载地址:http://lkfgx.baihongyu.com/

你可能感兴趣的文章
Sql与C#中日期格式转换总结
查看>>
iOS开发流程总结
查看>>
hadoop datanode 启动出错
查看>>
js颜色拾取器
查看>>
IDEA使用(1)intellIJ idea 配置 svn
查看>>
Thread Safety in Java(java中的线程安全)
查看>>
WPF 降低.net framework到4.0
查看>>
数据管理DMS 全量SQL诊断:你的SQL是健康的蓝色,还是危险的红色?
查看>>
搭建一个通用的脚手架
查看>>
开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
查看>>
开源磁盘加密软件VeraCrypt教程
查看>>
本地vs云:大数据厮杀的最终幸存者会是谁?
查看>>
阿里云公共镜像、自定义镜像、共享镜像和镜像市场的区别 ...
查看>>
shadowtunnel v1.7 发布:新增上级负载均衡支持独立密码
查看>>
IdleHandler,页面启动优化神器
查看>>
Java线程:什么是线程
查看>>
mysql5.7 创建一个超级管理员
查看>>
【框架整合】Maven-SpringMVC3.X+Spring3.X+MyBatis3-日志、JSON解析、表关联查询等均已配置好...
查看>>
要想成为高级Java程序员需要具备哪些知识呢?
查看>>
带着问题去学习--Nginx配置解析(一)
查看>>