2011-12-27 67 views
1

我有一個頁面,需要從用戶的郵政編碼,將該數據發送到一個PHP頁面,然後通過JSON檢索數據。此頁面拋出一​​個錯誤,指出:NSUrl POST錯誤

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; 

} 
+0

我該怎麼辦... – savagenoob 2011-12-27 23:38:23

+0

K它是什麼... – savagenoob 2011-12-27 23:48:06

回答

1

您正在嘗試的NSData一個實例傳遞到-initWithContentsOfURL:來創建NSStringjsonData實例。

我即將皮條客你的方法。請注意,我已經刪除了完全創建一個對象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]; 
}