IOS How to get data from server with json
/* Get data from Server */
+ (NSDictionary *)getDataFromJson:(NSString *)urlString {
// Execute search by performing an HTTP GET to the REST web service which returns JSON
NSString *jsonString = [Global jsonFromURLString:urlString];
//NSLog(@"jsonString: %@", jsonString);
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
// Parse JSON results with TouchJSON. It converts it into a dictionary.
CJSONDeserializer *jsonDeserializer = [CJSONDeserializer deserializer];
NSError *error = nil;
NSDictionary *resultsDictionary = [jsonDeserializer deserializeAsDictionary:jsonData error:&error];
if(error) return nil;
return resultsDictionary;
}
// Get json string from server
+ (NSString *)jsonFromURLString:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setTimeoutInterval:5.0];
[request setHTTPMethod:@"GET"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[request release];
if(error) {
NSLog(@"Error:%@",error);
} else {
NSLog(@"Success");
}
NSString *resultString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
return [resultString autorelease];
}
+ (NSDictionary *)getDataFromJson:(NSString *)urlString {
// Execute search by performing an HTTP GET to the REST web service which returns JSON
NSString *jsonString = [Global jsonFromURLString:urlString];
//NSLog(@"jsonString: %@", jsonString);
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
// Parse JSON results with TouchJSON. It converts it into a dictionary.
CJSONDeserializer *jsonDeserializer = [CJSONDeserializer deserializer];
NSError *error = nil;
NSDictionary *resultsDictionary = [jsonDeserializer deserializeAsDictionary:jsonData error:&error];
if(error) return nil;
return resultsDictionary;
}
// Get json string from server
+ (NSString *)jsonFromURLString:(NSString *)urlString {
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setTimeoutInterval:5.0];
[request setHTTPMethod:@"GET"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[request release];
if(error) {
NSLog(@"Error:%@",error);
} else {
NSLog(@"Success");
}
NSString *resultString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
return [resultString autorelease];
}
Comments
Post a Comment