通过Web服务在iPhone和Internet之间进行通信

问题描述 投票:0回答:2

来自this link的@Pravara问了一个问题。

问题是这样的。

XML是在.Net Web服务和iPhone之间进行通信的唯一方式吗?我已经在我的应用程序中实现了它,但我遇到了性能问题,因为扫描每个XML标记所需的时间需要时间。

除了发送XML作为回应之外,任何人都可以建议任何通信方式。

我想知道同样的事情。

(我知道Web服务本身意味着XML,但我发现它非常典型,比如在创建Web服务时将整个数据库传输到XML并再次通过iPhone解析它 - 这是一项艰苦的工作。)

.net iphone web-services communication
2个回答
2
投票

JSON是一种更好的通信协议,因为它的体积小,易于集成。你想看看JSON.frameworkTouchJSONObjectiveResource


2
投票

Hessian是比JSON更好的通信协议。作为二进制格式,它更加紧凑,并且通过严格的格式解析速度更快。

作为奖励,已经有Java,.NET和PHP的框架来公开Web服务。真的很容易。假设你有这个C#界面:

public interface ITest {
  public string getGreeting();
  int addNumbers(int a, int b);
}

然后使用HessianC#在服务器上实现它很简单:

public class CTest:CHessianHandler, ITest {
  public string getGreeting() { return "Hello World!"; }
  public int addNumbers(int a, int b) { return a + b; }
  [STAThread]
  private static void Main(string[] args) {
    CWebServer web = new CWebServer(5667, "/test/test.hessian", typeof (CTest));
    web.Paranoid = true;
    web.AcceptClient("[\\d\\s]");
    web.Run();
    for (;; ) {
      if (Console.ReadLine() != "") {
        web.Stop();
        break;
      }
    }
  }
}

在iPhone方面,需要将C#接口转换为Objective-C协议:

@protocol ITest
-(NSString*)getGreeting;
-(int)addNumbers:(int)a :(int)b;
@end

然后使用HessianKit获取服务的代理几乎一样容易:

id<ITest> proxy = [CWHessianConnection proxyWithURL:serviceURL
                                           protocol:@protocol(ITest)];
NSLog(@"Greeting: %@", [proxy getGreeting]);
NSLog(@"The answer: %d", [proxy addNumbers:40 :2]);

在这个简短的回答中,方法名称不是C#-ish,也不是Obj-C-ish。这是因为默认情况下HessianKit使用Java的命名约定。这可以通过提供方法和类型名称翻译在HessianKit中覆盖。因此,连接上的C#和Obj-C两侧感觉100%在家。例如:

[CWHessianArchiver setClassName:@"com.mycompany.ITest" 
                    forProtocol:@protocol(CWTest)];
[CWHessianArchiver setMethodName:@"AddNumbers"
                     forSelector:@selector(addInt:toInt:)];
© www.soinside.com 2019 - 2024. All rights reserved.