简体   繁体   中英

URL Decoding of json in ios

I am having 1 webservice in php which is having browser url mentioned bel :

http:Domain/webservice/ACTEC_WebService.php?lastupdate=2012-09-01 01:00:00

Now when I try to fetch url in my iphone app, it is taking this url:

http://Domain/webservice/ACTEC_WebService.php?lastupdate=2012-09-01%2001:00:00

This is creating problem.

i have used this code for fetching data from my url.

     SBJSON *json = [SBJSON new];
        json.humanReadable = YES;
        responseData = [[NSMutableData data] retain];

        NSString *service = @"";
        NSString *str;
        str = @"LastUpdated";


        NSString *requestString = [NSString stringWithFormat:@"{\"LastUpdated\":\"%@\"}",str];

       // [[NSUserDefaults standardUserDefaults] setValue:nil forKey:@"WRONGANSWER"];

        NSLog(@"request string:%@",requestString);
        NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];


        NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
        NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
        NSString *urlLoc = [fileContents objectForKey:@"URL"];
        //urlLoc = [urlLoc stringByAppendingString:service];
        NSLog(@"URL : %@",urlLoc);

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: 
                                        [NSURL URLWithString:urlLoc]];  
        NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
        [request setHTTPMethod: @"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:requestData];

        NSError *respError = nil;
        NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];
        NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
        NSLog(@"Resp : %@",responseString);

        if (respError) 
        {
            //        NSString *msg = [NSString stringWithFormat:@"Connection failed! Error - %@ %@",
            //                         [respError localizedDescription],
            //                         [[respError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]]; 
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ACTEC" 
                                                                message:@"check your network connection" delegate:self cancelButtonTitle:@"OK" 
                                                      otherButtonTitles:nil];
            [alertView show];
            [alertView release];

        } 
        else
        {
......
}

here,i am getting null response as url is getting decoded...How can I make this solved... Help me out. Thanks in advance.

NSString *urlLoc = [[fileContents objectForKey:@"URL"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSMutableData *postData = [NSMutableData data];
    NSMutableURLRequest *urlRequest;        
    [postData appendData: [[NSString stringWithFormat: @"add your data which you want to send"] dataUsingEncoding: NSUTF8StringEncoding]];  
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    urlRequest = [[NSMutableURLRequest alloc] init];
    [urlRequest setURL:[NSURL URLWithString:urlLoc]];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPBody:postData];

    NSString *temp = [[NSString alloc] initWithData:postData encoding:NSUTF8StringEncoding];
    NSLog(@"\n\nurl =%@ \n posted data =>%@",urlLoc, temp);

check nslog. that which data u send to service.

NSData *response = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"json_string >> %@",json_string);

Maybe this will help you.

hey dear just for an another example i just post my this code..

first Create new SBJSON parser object

SBJSON *parser = [[SBJSON alloc] init];


NSURLRequest *request = [NSURLRequest requestWithURL:
    [NSURL URLWithString:@"http:Domain/webservice/ACTEC_WebService.php?lastupdate=2012-09-01%2001:00:00"]];

Perform request and get JSON back as a NSData object

NSData *response = [NSURLConnection sendSynchronousRequest:request 
    returningResponse:nil error:nil];

Get JSON as a NSString from NSData response

NSString *json_string = [[NSString alloc] 
    initWithData:response encoding:NSUTF8StringEncoding];

Parse the JSON response into an object Here we're using NSArray since we're parsing an array of JSON status objects

NSArray *statuses = [parser objectWithString:json_string error:nil]

And in statuses array you have all the data you need.

i hope this help you...

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM