jsonparsing在ios + php中
我在PHP中创build了具有1个参数的1个函数的web服务。 我使用phpMyadmin作为后端。 它使用json格式来获取数据。 Web服务正在完美运行。 我想在我的iPhone应用程序中使用这个Web服务。 我想传递1个参数。 我也提到了rayWenderlich教程也作为参考。 但找不到解决办法。 请帮我一下
这是我的networking服务代码:
<?php echo getdata($_REQUEST['lastupdate']); function getdata($lastupdatedate){ $json = '{"foo-bar": 12345}'; $obj = json_decode($json); //print $obj->{'foo-bar'}; // 12345 $con = mysql_connect("localhost","un","Password"); if (!$con){ die('Could not connect: ' . mysql_error()); } //print_r($con); mysql_select_db("roster", $con); $query = "select * from rates where LastUpdated = '".$lastupdatedate."' order by LastUpdated limit 1"; $rs = mysql_query($query) or die($query); //print_r($rs); while($row=mysql_fetch_assoc($rs)){ $record[] = $row; } $data = json_encode($record); header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json'); return $data; }
iPhone的应用程序端,我已经尝试下面的代码:
-(void)GETJSONDATA { SBJSON *json = [SBJSON new]; json.humanReadable = YES; responseData = [NSMutableData data]; NSString *service = @""; NSString *str; str = @"LastUpdated"; NSString *requestString = [NSString stringWithFormat:@"{\"LastUpdated\":\"%@\"}",str]; // [[NSUserDefaults standardUserDefaults] setValue:nil forKey:@"WRONGANSWER"]; NSLog(@"request string:%@",requestString); NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]]; NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"]; NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc]; NSString *urlLoc = [fileContents objectForKey:@"URL"]; urlLoc = [urlLoc stringByAppendingString:@"?lastupdate=2012-09-01 01:00:00"]; NSLog(@"URL : %@",urlLoc); NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: urlLoc]]; NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]]; [request setHTTPMethod: @"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody: requestData]; NSError *respError = nil; NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ]; if (respError) { // NSString *msg = [NSString stringWithFormat:@"Connection failed! Error - %@ %@", // [respError localizedDescription], // [[respError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ACTEC" message:@"check your network connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } else { NSUserDefaults *dd = [NSUserDefaults standardUserDefaults]; NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding]; NSLog(@"Resp : %@",responseString); NSDictionary *results = [responseString JSONValue]; NSLog(@"results=%@",results); } }
你可能想用一个库来看看。 AFNetworking提供了一个名为AFHTTPClient的类,它允许您连接到一个安静的Web服务并进行简单的调用。 假设一个终点,你可以做这样的事情:
NSDictionary *body = [NSDictionary dictionaryWitObject:@"LastUpdated" forKey:@"LastUpdated"]; NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"]; NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc]; NSString *urlLoc = [fileContents objectForKey:@"URL"]; urlLoc = [urlLoc stringByAppendingString:@"?lastupdate=2012-09-01 01:00:00"]; AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:urlLoc]]; [client postPath:@"endpoint" parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) { // Do something with the responseObject } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // Log the error and proceed }];
你可以在这里find文档: http : //engineering.gowalla.com/AFNetworking/Classes/AFHTTPClient.html