`
dadi520
  • 浏览: 139488 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

ios Hessian Kit 包使用

    博客分类:
  • ios
 
阅读更多

 

下载地址: http://sourceforge.net/projects/hessiankit/

介绍:http://www.jayway.com/2008/07/10/hessiankit-released/

协议介绍 http://hessian.caucho.com/

 

HessianKit

HessianKit is a Framework for Objective-C 2.0 to allow Mac OS X 10.5, and iPhone 2.0 applications to seamlessly communicate with hessian web services.

HessianKit provided typed proxy objects to formward calls to, and and handle responses from any hessian web service, seamlessly just as if they where local objects. HessianKit also provide functionality to automatically generate classes for published value objects. Both web service proxies and value objects are defined as basic Objective-C Protocols.

Unless the client uses custom value objects, CWHessianConnection is the only class in HessianKit that is needed to be created directly. Instances of CWDistantHessianObject are fetched from CWHessianConnection using proxyWithURL:protocol:. If custom value objects are needed use the CWValueObject to realize objects using protocol definitions, or implement classes conforming to CWNSCoding protocol.

If the web service exposes the following Java-interface:

public interface Service {
  int getPersonCount();
  Person getPerson(int index);
}

Then the Objective-C client would define the service Protocol as this:

@protocol Service
-(int)getPersonCount;
-(id<Person>)getPerson:(int)index;
@end

Assumin Person is a value object defined as this in Java:

public class Persson implements Serializable {
  private int age;
  private String name;
  public Person(int age, String name) { setAge(age); setName(name); }
  public int getAge() { return age; }
  public void setAge(int v) { age = v; }
  public String getName() { return name; }
  public void setName(String v) { name = v; }
}

The Objective-C client would define the same value object as a Protocol like this:

@protocol Person
@property(assign, nonatomic) int age;
@property(retain, nonatomic) NSString* name;
@end

Value object from the web service will automatically be created as a subclas of CWValueObject if the class name matches a defined protocol. Value object to be sent to the web service can be created useing the class method valueObjectWithProtocol: of CWValueObject, or by using any class conforming to NSCoding.

Given the above example the code to get all available persons and write their age and name to logs, could be wimplemented as this:

NSURL* url = [URL URLWithString:@"http:/someserver.org/hessianservice"];
id<Service> proxy = [CWHessianConnection rootProxyWithURL:url protocol:@protocol(Service)];
int count = [proxy getPersonCount];
for (int index = 0; index < count; index++) {
  id<Person> person = [proxy getPerson:index];
  NSLog(@"age: %d name: %@", person.age, person.name);
}

Since Java and Objective-C differ in the naming of methods, some translation must be done when sending call tho the hessian web service. Method names are first looked up using the class method methodNameForSelector: of CWHessianArchiver, if a method name is not returned, the method name to use will be generated with these steps:

  1. Split the selector name on ':' delimiter character.
  2. Capitalize first charcter of all splitted parts but the first.
  3. Join splitted parts without delimiter.
  4. If method takes one or more arguments the nme is mangled by appending "__#" where # is the number of arguments.

    Some examples of method tranlsations:

    Objective-C Java
    doFoo doFoo
    getFoo: getFoo__1
    doFoo:bar: doFooBar__2
    doUgly::: doUgly__3

    Many Objective-C constructs feels alien in Java and vice versa, therefor non-automatic method translation can somethimes be prefered, in the above interface Service the method namedperson is not good looking Objective-C, albeit acceptable Java. To add a manual translation just do:

    [CWHessianArchiver setMethodName:@"getPersonCount" forSelector:@selector(personcount)];
    [CWHessianArchiver setMethodName:@"getPerson" forSelector:@selector(personAtIndex:)];

    And change the Objective-C Protocol accordingly:

    @protocol Service
    -(int)personCount;
    -(id<Person>)personAtIndex:(int)index;
    @end

    And now the much more Objective-C friendly code can be written:

    NSURL* url = [URL URLWithString:@"http:/someserver.org/hessianservice"];
    id<Service> proxy = [CWHessianConnection proxyWithURL:url protocol:@protocol(Service)];
    int count = [proxy personCount];
    for (int index = 0; index < count; index++) {
      id<Person> person = [proxy personAtIndex:index];
      NSLog(@"age: %d name: %@", person.age, person.name);
分享到:
评论
1 楼 Davidream 2014-05-08  
楼主:
       HessianKit库,我从你提供的地址下载出来了,但支持的版本太低了,当前我xcode5不能支持,不能编译静态库,源文件加载也不顺利,编译不过去。想知道楼主怎么提取的库。请加我扣扣沟通一下可以么?谢谢了,327932709谢谢。急等回复

相关推荐

Global site tag (gtag.js) - Google Analytics