|
// |
|
// 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 |