Create NSArray with row content (MySQL)
I want to create a NSArray witch contains the content of a MySQL table row
I use a php script to get the data
$response = $bdd->query('SELECT * FROM iPhoneTests');
while ($datas = $response->fetch())
{
?>
<array>
<integer><?php echo $datas['Temperature']; ?></integer>
</array>
<?php
}
and to test Iuse this code in my app
NSURL *getTemperature = [NSURL URLWithString:@"myURL..."];
NSError* error;
temperature = [NSArray arrayWithContentsOfURL:getTemperature ];
NSLog(@"%@",temperature);
When I test in my web broswe开发者_运维问答r I get all my datas but in my app, with the NSlog I only get the first value.
I on this problem since a few time, but I can't succeed If someone see my mistake please tell me
You are creating an array for each row (which by the way is invalid, because those arrays would have to be nested into an array, too).
Do it in this way:
$response = $bdd->query('SELECT * FROM iPhoneTests');
?><array><?
while ($datas = $response->fetch())
{
?>
<integer><?php echo $datas['Temperature']; ?></integer>
<?
}
?></array>
We can communicate in many ways with server. And I would suggest that communication data should only be in String format, one way is using JSON, you can get JSON parser from here https://github.com/schwa/TouchJSON , this is explained as below:
Starting Communication
[[[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:urlStr]] delegate:self startImmediately:YES] autorelease]; //urlStr is NSString ref
Start receiving data
- (void)connection:(NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response{ [receivedData setLength:0]; } //receivedData is NSMutableData ref
Get data in chunks
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [receivedData appendData:data]; }
After getting complete data
(void)connectionDidFinishLoading:(NSURLConnection *)connection{
// json data retrival complete (received data is NSMutableData) if(receivedData == nil || [receivedData length] == 0){ //handle error in getting response, may be no network [self showAlertViewWithTitle:@"No network" andMessage:@"Could not open the page, because internet connection was not found." andTag:0]; return; }
NSString *responseBody = [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease]; ////NSLog(responseBody); NSError *jsonParsingError = [[[NSError alloc] init] autorelease]; NSDictionary * jsonData = [NSDictionary dictionaryWithJSONData: [responseBody dataUsingEncoding:NSUTF8StringEncoding] error:&jsonParsingError];
NSDictionary *serverError = [jsonData objectForKey:@"error"]==[NSNull null]? nil:[jsonData objectForKey:@"error"];
if(serverError==nil || [serverError count]==0){ // no error found else NSDictionary *response = [jsonData objectForKey:@"response"]==[NSNull null]? nil:[jsonData objectForKey:@"response"]; NSArray *array = [response objectForKey:@"array"]==[NSNull null]?nil: [response objectForKey:@"array"]; // I have left the code incomplete, its just for understanding
Check for null values Never forget to check null values from JSON data as follows:
NSString *dataValue = [[[array objectAtIndex:i] objectForKey:@"key"]==[NSNull null]? @"0":[[array objectAtIndex:i] objectForKey:@"key"] intValue];
Please let me know if you require further explanation..
精彩评论