在 iPhone 应用程序中使用 REST Web 服务的最简单、最简单的方法是什么,而不是最好的方法?谢谢。
目前我发现的最好的如下:
我刚刚将上述类添加到我的项目中,并在必要时包含它们。那么:
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"OST"];
[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 performSelectorOnMainThreadselector(serviceConnectionDidFail) withObject:nil waitUntilDone:NO];
}
else
{
// request succeeded - parse response
NSDictionary *responseDict = [self.sbjson objectWithString:[request responseString]];
int value = [responseDict valueForKey"ServerValue"];
}
它非常简单、可靠且错误处理良好。
关于objective-c - 使用 REST 服务的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6039179/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |