iOS 应用程序 url 请求无法从 php 页面获取 JSON 返回


iOS app url request fails to get JSON return from php page

我正在尝试从我的服务器访问 php 页面,但没有返回。下面的代码应该没问题,因为当我将我的站点地址 (site1111111) 更改为另一个地址 (site222222) 时,代码就可以工作了。site2 使用 JSON.aspx,我不知道代码,但返回:

 {aaaa:false}

编辑并添加:不确定是否重要,当我说site1111111时,它实际上是site222222/subfolder/。结束编辑。

我的PHP代码是简单的测试,只需填充相同的json字符串:

<html><body>
$arr = array ('aaaa'=>false);
echo json_encode($arr); 
</body></html>

iOS 应用代码在这里:

    NSString *urlString = @"http://site111111111111111111/welcome.php";
    //urlString = @"http://site222222222222/JSON.aspx?function=ffff&item=aaaa";
    NSURL *url = [NSURL URLWithString:urlString];
    NSError *error;
    NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
    NSLog(@"%@", data);
    //<7b226973 64696769 74616c22 3a66616c 73657d> //print data from site2222222
    //<3c68746d 6c3e0d0a 3c626f64 793e0d0a 0d0a3c21 2d2d5765 6c636f6d 65203c62 723e0d0a 596f7572 20656d61 696c2061 64647265 73732069 733a200d 0a2d2d3e 0d0a0d0a 7b226973 64696769 74616c22 3a66616c 73657d0d 0a3c2f62 6f64793e 0d0a3c2f 68746d6c 3e0d0a> //data from site11111 
         if (error)
         {
             NSLog(@"error==%@==",[error localizedDescription]);
         }
         else
         {
             NSLog(@"no error for request"); //both site1111 and site2222 print this out
             NSError *errorInJsonParsing;
             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&errorInJsonParsing];
             if(errorInJsonParsing)
             {
                 NSLog(@"error in json==%@==",[error localizedDescription]);
             }
             else
             {
                 if ([[json valueForKey:@"aaaa"] boolValue] ) {
                     NSLog(@"YES");
                 } else {
                     NSLog(@"NO");
                 }
             }
         }

运行代码时,我得到了空 json,从 if 条件 if(errorInJsonParsing) 打印。

我不知道我是否错过了连接我的 php 和 ios 请求的任何内容。请帮忙。啪!

site1111 不返回 json,而是返回 html。 如果你解码这些十六进制字节,你会看到这个:

<html>
<body>
<!--Welcome <br>
Your email address is: 
-->
{"isdigital":false}
</body>
</html>

修复网站,使其返回 json,您应该很高兴。