iOS开发Hessian

HessianKit使用参考资料比较少,通过摸索,把测试过程贴出来,代码很乱,未整理,先实现功能,再应用到项目中.供新手参考.如有问题,跟帖指正...

HessianService与Java Server端的接口一样.

import <UIKit/UIKit.h>

import "CWHessianArchiver.h"

import "User.h"

@protocol HessianService

-(NSString *) sayHello; //定一个无参方法,返回string类型

-(NSString *) yy:(NSString *)userName;//传递一个string参数,返回string参数

-(id<User>) getHessianModel;//无参,返回一个User类型的对象.在Java Server端中有一个实体类与之对应

-(BOOL)save:(NSString *)userName;

-(NSArray *)listAll;

-(void)save2:(NSString *)username password:(NSString *)password;

-(int)syn:(NSMutableArray *)list;

-(int)suser:(id<User>)user;

-(int)slist:(NSMutableArray<User>*)list; //传递list 数组

@end

//以下是测试方法 要引入相关的头文件

//server URL

NSURL *url = [NSURL

URLWithString:@http://192.168.1.102:8080/SignInService/mvc/hessian/hessianService];

//设置代理类

id<HessianService> proxy = (id<HessianService>)[CWHessianConnection proxyWithURL:url

protocol:@protocol(HessianService)];

//客户端与服务器端的方法关联.可以用不一样的方法名.通过 CWHessianArchiver 映射,就可以解决找不到方法

[CWHessianArchiver setMethodName:@"yy_1" forSelector:@selector(yy:)];

[CWHessianArchiver setMethodName:@"getHessianModel" forSelector:@selector(getHessianModel)];

[CWHessianArchiver setMethodName:@"save" forSelector:@selector(save:)];

[CWHessianArchiver setMethodName:@"listAll" forSelector:@selector(listAll)];

[CWHessianArchiver setMethodName:@"save2" forSelector:@selector(save2:password:)];

[CWHessianArchiver setMethodName:@"syn" forSelector:@selector(syn:)];

//User 与服务器端的com.listentek.signIn.server.model.User进行关联映射,否则找不到实体类.带上包名.

[CWHessianArchiver setClassName:@"com.listentek.signIn.server.model.User"forProtocol:@protocol(User)];

[CWHessianArchiver setMethodName:@"slist" forSelector:@selector(slist:)];

//调用方法很简单,

//调用sayHello方法

NSLog(@"xxx:%@",[proxy sayHello]);

NSString *re = @"hello";

NSLog(@"web:%@",[proxy yy:re]);

id<User> user = [proxy getHessianModel];

NSLog(@"name=%@",[user objectForKey:@"password"]);

//传递单个数据

NSLog(@"save:%@",[proxy save:re]);

//获取list数据

NSArray *array = [proxy listAll];

for(id user2 in array){

NSLog(@"name=%@",[user2 objectForKey:@"password"]);

}

[proxy save2:@"hehe" password:@"yyy"];

//声明实体类 查看CWValueObject 源码,  valueObjectWithProtocol方法中,为@protocol User 初始化,分配内存.

id<User> u = (id<User>)[CWValueObject valueObjectWithProtocol:@protocol(User)];

[u setPassword:@"xxx"];

[u setUserName:@"yyy"];

NSLog(@"%@",u);

NSLog(@"pass=%@,name=%@",u.password,u.userName);

[proxy suser:u];

//传递数组

NSMutableArray  *array3 = [[NSMutableArray  alloc] init];

[array3 addObject:u];

for (int k=1; k<3; k++) {

id<User> u2 = (id<User>)[CWValueObject valueObjectWithProtocol:@protocol(User)];

u2.password = [NSString stringWithFormat:@"k%d",k];

u2.userName = [NSString stringWithFormat:@"k%d",k];

[array3 addObject:u2];

}

[proxy slist:array3];

上一篇:SQL Server中Text和varchar(max)数据类型区别


下一篇:将文件导入到SQL server数据库表中的字段中