NSUrl POST 错误
Posted
技术标签:
【中文标题】NSUrl POST 错误【英文标题】:NSUrl POST Error 【发布时间】:2011-12-27 23:24:51 【问题描述】:我有一个页面,它接收用户的邮政编码,将该数据发送到 php 页面,然后通过 JSON 检索数据。这个页面抛出一个错误,上面写着:
2011-12-27 15:17:49.919 BusinessManager[3595:20b] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[NSConcreteData isFileURL]:发送了无法识别的选择器到实例 0x4751490'
代码是:
- (id)initWithNibName:(NSString *)SearchZip bundle:(NSBundle *)nibBundleOrNil
if (self = [super initWithNibName:SearchZip bundle:nibBundleOrNil])
// Custom initialization
return self;
- (IBAction) searchzip: (id) sender
NSString *post =[NSString stringWithFormat:@"zipcode=%@",zipField.text];
NSString *hostStr = @"https://www.mysite.com/searchzip.php?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:dataURL];
self.zipArray = [jsonData JSONValue];
[jsonData release];
- (void)viewDidLoad
[super viewDidLoad];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [zipArray count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
NSDictionary *infoDictionary = [self.zipArray objectAtIndex:indexPath.row];
static NSString *Prospects = @"agencyname";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Prospects];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:Prospects] autorelease];
// setting the text
cell.text = [infoDictionary objectForKey:@"agencyname"];
self.navigationItem.title = @"Zip Search";
// Set up the cell
return cell;
【问题讨论】:
【参考方案1】:您正在尝试将NSData
的实例传递给-initWithContentsOfURL:
以创建jsonData
的NSString
实例。
我正要对你的方法进行拉皮条。请注意,我已完全删除创建 NSData
对象并重命名变量以更清晰。
- (IBAction)searchzip:(id)sender
NSString *post = [NSString stringWithFormat:@"zipcode=%@", zipField.text];
NSString *hostString = @"https://www.mysite.com/searchzip.php?";
// Append string and add percent escapes
hostString = [[hostString stringByAppendingString:post] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *hostURL = [NSURL URLWithString:hostString];
NSString *jsonString = [[NSString alloc] initWithContentsOfURL:hostURL];
self.zipArray = [jsonString JSONValue];
[jsonString release];
【讨论】:
以上是关于NSUrl POST 错误的主要内容,如果未能解决你的问题,请参考以下文章
出现错误 - (BOOL)copyItemAtURL:(NSURL *)srcURL toURL:(NSURL *)dstURL 错误:(NSError **)error