【问题标题】:NSUrl POST ErrorNSUrl POST 错误
【发布时间】: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;

 }

【问题讨论】:

    标签: objective-c cocoa-touch iphone-sdk-3.0


    【解决方案1】:

    您正在尝试将NSData 的实例传递给-initWithContentsOfURL: 以创建jsonDataNSString 实例。

    我正要对你的方法进行拉皮条。请注意,我已完全删除创建 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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 2011-03-20
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      相关资源
      最近更新 更多