How to get response data using the REST API URL in ios

advertisements

How to get the Response Data from REST API url in iOS.

NSURL *url = [NSURL URLWithString:@"http://sssssssssssssssss"];
//NSData * JSONdata = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
        NSString *headerValue = [NSString stringWithFormat:@"%@" , testToken];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:JSONBody];
        [request addValue:headerValue forHTTPHeaderField:@"oauth_token"];
        // examine the response

Thanks in Advance


you can use the code below to get the data from REST API

{
NSString *post = [NSString stringWithFormat:@"parameter1=%@&parameter2=%@&parameter3=%@",parameter1txt]; // <--here put the request parameters you used to get the response

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://your api"]];

    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    NSString *str=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
   //NSLog(@"str : %@",str);

    dict6 = [self cleanJsonToObject:responseData];
    NSLog(@"str : %@",dict6);
}

Here dict6 is NSMutableDictionary and last NSLOG will print your response in Console window

- (id)cleanJsonToObject:(id)data
{
    NSError* error;
    if (data == (id)[NSNull null])
    {
        return [[NSObject alloc] init];
    }
    id jsonObject;
    if ([data isKindOfClass:[NSData class]])
    {
        jsonObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    } else
    {
        jsonObject = data;
    }
    if ([jsonObject isKindOfClass:[NSArray class]])
    {
        NSMutableArray *array = [jsonObject mutableCopy];
        for (int i = (int)array.count-1; i >= 0; i--)
        {
            id a = array[i];
            if (a == (id)[NSNull null])
            {
                [array removeObjectAtIndex:i];
            } else
            {
                array[i] = [self cleanJsonToObject:a];
            }
        }
        return array;
    } else if ([jsonObject isKindOfClass:[NSDictionary class]])
    {
        NSMutableDictionary *dictionary = [jsonObject mutableCopy];
        for(NSString *key in [dictionary allKeys])
        {
            id d = dictionary[key];
            if (d == (id)[NSNull null])
            {
                dictionary[key] = @"";
            } else
            {
                dictionary[key] = [self cleanJsonToObject:d];
            }
        }
        return dictionary;
    } else
    {
        return jsonObject;
    }
}