同步get方法请求:
web service 请求过程:
- (void) startRequest
{
NSString strURL = [[NSString alloc] initWithFormat:@"http://iosbook3.com/service/mynotes/webservice.php?email =%@&type=%@&action=%@", @"你的iosbook1.com用户邮箱", @"JSON", @"query"];
NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"请求完成。。。");
NSDictionary *resDict = [NSJONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
[self reloadView:resDict];
}
其中 NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]]; 中URLEncodedString方法将字符串转化为URL字符串,在Internet传输的时候URL中不能有中文等特殊字符,,该方法来自自己编写的分类(NSString+URLEncoding.h),例如:"<“符号的URL编号是”%3C“,使用该方法就是把这些字符串转换为有百分号的URL编码。
@interface NSString (URLEncoding)
- (NSString *) URLEncodedString;
- (NSString *) URLDecodedString;
@end
@implementatoin NSString (URLEncoding)
- (NSString *)URLEncodedString
{
NSString *result = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR(”+ $, # [ ]") kCFStringEncodingUTF8));
return result;
}