我是新來的Objective-C和我開始把精力大量進入請求/響應由於最近在HTTP請求發送JSON數據。我有一個可以調用url(通過http GET)並解析返回的json的工作示例。如何使用的NSURLRequest
這樣做的工作例子如下
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
//do something with the json that comes back ... (the fun part)
}
- (void)viewDidLoad
{
[self searchForStuff:@"iPhone"];
}
-(void)searchForStuff:(NSString *)text
{
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.whatever.com/json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
我的第一個問題是 - 將這種做法擴大?或者這是不是異步(意思是我阻止用戶界面線程而應用程序正在等待響應)
我的第二個問題是 - 我怎麼可能會修改這一請求的一部分做了POST,而不是得到什麼?是否只是像這樣修改HttpMethod?
[request setHTTPMethod:@"POST"];
最後 - 我怎麼一組JSON數據添加到這個崗位作爲一個簡單的字符串(例如)
{
"magic":{
"real":true
},
"options":{
"happy":true,
"joy":true,
"joy2":true
},
"key":"123"
}
預先感謝您
這是一個教程:HTTP:// mobileorchard .com/tutorial-json-over-http-on-iphone/ – Josh 2010-12-16 02:33:04