2016-09-15 100 views
0

你好我所知道的这种问题以前问过,但我没有得到任何解决方案从他们 在我的项目我工作在登录视图时,我把代码登录按钮,我收到一个错误IOS:kCFErrorDomainCFNetwork错误-1002。错误域= NSURLErrorDomain代码= -1002“不受支持的URL”

错误:错误域= NSURLErrorDomain代码= -1002 “不受支持的URL” 的UserInfo = {0x7fb37b62c9a0 = NSLocalizedDescription不支持URL, NSUnderlyingError = 0x7fb37b715a20“操作无法完成。 (kCFErrorDomainCFNetwork error -1002。)“}

但我使用的登入,我在以前的项目中使用相同的代码,它工作正常有

这里是我的代码:

-(IBAction)login:(id)sender 
{ 
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; 
    NSURL *url = [NSURL URLWithString:@"http://eyeforweb.info.bh-in-15.webhostbox.net/myconnect/api.php?token={LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUc4d0RRWUpLb1pJaHZjTkFRRUJCUUFEWGdBd1d3SlVBeWo0WE9JNjI4cnJRTG9YeEpXNG1zUWI1YmtvYk1hVQpzMnY1WjFKeXJDRWdpOVhoRzZlZk4rYTR0eGlMTVdaRXdNaS9uS1cyL1NCS2pCUnBYUzVGYUdiV0VLRG1WOXkvCkYrWHhsUXVoeER0MEV3YkRBZ01CQUFFPQotLS0tLUVORCBQVUJMSUMgS0VZLS0tLS0K}&method=user.getLogin"]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy 
                 timeoutInterval:60.0]; 

    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request addValue:@"*/*" forHTTPHeaderField:@"Accept"]; 
    [request setHTTPMethod:@"POST"]; 
    NSString *mapData = [NSString stringWithFormat:@"[email protected]&password=123456"];//,username.text, password.text]; 
    NSData *postData = [mapData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    [request setHTTPBody:postData]; 
    NSLog(@"map data is = %@",mapData); 
    NSURLSessionDataTask * postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse* response, NSError * error) { 

     if(error == nil) 
     { 
      NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
      NSString *text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; 
      NSLog(@"Data = %@",text); 
      NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
      NSLog(@"jsondic= %@",jsonDic); 

      NSDictionary *userDataDic = [jsonDic objectForKey:@"data"]; 
      NSLog(@"Dict is %@",userDataDic); 

请帮我解决这个问题,我已经看到了类似的类型的问题,但没有解决这个问题 任何帮助表示赞赏

+0

阿比您的网址是不正确的 – user3182143

+0

但它给提前休息客户 – Abhi

+0

正确的反应,当我运行代码,它给了我零 – user3182143

回答

0

我试过你的代码。除了你的网址,其他代码行是正确的。如果你通过你的corrct网址,它完美的作品。

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://eyeforweb.info.bh-in-15.webhostbox.net/myconnect/api.php"]]; //pass your url here 
[request setHTTPMethod:@"POST"]; 
//Passing The String to server 
NSString *strUserId = @"[email protected]"; 
NSString *strPassword = @"admin123"; 


NSString *userUpdate =[NSString stringWithFormat:@"login=%@&password=%@",strUserId,strPassword, nil]; 

//Check The Value what we passed 
NSLog(@"the data Details is %@", userUpdate); 


//Convert the String to Data 
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding]; 
NSLog(@"The postData is - %@",data1); 


//Apply the data to the body 
[request setHTTPBody:data1]; 

NSURLSession *session = [NSURLSession sharedSession]; 
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 
if(httpResponse.statusCode == 200) 
{ 
NSError *parseError = nil; 
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError]; 
NSLog(@"The response is - %@",responseDictionary); 
NSInteger success = [[responseDictionary objectForKey:@"success"] integerValue]; 
if(success == 1) 
{ 
NSLog(@"Login SUCCESS"); 
} 
else 
{ 
NSLog(@"Login FAILURE"); 
} 
} 
else 
{ 
NSLog(@"Error"); 
} 
}]; 
[dataTask resume]; 
相关问题