Skip to content

Instantly share code, notes, and snippets.

@thuss
Created August 11, 2011 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thuss/1139006 to your computer and use it in GitHub Desktop.
Save thuss/1139006 to your computer and use it in GitHub Desktop.
+(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