0

From this link, @Pravara asked a question in answer.

The question is something like this.

Is the XML is only way to communicate between .Net web service and iPhone? I have implemented it, in my application, but I am facing performance issue, as the time required to scan each tag of XML takes time.

Please can anybody suggest any way for communication other than sending XML in response.

I would like to know the same.

(I know web service means itself XML, But I found it very typical, like transfer entire database to XML while creating web service & again parse it by iPhone - is a laborious job.)

Community
  • 1
  • 1
sagarkothari
  • 24,520
  • 50
  • 165
  • 235

2 Answers2

2

JSON is a better communication protocol because of it's small size and ease of integration in my opinion. You'll want to check out JSON.framework, TouchJSON, and ObjectiveResource

slf
  • 22,595
  • 11
  • 77
  • 101
  • Better is, of course, subjective. JSON is great for the reasons you've specified, but it is terribly painful to use if you have data structured in deep hierarchies. It's a lot easier to query an XML document with XPath in those cases compared to the cascade a dictionary within a dictionary within a dictionary, etc... type structure you get when you convert a JSON string using one of the libraries you've linked. I'm all for JSON and said libraries, but sometimes XML is in fact "better". – Matt Long Oct 01 '09 at 17:12
  • well said, everything has benefits and drawbacks – slf Oct 02 '09 at 01:31
2

Hessian is an even better communication protocol than JSON. Being a binary format it is even more compact, and with a strict format parsing is much faster.

As a bonus there are already frameworks for Java, .NET and PHP to expose a web service. Truly easy. Asume you have this C# interface:

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

Then implementing it on the server using HessianC# is a snap:

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;
      }
    }
  }
}

On the iPhone side the C# interface need to be translated into an Objective-C protocol:

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

And then using HessianKit for getting a proxy for the service is almost as easy:

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

In this short answer the method names are not quite C#-ish, an not quite Obj-C-ish either. This is because by default HessianKit uses Java's naming conventions. This can be overriden in HessianKit by providing method, and type name translations. So that both the C# and the Obj-C sides on the connection feels 100% at home. For example:

[CWHessianArchiver setClassName:@"com.mycompany.ITest" 
                    forProtocol:@protocol(CWTest)];
[CWHessianArchiver setMethodName:@"AddNumbers"
                     forSelector:@selector(addInt:toInt:)];
PeyloW
  • 36,742
  • 12
  • 80
  • 99