Reputation: 2395
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>
Upvotes: 1
Views: 379
Reputation: 12719
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;
}
?>
Upvotes: 1
Reputation: 3571
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);
}];
Upvotes: 1