Skip to content

Instantly share code, notes, and snippets.

@thuss

thuss/gist:1139006

Created Aug 11, 2011
Embed
What would you like to do?
+(NSDictionary *)dictionaryFromJsonWebService:(NSString *)urlString {
if (!urlString) return [NSDictionary dictionary];
NSDictionary *dict = nil;
NSError *err;
// Make the web service request
NSURL *url = [NSURL URLWithString:urlString];
NSHTTPURLResponse* urlResponse = nil;
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:UPDATE_TIMEOUT_INTERVAL];
NSData *responseData = [NSURLConnection sendSynchronousRequest:req
returningResponse:&urlResponse
error:&err];
NSString *responseString = [[[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding] autorelease];
[self logRequest:urlString withResponseCode:[urlResponse statusCode]];
// Convert the JSON response to a dictionary
if ([urlResponse statusCode] != 200) {
// A 0 status code occurs with flaky network connections so ignore it
if ([urlResponse statusCode] != 0) [FlurryAPI logError:@"Background - Error in dictionaryFromJsonWebService"
message:[NSString stringWithFormat:@"status code %i for url %@", [urlResponse statusCode], urlString]
error:err];
} else if(responseString != nil && ![responseString isEqualToString:@""]) {
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
dict = [parser objectWithString:responseString];
}
return dict;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment