我有一个json字符串和另一个。我必须一起追加,发送到服务器并获取用户ID。我被困在中间。附加字符串未转换为NSURL。
这是我的代码。
- (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://forthepeople.mcms.net.in/forthepeople_service/registration?reg="];
[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);
}];
}
问题: - registerURL显示空值...
答案 0 :(得分:-1)
我建议使用NSURLComponents
构建您的网址,它会在您提供的NSURLQueryItem
中正确处理查询参数编码。
答案 1 :(得分:-1)
我认为您需要在转换为NSURl
之前对NSString进行编码。你可以做到这一点。
NSMutableString* registerwithserverstring = [[NSMutableString alloc]initWithString:@"http://forthepeople.mcms.net.in/forthepeople_service/registration/reg=?{\"reg_name\":\"ugh\",\"place\":\"Malappuram\",\"ph_nbr\":\"7204412649\",\"is_municipality\":\"1\"}"];
NSString * properlyEncodedString = [registerwithserverstring stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL *registerURL = [NSURL URLWithString:properlyEncodedString];
NSLog(@"register URL%@", registerURL);
输出:
Printing description of registerURL:
http://forthepeople.mcms.net.in/forthepeople_service/registration/reg=?%7B%22reg_name%E2%80%8C%E2%80%8B%22:%22ugh%22,%22place%22:%22Malappuram%22,%22ph_nbr%22:%227204412649%22,%22is_municipality%22:%221%22%7D