Ios RequestResponse nil I';我越来越
我想用api传递这些数据。我想得到countryname id和代码,但我不知道怎么做Ios RequestResponse nil I';我越来越,ios,json,iphone,nsurlrequest,Ios,Json,Iphone,Nsurlrequest,我想用api传递这些数据。我想得到countryname id和代码,但我不知道怎么做 -(void) getCountries { NSString *get=[[NSString alloc]initWithFormat:@"city id=%@,&lang=%@",[self.countryCode text],[self.countryList textInputMode]]; NSLog(@"postDat :%@",get); NSURL *url
-(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];
}
-(无效)获取国家/地区
{
NSString*get=[[NSString alloc]initWithFormat:@“城市id=%@,&lang=%@”,[self.countryCode文本],[self.countryList文本输入模式];
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”,(无符号长)[postData长度]];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]init];
[请求设置url:url];
[请求设置HttpMethod:@“POST”];
[请求设置值:HttpHeaderField的postLength:@“contentLength”];
//[RequestSetValue:@“application/json;charset=UTF-8”用于HttpHeaderField:@“Accept”];
[请求setHTTPBody:postData];
NSError*error=[[NSError alloc]init];
NSHTTPURLResponse*响应=nil;
NSData*urldata=[NSURLConnection sendSynchronousRequest:request returningResponse:&响应错误:&错误];
NSLog(@“响应代码%d”,(int)[响应状态代码]);
如果([response statusCode]您尝试过使用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);
}];
我的回答是:{“countrylist”:[{“country_list”:{“country_list”:{“httpCode”:200,“country_id”:“25”,“country_name”:“Australia”,“country_url”:“Australia”},{“country_list”:{“country_id”:“22”,“country_name”:“Canada”,“country_url”:“Canada”},{“country_list”:{“httpCode”:我只是问了一个问题,只是检查一下,我不知道如何通过这个plz,你可以去那个api,做参数,你在评论中写的回答,是你通过使用AFNetworking得到的?还是你通过问题中的代码得到的?你不能使用响应对象,对吗?来自t的代码他问我,我的任务是在datepickerview中输入国家名称,但使用这个国家的api,请帮助我@IOSDev82你可以通过[responseObject Forkey:@“countrylist”]查看国家列表;你得到的每个对象本身都是一本NSDictionary。你可以通过[Aconry objectForKey:@“country\u name”获得他们的国家名称