Created
August 11, 2011 06:11
-
-
Save thuss/1139006 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
+(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