-3

I want to pass this data using api. I want to get countryname id,code,but I'm not getting how to do.

-(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];
}
Cœur
  • 32,421
  • 21
  • 173
  • 232

1 Answers1

0

Have you tried using AFNetworking? It will make your life much more easier.

Download the code from here AFNetworking And import the AFNetworking folder to your project.

You can use the following code to get country list in an 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);
}];
Shamas S
  • 7,237
  • 8
  • 44
  • 58
  • iam getting the out put as Response={"countrylist":[{"country_list":{"httpCode":200,"country_id":"25","country_name":"Australia","country_url":"Australia"}},{"country_list":{"httpCode":200,"country_id":"22","country_name":"Canada","country_url":"Canada"}},{"country_list":{"httpCode": i just edditd the question just check it plz – nayeem shaik Feb 28 '15 at 09:33
  • iam not getting how pass this plz can u just go to that api and do the parameters plz – nayeem shaik Feb 28 '15 at 09:35
  • The response that you have written in the comment, is that the one that you are getting from using AFNetworking? or you from your code in the question? And you are unable to use the response object, right? – Shamas S Feb 28 '15 at 09:51
  • code from the question iam getting, my task is to do country name in a datepickerview but using this countrys api plz help me @iosDev82 – nayeem shaik Feb 28 '15 at 09:57
  • You can go through the country list by doing [responseObject objectForKey:@"countrylist"]; Each object that you get, will itself be an NSDictionary. You can get their country name by [aCountry objectForKey:@"country_name"] – Shamas S Feb 28 '15 at 10:03
  • If you need help for creating a pickerView, either ask another question or you can get help here http://codewithchris.com/uipickerview-example/ – Shamas S Feb 28 '15 at 10:05
  • i didt get [responseObject objectForKey:@"countrylist"]; can u plz code it wher i have to replace it – nayeem shaik Feb 28 '15 at 10:10
  • [aCountry objectForKey:@"country_name"] when i put this in my pickerview it showing id nill – nayeem shaik Feb 28 '15 at 10:16