使用AFNetworking对象创建URL时出现问题

时间:2015-04-16 05:28:42

标签: ios objective-c afnetworking afnetworking-2

我正在制作自定义URL,从UITextField获取值,并从其他ViewController传递一些变量。

我正在使用AFNetworking我的代码是:

- (void)getTheGoblin:(NSString*)userName
{
NSString *completeURL = [NSString stringWithFormat:@"http://%@/read/",self.ipAddress];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];

NSDictionary *parameters = @{@"username":userName};
[manager POST:completeURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

    [self hideActivityIndicator];
    ChangeLightStateVC *nextScr = (ChangeLightStateVC *) [self.storyboard instantiateViewControllerWithIdentifier:@"ChangeLightStateVC"];

    NSString *fullApiURL = [NSString stringWithFormat:@"%@/%@",completeURL,userName];

    [nextScr setApiURL:fullApiURL];

    [self.navigationController pushViewController:nextScr animated:YES];



} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [self showAlert:[NSString stringWithFormat:@"Error: %@", error]];
    [self hideActivityIndicator];
}];

}

我收到此错误消息:

错误:

  

Domain = NSURLErrorDomain           Code = -1004无法连接到服务器

1 个答案:

答案 0 :(得分:0)

嘿Sid使用下面的代码并检查可能这可以解决您的问题

- (void)getTheGoblin:(NSString*)userName
{
    NSString *completeURL = [NSString stringWithFormat:@"http://%@/read/",self.ipAddress];
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    [parameters setValue:userName forKey:@"username"];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:completeURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSLog(@"JSON: %@", responseObject);
         [self hideActivityIndicator];
         ChangeLightStateVC *nextScr = (ChangeLightStateVC *) [self.storyboard instantiateViewControllerWithIdentifier:@"ChangeLightStateVC"];
         NSString *fullApiURL = [NSString stringWithFormat:@"%@/%@",completeURL,userName];
        [nextScr setApiURL:fullApiURL];
        [self.navigationController pushViewController:nextScr animated:YES];

     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Error: %@", error);
         [self showAlert:[NSString stringWithFormat:@"Error: %@", error]];
         [self hideActivityIndicator];
     }];
}