在iPhone应用程序中使用REST Web服务的最简单,最简单的方法是什么,而不是最好的方法?感谢。
答案 0 :(得分:4)
到目前为止,我发现的最好成绩如下:
我刚将上面的类添加到我的项目中,并在必要时包含它们。然后:
NSURL *url = @"http://example.com/rest/whatever";
// create dictionary with post data
NSMutableDictionary *requestDict = [[NSMutableDictionary alloc] init];
[requestDict setObject: Id forKey:@"Id"];
[requestDict setObject: field.text forKey:@"Name"];
//Prepare the http request
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL: url];
[request setRequestMethod:@"POST"];
[request addRequestHeader:@"Content-Type" value:@"raw"];
[request setPostBody: [NSMutableData dataWithData: [[[self sbjson] stringWithObject:requestDict] dataUsingEncoding:NSUTF8StringEncoding]]];
[request startSynchronous];
[requestDict release];
self.error = [request error];
if(self.error)
{
[self performSelectorOnMainThread:@selector(serviceConnectionDidFail) withObject:nil waitUntilDone:NO];
}
else
{
// request succeeded - parse response
NSDictionary *responseDict = [self.sbjson objectWithString:[request responseString]];
int value = [responseDict valueForKey:@"ServerValue"];
}
它非常简单,可靠,并且具有良好的错误处理能力。
答案 1 :(得分:0)
打开safari并导航到REST服务器的相应URL。