如何使用NSURLRequest在Http請求中發(fā)送json數(shù)據(jù)我是Objective-c的新手,我開始在最近的請求/響應(yīng)中投入大量精力。我有一個可以調(diào)用url(通過http GET)并解析返回的json的工作示例。這個工作的例子如下- (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];}我的第一個問題是 - 這種方法會擴(kuò)大嗎?或者這不是異步(意味著我在應(yīng)用程序等待響應(yīng)時阻止UI線程)我的第二個問題是 - 如何修改此請求部分以執(zhí)行POST而不是GET?它只是簡單地修改HttpMethod嗎?[request setHTTPMethod:@"POST"];最后 - 如何將一組json數(shù)據(jù)作為簡單字符串添加到此帖子中(例如){
"magic":{
"real":true
},
"options":{
"happy":true,
"joy":true,
"joy2":true
},
"key":"123"}先感謝您
- 3 回答
- 0 關(guān)注
- 1262 瀏覽
添加回答
舉報(bào)
0/150
提交
取消