Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data to POST JSON web service through iOS

Tags:

json

post

ios

I want to pass data to POST JSON web service using the below code.

NSString *urlString = @"http://3.10.204.99:9090/service/outage/data/getShortFormData";
NSDictionary *inputData = [[NSDictionary alloc] initWithObjectsAndKeys:
                           @"1020", @"outageId",
                           @"Alex", @"customerName",
                           nil];

NSError *error = nil;
NSData *jsonInputData = [NSJSONSerialization dataWithJSONObject:inputData options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonInputString = [[NSString alloc] initWithData:jsonInputData encoding:NSUTF8StringEncoding];
[self checkWithServer:urlString jsonString:jsonInputString];    

-(void)checkWithServer:(NSString *)urlname jsonString:(NSString *)jsonString{

    NSURL *url1 = [NSURL URLWithString:urlname];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url1];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    id jsonResponseData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];

    NSDictionary *jsonResponseDict;
    if ([jsonResponseData isKindOfClass:[NSDictionary class]]) {
        jsonResponseDict = jsonResponseData;
    } else {
        // Error-handling code
    }
    jsonResponseData = [jsonResponseDict objectForKey:@"d"];
    if (jsonResponseData == nil) {
        // Server may have returned a response containing an error
        // The "ExceptionType" value is returned from my .NET server used in sample
        id jsonExceptioTypeData = [jsonResponseDict objectForKey:@"ExceptionType"];
        if (jsonExceptioTypeData != nil) {
            NSLog(@"%s ERROR : Server returned an exception", __func__);
            NSLog(@"%s ERROR : Server error details = %@", __func__, jsonResponseDict);
        }
    }
}

When executing this code,it is not giving error, but the data is not getting updated.

Any help on this will be regretted.

Thank you all.

like image 916
user2959571 Avatar asked Nov 06 '13 08:11

user2959571


2 Answers

Try the below code

+(NSData *)postDataToUrl:(NSString*)urlString:(NSString*)jsonString
{
    NSData* responseData = nil;
    NSURL *url=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    responseData = [NSMutableData data] ;
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSString *bodydata=[NSString stringWithFormat:@"data=%@",jsonString];

    [request setHTTPMethod:@"POST"];
    NSData *req=[NSData dataWithBytes:[bodydata UTF8String] length:[bodydata length]];
    [request setHTTPBody:req];
    NSURLResponse* response;
    NSError* error = nil;
    responseData = [NSURLConnection sendSynchronousRequest:request     returningResponse:&response error:&error];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"the final output is:%@",responseString);

    return responseData;
}
like image 159
iGagan Kumar Avatar answered Oct 09 '22 01:10

iGagan Kumar


Try this:

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
like image 32
Shob-Z Avatar answered Oct 09 '22 03:10

Shob-Z