1

i want to step into the mySQL, JSON iPhone world. I set up a simple weather station with my Arduino and send the temperature to my MySQL database. I created a php file on my server.

When i open the file in Safari it looks like this:

{"weatherstation":[{"location":"indoor","celsius":"22.85"}]} 

Now i want to create a simple Application for my iPhone which displays the temperature. Can someone help me out some code? I searched here on stack overflow, but the most people are more advanced than i am.

Almost every code began with:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURL *url = [NSURL URLWithString:@"http://myWebsite.com/myPHPFile.php"]; NSError *error = nil; 

I would be very thankful if someone could help me to step into this topic.

EDIT

Here is some code of my php file. how can i change it to a JSON type? i tried it with

header('Content-Type: application/json'); 

But afterwards my php file shows the whole html structure (< html>...< /html>). do i have to change the html part if i change the content type to json?

<?php ... header('Content-Type: application/json'); ... <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="application/json; charset=utf-8"> <meta name="viewport" content="user-scalable=yes, width=device-width"> <title>My Temperature</title> </head> <body> <?php if(!isset($E)) { ?> {"weatherstation":[{"location":"indoor","celsius":"<?php echo $temp;?>"}]} <?php } else { echo $M; } ?> </body> </html> 
1
  • Please remove html tags it should only have php without space or, tabs or newline characters etc.. See my edit for PHP thing.. Commented Nov 12, 2013 at 22:17

2 Answers 2

1

Using following code you can easily get your json data work

-(void)viewDidLoad{ //Call function to fetch temperature dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self getTemperature]; }); } -(void)getTemperature{ NSData *jsonData=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://myWebsite.com/myPHPFile.php"]]; if(jsonData){ NSJSONSerialization *result=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil]; //Here you get your json parsed NSDictionary *json=(NSDictionary *)result; //Get the weather array from your json NSArray *arrWeather=[json objectForKey:@"weatherstation"]; //Loop to get the values for(NSDictionary *weather in arrWeather){ NSLog(@"Temp for Location %@ is %@",[weather objectForKey:@"location"],[weather objectForKey:@"celsius"]); } } } 

Edit

You PHP should look like below you can use this version directly as I have removed leading space from the code

<?php header('Content-Type: application/json'); if(!isset($E)) { ?> {"weatherstation":[{"location":"indoor","celsius":"<?php echo $temp;?>"}]} <?php } else { echo $M; } ?> 
Sign up to request clarification or add additional context in comments.

3 Comments

hi and thanks for the quick answer. i tried your code but it does not log the "Temp for Location.." line. the code inside the if(jsonData) got executed, but it shows nothing in the console.
Try set content-type of your PHP to application/json
i edited my first post. i have some trouble with the php file. it would be nice if you could help me out!
1

Include AFNetworking in your project, import "AFHTTPRequestOperationManager.h" and "AFURLResponseSerialization.h" in your file and use the following code:

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager] initWithBaseURL:BASE_URL]; [manager setResponseSerializer:[AFJSONResponseSerializer serializer]]; NSDictionary *parameters = @{@"username" : @"john123", @"type" : @"login"}; [manager POST:@"data.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject){ NSLog(@"response: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error){ NSLog(@"error: %@", error); }]; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.