ios iPhone:对example.com进行Web调用 API

bgibtngc  于 5个月前  发布在  iOS
关注(0)|答案(4)|浏览(75)

我想验证从我的iPhone应用程序,我可以调用我的自定义网站API(PHP)和发送/接收信息没有任何问题.如果是这样,如果有一个首选的交易语言?XML,JSON,其他?
我只是不确定是否有任何Web调用需要通过SSL进行或不.只是一个无害的问题。

nwo49xxi

nwo49xxi1#

你可以使用XML或者JSON。JSON被认为比XML更轻量级。

bpsygsoo

bpsygsoo2#

SDK缺乏对XML和JSON的原生支持。第三方库对JSON的支持稍好一些-我会使用它。
你不需要使用SSL -你可以使用任何你想要的协议和端口。
您可以执行类似[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];的操作,并将NSMutableURLRequestNSURLResponseNSError作为三个参数。

jei2mxaa

jei2mxaa3#

我认为json应该是一个很好的解析媒介。json-framework可以很容易地将NSDictionary和原始对象转换为json数据,反之亦然。
让使用json的web服务变得轻而易举。

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

字符串
查看此页https://github.com/stig/json-framework
我将包括一些我使用的代码,这些代码是基于json框架构建的,并与json web服务一起工作。

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


此代码用于.net服务器上的jayrock webservices。

im9ewurl

im9ewurl4#

您可以使用HTTP库或内置的可可Framework代码调用任何URL。
我推荐ASIHTTPRequest,它允许异步HTTP请求和简单的进度报告。
您可以使用NSXMLParser类解析返回的数据(作为XML)。该类的缺点是它是线性的,不太灵活。Ray Wenderlich有an article on the topic of XML parsing on iOS
参见this question,其中提到了JSON parser for iOS

相关问题