我正面临服务响应的匿名行为。某些时候服务很好,有时它会显示错误消息"无法找到具有指定主机名的服务器" 。我正在使用AFNetworking
。同样的服务在android平台上运行得很好。有没有更好的方法在iPhone中准确地获取它们。
这是我的代码:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSDictionary *params = @{@"email" : txtEmail.text,
@"password" : txtPassword.text,
};
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
operationManager.requestSerializer = [AFJSONRequestSerializer serializer];
operationManager.responseSerializer = [AFJSONResponseSerializer serializer];
[operationManager POST:@"http://somelink/someuser.svc/login" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", operation.responseString);
NSDictionary *responseDict = responseObject;
NSLog(@"%@",[responseDict valueForKey:@"result_code"]);
if ([[responseDict valueForKey:@"result_code"] isEqualToString:@"1"]) {
NSDictionary *data = [responseObject objectForKey:@"result_data"];
NSLog(@"%@",[data objectForKey:@"email"]);
[[NSUserDefaults standardUserDefaults]setObject:[data objectForKey:@"user_id"] forKey:@"UserId"];
NSString *query = [NSString stringWithFormat:@"insert into userMaster (user_id, name_first, name_last, email, password, image, gender, rec_limit, total_followers, total_following, is_brand, category) values ('%@','%@','%@','%@','%@','%@','%@',%d,'%@','%@',%d,%d)",[data objectForKey:@"user_id"],[data objectForKey:@"name_first"],[data objectForKey:@"name_last"],[data objectForKey:@"email"],[data objectForKey:@"password"],[data objectForKey:@"image"],[data objectForKey:@"gender"],[[data objectForKey:@"rec_limit"]intValue],[data objectForKey:@"total_followers"],[data objectForKey:@"total_following"],[[data objectForKey:@"is_brand"]intValue],[[data objectForKey:@"category"]intValue]];
// NSLog(@"%@",[data objectForKey:@"intrests"]);
NSLog(@"%@",query);
int n = [service insertUpdateDeleteWithQuery:query inDatabase:@"WaveDb.sql"];
NSLog(@"%d",n);
NSArray *arr = [data objectForKey:@"intrests"];
for (int i = 0; i < arr.count; i++) {
NSLog(@"%@",[arr objectAtIndex:i]);
query = [NSString stringWithFormat:@"insert into userCategory (user_id, cat_id) values ('%@','%@')",[[NSUserDefaults standardUserDefaults]objectForKey:@"UserId"],[arr objectAtIndex:i]];
[service insertUpdateDeleteWithQuery:query inDatabase:[DataClass databaseName]];
}
dispatch_async(dispatch_get_main_queue(), ^{
[(AppDelegate *)[[UIApplication sharedApplication]delegate]hideProgress];
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"login"];
WallViewController *main = [self.storyboard instantiateViewControllerWithIdentifier:@"wallView"];
[self.navigationController pushViewController:main animated:YES];
});
}
else {
[(AppDelegate *)[[UIApplication sharedApplication]delegate]hideProgress];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert!" message:@"Invalid username and password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
} failure:^(AFHTTPRequestOperation operation, NSError error) {
NSLog(@"Error: %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[(AppDelegate *)[[UIApplication sharedApplication]delegate]hideProgress];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert!" message:@"Some technical error occured. Try later." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
});
}];
});
答案 0 :(得分:1)
在任何代码执行解决之前检查reachability
我的问题。