RequestResponse nil 我得到
RequestResponse nil I'm getting
我想使用 api 传递此数据。我想获取国家名称 ID,代码,但我不知道该怎么做。
-(void) getCountries
{
NSString *get=[[NSString alloc]initWithFormat:@"city id=%@,&lang=%@",[self.countryCode text],[self.countryList textInputMode]];
NSLog(@"postDat :%@",get);
NSURL *url=[NSURL URLWithString:@"http://demo28.know3.com/api/country-list/en.html"];
NSData*postData=[get dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"contentLength"];
//[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
NSError *error=[[NSError alloc]init];
NSHTTPURLResponse *response=nil;
NSData *urldata=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code %d",(int)[response statusCode]);
if ([response statusCode]<=200 &&[response statusCode]<=300) {
NSString *responseData=[[NSString alloc]initWithData:urldata encoding:NSASCIIStringEncoding];
NSLog(@"Response=%@",responseData);
NSError *error=nil;NSDictionary *jsondata=[NSJSONSerialization JSONObjectWithData:urldata options:NSJSONReadingMutableContainers error:&error];
success=[jsondata[@"success"]integerValue];
NSLog(@"success:%ld",(long)success);
if (success==0) {
NSLog(@"Country");
[self alertStatus:@" country " :@"country List scucess!"];
}else {
NSString *errormsg=(NSString *)jsondata[@"errormesg"];
[self alertStatus:errormsg :@" Failed"];
}
}else{
[self alertStatus:@"connection Failed" :@"sign in failed"];
}
}
-(void)alertStatus:(NSString *)message :(NSString *)title
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
[alert show];
}
您尝试过使用 AFNetworking 吗?它会让你的生活更轻松。
从这里下载代码 AFNetworking
并将 AFNetworking 文件夹导入到您的项目中。
您可以使用以下代码在 NSDictionary
中获取国家/地区列表
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
我想使用 api 传递此数据。我想获取国家名称 ID,代码,但我不知道该怎么做。
-(void) getCountries
{
NSString *get=[[NSString alloc]initWithFormat:@"city id=%@,&lang=%@",[self.countryCode text],[self.countryList textInputMode]];
NSLog(@"postDat :%@",get);
NSURL *url=[NSURL URLWithString:@"http://demo28.know3.com/api/country-list/en.html"];
NSData*postData=[get dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"contentLength"];
//[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:postData];
NSError *error=[[NSError alloc]init];
NSHTTPURLResponse *response=nil;
NSData *urldata=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code %d",(int)[response statusCode]);
if ([response statusCode]<=200 &&[response statusCode]<=300) {
NSString *responseData=[[NSString alloc]initWithData:urldata encoding:NSASCIIStringEncoding];
NSLog(@"Response=%@",responseData);
NSError *error=nil;NSDictionary *jsondata=[NSJSONSerialization JSONObjectWithData:urldata options:NSJSONReadingMutableContainers error:&error];
success=[jsondata[@"success"]integerValue];
NSLog(@"success:%ld",(long)success);
if (success==0) {
NSLog(@"Country");
[self alertStatus:@" country " :@"country List scucess!"];
}else {
NSString *errormsg=(NSString *)jsondata[@"errormesg"];
[self alertStatus:errormsg :@" Failed"];
}
}else{
[self alertStatus:@"connection Failed" :@"sign in failed"];
}
}
-(void)alertStatus:(NSString *)message :(NSString *)title
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
[alert show];
}
您尝试过使用 AFNetworking 吗?它会让你的生活更轻松。
从这里下载代码 AFNetworking 并将 AFNetworking 文件夹导入到您的项目中。
您可以使用以下代码在 NSDictionary
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];