vendredi 8 mai 2015

JSON conversion issue in iOS

I have a json string and another one. i have to append together, send to server and get user id. I am stuck in d middle. Appended string is not getting converted to NSURL.


Here is my code.

- (void)convertingstringtojsondata
{
    NSArray *components = [[NSArray alloc]initWithObjects:ismunicipality,mobilenumberstring,placestring,namestring,  nil];
    NSArray *keys = [[NSArray alloc]initWithObjects:@"is_municipality",@"ph_nbr",@"place",@"reg_name", nil];
    NSLog(@"%@",components);

    NSDictionary *dict = [[NSDictionary alloc]initWithObjects:components forKeys:keys];

    NSLog(@"dict %@", dict);

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:0
                                                         error:nil];
    NSLog(@"JSON DATA %@", jsonData);

    jsonData= jsonData;

  JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];

    NSLog(@"JSON STRING%@",JSONString);
}
-(void)sendjsonforregistration
{
 [self convertingstringtojsondata];

    registerwithserverstring = [[NSMutableString alloc]initWithString:@"http://ift.tt/1PtRVKM"];
    [registerwithserverstring appendString:JSONString];

    NSLog(@" appended string %@", registerwithserverstring);

    NSURL *registerURL = [NSURL URLWithString:registerwithserverstring];

    NSLog(@"register URL%@", registerURL);

NSMutableURLRequest * request = [[NSMutableURLRequest alloc]initWithURL:registerURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:0];
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * response, NSData * data, NSError * error) {
    NSData * jsonData1 = [NSData dataWithContentsOfURL:registerURL];

    NSLog(@"%@", jsonData1);

    dataDictionary1 = [NSJSONSerialization JSONObjectWithData:jsonData1 options:0 error:&error];

   NSLog(@"DATA DICTIONARY %@", dataDictionary1);

}];
}


Problem:- The registerURL shows null value...

Aucun commentaire:

Enregistrer un commentaire