這篇主要是利用POST把iPhone上的東西傳給RoR Server
iPhone的寫法就是這樣
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/getTest"];
NSString* content = @"first=michael&second=pan";
NSMutableURLRequest* urlRequest = [[NSMutableURLRequest alloc]initWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[content dataUsingEncoding:NSASCIIStringEncoding]];
NSURLConnection * myConnection = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"response %@", response.URL );
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"received data %@", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease ]);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"error occurs");
}
在RoR端
直接用
http://tinyurl.com/28b4w23 設定
在Controller的部分改成這樣
=====================
class EchoTestController < ApplicationController
skip_before_filter :verify_authenticity_token
def index
render :text => 'Hello ' + params["first"]
end
end
=====================
這樣就可以了
在iPhone端會收到
2010-09-24 16:07:05.519 AsiTest[15495:207] response http://127.0.0.1/getTest
2010-09-24 16:07:05.521 AsiTest[15495:207] received data Hello michael
沒有留言:
張貼留言