2011-05-18 55 views

回答

4

到目前为止,我已经找到了最好如下:

  1. http://allseeing-i.com/ASIHTTPRequest/
  2. 使用JSON使用非常优异ASIHTTPRequest辅助类为您的请求和响应,并与解析http://code.google.com/p/json-framework/

我只是将上述类添加到我的项目中,并在必要时包含它们。然后:

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"]; 
} 

它真的很简单,可靠,很好的错误处理。

+0

非常感谢。例子看起来很简单。但是我的web服务使用的是GET方法,这是有点像这样的问题.http://85.17.168.204:8080/TunisairRESTful/resources/cities/ 请问你有没有例子? – siMonAkro 2011-05-18 02:42:56

+0

将请求的RequestMethod更改为@“GET”,并省略PostBody。 请阅读入门指南了解更多信息:http://allseeing-i.com/ASIHTTPRequest/How-to-use – 2011-05-18 02:46:10

0

打开safari并导航到适用于REST服务器的URL。

+1

罗布 - 我不是故意粗鲁,当我问 - 这是一个实际的答案,或者你在开玩笑吗?这真的有可能吗?如何将数据发布到服务器,以及如何解析响应? – 2011-05-18 02:34:51

+0

Ryan,这是一个半开玩笑,但它确实完全满足OP指出的问题,它没有说明与REST服务器通信时需要什么操作。 – 2011-05-18 02:37:17

+0

这是个玩笑,或者你是认真的? – siMonAkro 2011-05-18 02:39:45

相关问题