Abhi Abhi - 5 months ago 18
iOS Question

iOS - different response from single API in ARC and parsing

In my project i am passing this API: http://dev-demo.info.bh-in-15.webhostbox.net/dv/nationalblack/api/businessbysubcat with params:

prod_id=25,var_id=140
.
The problem is when i am pass this api in Rest Client it displays correct response but when i am trying to put it in my code it shows different response.

i am using the following code:

-(void)listofNotice
{
NSString *post = [NSString stringWithFormat:@"prod_id=25,var_id=140"];
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://dev-demo.info.bh-in-15.webhostbox.net/dv/nationalblack/api/businessbysubcat"]];
[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);

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

}


- (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;
}
}


it display the following response:

str : {
business = 0;
"business-list" = "Business list empty.";
response = 401;
}


but the actual response is something like this

enter image description here

please help me.. Thanks In advance

Answer

Please change this

NSString *post = [NSString stringWithFormat:@"prod_id=25,var_id=140"];

To:

NSString * post =[NSString stringWithFormat:@"prod_id=25&var_id=140"];

if possible use this:

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        configuration.HTTPAdditionalHeaders = @{@"application/x-www-form-urlencoded" :  @"Content-Type"};
        NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
        NSURL *url = [NSURL URLWithString:@"http://dev-demo.info.bh-in-15.webhostbox.net/dv/nationalblack/api/businessbysubcat"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

      NSData *requestData = [post dataUsingEncoding:NSUTF8StringEncoding];
      [request setHTTPMethod:@"POST"];
      [request setHTTPBody:requestData];

      NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)                                                                              {   
  if (data != nil){
    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
    NSInteger code = [httpResponse statusCode];
    NSLog(@"Status Code: %ld", (long)code);

  if (code == 200) {
    NSError *error;
    id responseObject =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
   }
  }                                                                              
}];

[postDataTask resume];

OR

    NSString *postString = [NSString stringWithFormat:@"prod_id=%@&var_id=%@",@"25",@"140"];
    NSURL *urlPath = [NSURL URLWithString:@"http://dev-demo.info.bh-in-15.webhostbox.net/dv/nationalblack/api/businessbysubcat"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlPath
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];

    NSData *requestData = [postString dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:requestData];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        [APP_DELEGATE removeLoader];

        if(data != nil) {
            NSDictionary *responseObject =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
            NSLog(@" %@", responseObject);
        }
        else {
        }
    }];

Hope this helps.

Comments