【问题标题】:RequestResponse nil I'm gettingRequestResponse nil 我得到
【发布时间】:2015-02-28 07:16:37
【问题描述】:

我想使用 api 传递这些数据。我想获取countryname id,code,但我不知道该怎么做。

-(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];
}

【问题讨论】:

    标签: ios json iphone nsurlrequest


    【解决方案1】:

    您是否尝试过使用 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);
    }];
    

    【讨论】:

    • 我得到的输出为 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": 我刚刚编辑了这个问题,请检查一下
    • 我不知道如何通过这个plz你可以去那个api并做参数plz
    • 您在评论中写的回复是您使用 AFNetworking 得到的回复吗?或者你从你的代码中的问题?而且您无法使用响应对象,对吗?
    • 我得到的问题中的代码,我的任务是在 datepickerview 中做国家名称,但使用这个国家的 api 请帮助我@iosDev82
    • 你可以通过 [responseObject objectForKey:@"countrylist"];你得到的每个对象,本身就是一个 NSDictionary。您可以通过 [aCountry objectForKey:@"country_name"] 获取他们的国家/地区名称
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多