views:

898

answers:

3

From the following link.


http://stackoverflow.com/questions/1290924/iphone-interaction-with-asp-net-webservice/1503206#1503206


@Pravara - Asked a Question in answer.

The question is something like this.


Hi All,

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.

Thanx in advance. :-)


I would like to know the same.

Thanks in advance for sharing your knowledge with us.

Sagar

(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 like laborious job. )

+2  A: 

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
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
well said, everything has benefits and drawbacks
slf
+2  A: 

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
A: 

Hi,

Its really interesting information you have posted here I clear few doubts from this information thanks for sharing here

Thanks

Addon Solutions