开发者

Web services V HTTP Post?

I am trying to decide on what is the best option for a small application I am trying to write while learning how to develop for the iPhone. I want the application to send a request to my website, which will in turn update the database and alert a user on the web screens. If this happens successfully a message should appear on the iPhone.

My database is MySQL and I am using PHP for the site. I had considered using REST or SOAP, but now I am considering just using the POST array on a HTTP request. Which is the best option to use and easiest to implement for a beginner?

Thanks, BON

BTW Sorry 开发者_运维百科if this has all been asked before, but I am trying to learn Objective C at the min and this is all new to me.


There isn't much more to REST than basic HTTP - the only parts that REST adds are conventions as to which HTTP verbs (POST / GET / PUT / DELETE) should map to which database operations, and how URL paths map to resources.

SOAP is not a wise choice; it doesn't offer anything truly interesting in this scenario, but introduces considerable overhead (both in terms of bandwidth and processing complexity).

My choice would be a simple JSON interface over plain HTTP. If you're running PHP under Apache, you can simply rewrite all requests to one handler script, and from there, analyze the original URL to perform the desired action. Returning JSON is simple and straightforward: build a nested array structure, call json_encode and echo the result. Whether you follow RESTful conventions (HTTP verbs indicate action) or simply use query string parameters (e.g. ?action=insert) is a matter of taste - query string parameters are easier to implement, HTTP verbs are closer to the RESTful philosophy.


I would definitely recommend you to use simple HTTP. It gets you going very fast and it's easy to implement.

To create a request :

NSString *url = @"your request";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if(connection) 
{
    _systemsData = [[NSMutableData data] retain];
}
else 
{
    NSLog(@"NSURLConnection failed!");
}

Then just implement the methods:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜