【问题标题】:url could not give any response from json parsingurl 无法从 json 解析中给出任何响应
【发布时间】:2016-01-25 06:32:34
【问题描述】:

我正在尝试在服务器上发布数据,但它无法从服务器端给出任何响应,请检查我的代码。

不给这个 url 任何值 NSString *urlLoc = @"http://www.vallesoft.com/vlcc_api/bookappointment.php";

NSString *requestString = [NSString stringWithFormat:@"name=%@&mobileno=%@&emailid=%@&dob=%@&password=%@&gender=%@&ref=%@&fbflag=0",goSignUpName,goSignUpMobile,goSignUpEmailId,goSignUpDOB,goSignUpPassword,goSignUpGender,goSignUpReferenceId] ;
NSData *postData = [requestString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:urlLoc]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
 {

     if(data.length>0 && connectionError==nil)
     {


         responseData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
         NSLog(@"LOGIN Response ==> %@", responseData);

         if ([responseData intValue] == 1)
         {

             UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Thanks" message:@"Registered" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
             [alert show];

             for(id controller in [self.navigationController viewControllers])
             {
                 if([controller isKindOfClass : [ViewController class]]){ [self.navigationController popToViewController: controller animated: YES];
                     break;
                 }
             }



         }
         else if ([responseData intValue] == 2)
         {
             UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Already Reegistered" message:@"Please choose other Details" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
             [alert show];
         }
         else if (responseData==NULL)
         {
             UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Not Book Try Again!!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
             [alert show];
         }


     }


 }];



}

【问题讨论】:

  • 问题不在您的代码中,请检查 URL 是否有效
  • 你能发布connectionErrorlocalizedDescription吗?
  • vallesoft.com/vlcc_api/… url 在我点击服务器时给出响应

标签: ios objective-c json


【解决方案1】:

从在 iOS 上发送 HTTP 调用的角度来看,您发布的代码是可以的。 但它有一个可能的问题:您将“requestString”参数创建为 GET,但您将其作为 POST 正文发送。您确定要这样做吗?

除此之外似乎还不错。

尝试将“urlLoc”设置为虚拟值;例如“http://www.google.com”,您会看到异步回调中出现错误。

【讨论】:

    【解决方案2】:

    我认为,问题出在您的服务器代码中。在从任何来源发送请求时,它没有给出任何响应。您可以使用 Postman(Google Chrome 插件)验证

    更新

    好的,服务器正在等待 GET 请求,但您正在发出 POST 请求

    请使用以下方法。

    -(void)sendToServerHandler:(NSString*)serviceURL showLoader:(BOOL)showLoader completionBlock:(void (^)(BOOL success, NSDictionary *responseData))completionBlock
    {
        NSURL *url = [[NSURL alloc] initWithString:serviceURL];
    
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:600.0];
    
        [NSURLConnection sendAsynchronousRequest:request
                                           queue:[[NSOperationQueue alloc] init]
                               completionHandler:^(NSURLResponse *response,
                                                   NSData *data,
                                                   NSError *connectionError)
                    {
                               dispatch_async(dispatch_get_main_queue(),
                            ^{
    
                                if(showLoader)
                                {
                                    [appDelegate hideLoaderInstantaneously];
                                }
                                   // handle response
                                   if (connectionError)
                                   {
                                       completionBlock(false, nil);
                                       return;
                                   }
                                   else
                                   {
                                        NSDictionary *parsedDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                                       if(parsedDictionary==nil)
                                       {
                                           completionBlock(false, nil);
                                           return;
                                       }
    
                                       completionBlock(true, parsedDictionary);
                                   }
                               });
                           }];
    

    }

    它应该给你适当的回应

    调用它使用

    NSString *URL = @"http://www.vallesoft.com/vlcc_api/registration.php?name=qq&mobileno=123456789&emailid=hk@gmail&dob=2015-10-26&password=234&gender=MALE&ref=02&fbflag=0";
    
    [self sendToServerHandler:URL showLoader:showLoader completionBlock:^(BOOL success, NSDictionary *responseData)
    {
        NSLog(@"response data  --> %@", reponse data);// RESPONSE FROM SERVER
    }];
    

    更新

    因为您使用的是 iOS 9 应用程序传输安全默认阻止的 http 连接。我希望你会为你的 URL 添加一个例外。如果没有,下面是解释的方式。

    如果您尝试在启用 ATS 的情况下发出 HTTP 请求(使用 NSURLSession 或 AFNetworking 等库),您将看到如下错误: 错误域=NSURLErrorDomain 代码=-1004 “无法连接到服务器。” UserInfo=0x12ed5bad0 {NSUnderlyingError=0x12ee495b0 “操作无法完成。(kCFErrorDomainCFNetwork 错误 -1004。)” 或这个: NSURLSession/NSURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9802) 或这个: App Transport Security 已阻止明文 HTTP (http://) 资源加载,因为它不安全。可以通过应用的 Info.plist 文件配置临时例外。

    以下是完全禁用 ATS 的方法。打开 Info.plist,并添加以下行:

    <key>NSAppTransportSecurity</key>
    <dict>
      <key>NSAllowsArbitraryLoads</key>
         <true/>
     </dict>
    

    点击链接了解更多详情: Enable http connection in iOS 9

    【讨论】:

    • vallesoft.com/vlcc_api/… url 在我点击服务器时给出响应
    • :-没有一个答案对我有帮助..有人可以帮忙吗?
    • 你能告诉问题或任何错误,你是从上面的代码中收到的。我已经更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 2023-03-18
    • 2022-01-12
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多