zoukankan      html  css  js  c++  java
  • asp.net hessian + android hessdroid


    做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<T>
        {
            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<List<UserInfo>>
        {
            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<UserInfo> 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<UserInfo> getUsers()
            {
                UserInfo u = new UserInfo();
                u.Username = "张三";
                u.UserLevel = 88;
                return new List<UserInfo>() { 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<T> 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<List<UserInfo>> 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<UserInfo> 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<UserInfo> list=new ArrayList<UserInfo>();
                        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

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

  • 相关阅读:
    04邻接表深度和广度遍历DFS_BFS
    03邻接矩阵的深度和广度遍历的C语言实现
    02邻接表创建的C语言实现
    01邻接矩阵的创建C语言实现
    GUN的相关使用
    JAVA学习笔记
    排序的C语言实现
    线索二叉树的C语言实现
    maven配置logback
    多线程概念
  • 原文地址:https://www.cnblogs.com/wdfrog/p/3275281.html
Copyright © 2011-2022 走看看