可能重复:
Tutorials for using HTTP POST and GET on the iPhone in Objective-C
是否可以创建一个具有正确信息的NSArray,如id = 1,name =@John,score = 100,然后发送它并从服务器接收响应?
可能在NSLog()中显示它;
有人能帮我回答这个问题吗?我也不想使用ASIHTTPRequest。我知道这要简单得多,但是如果不使用大量预先编写的代码id就可以完成一些事情,那么最好在使用别人的类之前,先学习如何使用基础框架提供的功能来做一些事情。
发布于 2011-09-05 22:22:10
使用NSData发送url请求并存储响应可能更容易,然后再重新创建轮子。下面是一些类似于我的生产项目的代码:
+ (NSData *)getProfiles {
NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
// Create string of the URL
NSString *serviceURL = [NSString stringWithFormat:@"http://www.myurlhere.com/getProfiles.php?token=%@", token];
NSLog(@"Service URL : %@", serviceURL);
// Create a NSURL out of the string created earlier. Use NSASCIIStringEncoding to make it properly URL encoded (replaces " " with "+", etc)
NSURL *URL = [NSURL URLWithString:[serviceURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
// Request the url and store the response into NSData
NSData *data = [NSData dataWithContentsOfURL:URL];
if (!data) {
return nil;
}
// Since I know the response will be 100% strings, convert the NSData to NSString
NSString *response = [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
// Test response and return a string that an XML Parser can parse
if (![response isEqualToString:@"UNAUTHORIZED"]) {
response = [response stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
data = [response dataUsingEncoding:NSASCIIStringEncoding];
return data;
} else {
return nil;
}
}NSLog输出:
[Line: 476] +[WebSupport getProfiles]: Service URL : http://www.myurlhere.com/getProfiles.php?token=abcdef0123456789abcdef0123456789https://stackoverflow.com/questions/7313123
复制相似问题