Last active
December 17, 2015 03:19
-
-
Save kgdesouz/5542543 to your computer and use it in GitHub Desktop.
A quick a dirty JSON example using Objective C (requires clean up and refactoring of the code into better methods). This code goes to one of my VPS's that has Tiny Tiny RSS running. The example shows you how to log into the system, then extract the categories necessary. Code that has been mashed together from : iOS6 Cookbook and http://www.raywe…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// JSONExample | |
// | |
// Created by Kenny on 2013-05-08. | |
// Copyright (c) 2013 tkee.org. All rights reserved. | |
// | |
#import "ViewController.h" | |
#define kTinyTinyRSSURL [NSString stringWithFormat:@"http://dev.tkee.org/tt-rss/api/"] | |
@interface ViewController () | |
@end | |
@implementation ViewController | |
- (void)fetchData:(NSMutableDictionary *)dictionary | |
{ | |
NSError *error = nil; | |
NSData *jsonData = [NSJSONSerialization | |
dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error]; | |
if ([jsonData length] > 0 && error == nil){ | |
NSLog(@"Successfully serialized the dictionary into data."); NSString *jsonString = | |
[[NSString alloc] initWithData:jsonData | |
encoding:NSUTF8StringEncoding]; | |
NSLog(@"JSON String = %@", jsonString); | |
// create the connection and send it to the server | |
NSString *requestString = kTinyTinyRSSURL; | |
NSURL *url = [NSURL URLWithString:requestString]; | |
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; | |
[req setHTTPMethod:@"POST"]; | |
NSString *dataString = jsonString; | |
NSData *requestData = [NSData dataWithBytes:[dataString UTF8String] length:[dataString length]]; | |
[req setHTTPBody:requestData]; | |
NSURLResponse *theResponse = NULL; | |
NSError *theError = NULL; | |
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&theResponse error:&theError]; | |
//deserialize the response into something usable | |
NSDictionary* json = [NSJSONSerialization | |
JSONObjectWithData:theResponseData //1 | |
options:kNilOptions | |
error:&error]; | |
NSArray* validResponse = [json objectForKey:@"content"]; //2 | |
NSLog(@"content: %@", validResponse); //3 | |
// TODO: need to refactor into something like fetchData | |
if ([_sid length] == 0) { | |
_sid = [(NSDictionary*)[json objectForKey:@"content"] objectForKey:@"session_id"]; | |
NSLog(@"sid: %@", _sid); | |
} | |
} | |
else if ([jsonData length] == 0 && error == nil){ | |
NSLog(@"No data was returned after serialization."); | |
} | |
else if (error != nil){ | |
NSLog(@"An error happened = %@", error); | |
} | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
NSString *login = @"demo";; | |
NSString *password = @"Pa$$w0rdForD3m0"; | |
NSLog(@"Username: %@", login); | |
NSLog(@"Password: %@", password); | |
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:@{ | |
@"op" : @"login", @"user" : login, @"password" : password | |
}]; | |
[self fetchData:dictionary]; | |
[self getCategories:_sid]; | |
} | |
- (void) getCategories:(NSString *)sid { | |
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:@{ | |
@"sid" : sid, @"op" : @"getCategories" | |
}]; | |
[self fetchData:dictionary]; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.h | |
// JSONExample | |
// | |
// Created by Kenny on 2013-05-08. | |
// Copyright (c) 2013 tkee.org. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface ViewController : UIViewController | |
@property (strong, nonatomic) NSString *sid; | |
@property (strong, nonatomic) NSDictionary *json; | |
- (void)getCategories:(NSString *)sid; | |
- (NSDictionary*)fetchedData:(NSMutableDictionary *)dictionaryJSON; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2013-05-08 14:27:19.334 JSONExample[7976:c07] Successfully serialized the dictionary into data. | |
2013-05-08 14:27:19.334 JSONExample[7976:c07] JSON String = { | |
"op" : "login", | |
"user" : "yourUser", | |
"password" : "yourPassword" | |
} | |
2013-05-08 14:27:19.496 JSONExample[7976:c07] content: { | |
"api_level" = 5; | |
"session_id" = kbgfb44dcctp9kouabi3ql6874; | |
} | |
2013-05-08 14:27:19.496 JSONExample[7976:c07] sid: kbgfb44dcctp9kouabi3ql6874 | |
2013-05-08 14:27:26.648 JSONExample[7976:c07] Successfully serialized the dictionary into data. | |
2013-05-08 14:27:26.648 JSONExample[7976:c07] JSON String = { | |
"sid" : "kbgfb44dcctp9kouabi3ql6874", | |
"op" : "getCategories" | |
} | |
2013-05-08 14:27:26.766 JSONExample[7976:c07] content: ( | |
{ | |
id = 23; | |
"order_id" = 0; | |
title = Technology; | |
unread = 50; | |
}, | |
{ | |
id = 24; | |
"order_id" = 0; | |
title = News; | |
unread = 15; | |
}, | |
{ | |
id = "-1"; | |
title = Special; | |
unread = 65; | |
}, | |
{ | |
id = 0; | |
title = Uncategorized; | |
unread = 0; | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment