开发者

iPhone: making web calls to example.com API

I want to verify that from my iPhone App, I can call my custom website A开发者_StackOverflow中文版PI (PHP) and send/receive information without any issues. If so, if there a preferred transaction language? XML, JSON, something else?

I'm just not sure if any web calls need to be made through SSL only or not... just a harmless question.


You can use either XML or JSON. JSON is considered more light weight than XML though.


The SDK lacks much native support for XML and JSON. Third party library support is a bit better for JSON - I'd use that.

You don't need to use SSL - you can use whatever protocol and port you want.

You could do something like [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];, with NSMutableURLRequest, NSURLResponse, and NSError as the three parameters.


I think json should be a good medium for parsing. json-framework makes it easy to turn NSDictionary and primitive objects into json data and vice versa.

Makes web services using json a breeze.

{ name : value , name : value } = NSDictionary
[ value , value , value] = NSArray
NSString, NSNumber, BOOL and NSNull are the items it create, I believe.

check out this page https://github.com/stig/json-framework

I will include some code I used that is built against the json-framework and works with json web services.

- (NSDictionary*) sendJSONRPCRequestTo:(NSString*) url 
                            forCommand:(NSString*)command 
                        withParamaters:(NSMutableArray*) parameters 
                           synchronous:(BOOL) sendSynchronous
{

    if (self.commandId == nil)
    {
        self.commandId = @"1";//Just set a commandID
    }

    NSMutableURLRequest *request = [self.baseTransaction makeNewRequestFor:url];

    NSMutableDictionary *mainPackage = [NSMutableDictionary dictionary];
    [mainPackage setValue:self.commandId forKey:@"id"];
    [mainPackage setValue:command forKey:@"method"];
    [mainPackage setValue:parameters forKey:@"params"];

    NSString *jsonData = [mainPackage JSONRepresentation];

    [request setValue:command forHTTPHeaderField:@"X-JSON-RPC"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    if (jsonData != nil && [jsonData isEqual:@""] == NO)
    {
        [request setHTTPMethod:@"POST"];
        [request setValue:[[NSNumber numberWithInt:[jsonData length]] stringValue] forHTTPHeaderField:@"Content-Length"];
    }

    [request setHTTPBody:[jsonData dataUsingEncoding:NSUTF8StringEncoding]];
    if (sendSynchronous)
    {
        NSHTTPURLResponse   * response = nil;
        NSError             * error = nil;

        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

        NSString *jsonResult = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];

        NSDictionary *jsonDict = nil;

        @try 
        {
            jsonDict = [jsonResult JSONValue];
        }
        @catch (NSException * e) 
        {
            NSLog(@"Error: %@",jsonResult);
            jsonDict = [NSMutableDictionary dictionary];
                [jsonDict setValue:self.commandId forKey:@"id"];
                [jsonDict setValue:@"Unable to call function on server" forKey:@"error"];
                [jsonDict setValue:[NSNull null] forKey:@"result"];
        }
        @finally
        {
            return jsonDict;
        }
    }

    return nil;
}

This code is used against jayrock webservices on a .net server.


You can make calls to any URL using an HTTP library or built in Cocoa Framework code.

I recommend ASIHTTPRequest, which allows for asynchronous HTTP requests and easy progress reporting.

You can parse returned data (as XML) with the NSXMLParser class. The downside to this class is that it's linear and not so flexible. Ray Wenderlich has an article on the topic of XML parsing on iOS.

See this question which mentions a JSON parser for iOS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜