Objective c 目标C JSON对象为null,但responseData不为空

Objective c 目标C JSON对象为null,但responseData不为空,objective-c,json,http,request,Objective C,Json,Http,Request,我正在向web服务发送一个HTTP请求,应该会得到一个JSON作为响应。在Objective C中,responseData不是空的,但它作为JSON的序列化是空的。这是我的代码: - (IBAction)getProfileInfo:(id)sender { NSString *code = @"oanure!1"; //Start request NSURL *url = [NSURL URLWithString:@"http://192.168.1.

我正在向web服务发送一个HTTP请求,应该会得到一个JSON作为响应。在Objective C中,responseData不是空的,但它作为JSON的序列化是空的。这是我的代码:

   - (IBAction)getProfileInfo:(id)sender
   {

    NSString *code = @"oanure!1";

    //Start request
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.8:5000/login?json"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:code forKey:@"logincode"];
    [request setDelegate:self];
    [request startAsynchronous];

    }

    - (void)requestFinished: (ASIHTTPRequest *) request{
       if (request.responseStatusCode == 400){
             self.profileInfo.text = @"Invalid code";
       } else if (request.responseStatusCode == 403) {
             self.profileInfo.text = @"Code alredy used";
       } else if (request.responseStatusCode == 200) {


            NSData *responseData = [request responseData];
            NSLog(@"%@", responseData);
            NSError *error;

            //I tried with NSDictionary as well
            NSArray *json = (NSArray *)
                  [NSJSONSerialization JSONObjectWithData:responseData
                                                         options:kNilOptions
                                                           error:&error];
            NSLog(@"%@", json);

        }
     else {
            self.profileInfo.text = @"Unexpected error";
     }
     }
控制台打印出:

    2013-07-02 16:30:16.047 AppName[1396:c07] <3c68746d 6c3e0a0a 3c686561 643e0a3c      
    7469746c 653e5072 6f66696c 65206f66 206f616e 7572653c 2f746974 6c653e0a 0a3c7363 
    72697074 20747970 653d2274 6578742f 6a617661 53637269 70742220 7372633d 22737461 
    7469632f 6a717565 7279312e 392e6a73 223e3c2f 73637269 70743e0a 0a0a3c2f 68656164  
    3e0a0a3c 626f6479 3e0a3c68 313e4865 6c6c6f2c 206f616e 75726521 3c2f6831 3e0a3c74 
    61626c65 3e0a2020 3c74723e 3c746820 636f6c73 70616e3d 323e4661 6365626f 6f6b3c2f 
    74683e3c 2f74723e 0a20203c 74723e0a 20202020 3c74643e 75736572 6e616d65 3c2f7464 
    3e3c7464 3e6f616e 7572653c 2f74643e 0a20203c 2f74723e 0a3c2f74 61626c65 3e0a3c2f 
    626f6479 3e0a0a3c 2f68746d 6c3e0a>
    2013-07-02 16:30:16.048 AppName[1396:c07] (null)
在这种情况下,控制台打印出: 200行 {“loggedin”:0,“错误”:“未登录”}

我想在目标C中得到同样的结果


请帮忙

可能会出现类似的问题,因为您使用的端口是
URL
。在
Python
的情况下,您可以在指定的端口打开连接。可能您需要找到一个解决方案来正确使用
ASIFormDataRequest中的端口,或者尝试使用另一个库。

我尝试使用十六进制到字符串在线转换器转换响应数据,它是HTML格式的(不是JSON格式)。请检查服务器端是否正确发送数据。以下是转换后的响应数据:

<html>

<head>
<?title>Profile of oanure</title>

<sc?ript type="text/javaScript" src="sta?tic/jquery1.9.js"></script>


</head?>

<body>
<h1>Hello, oanure!</h1>
<t?able>
  <tr><th colspan=2>Facebook</?th></tr>
  <tr>
    <td>username</td?><td>oanure</td>
  </tr>
</table>
</?body>

</html>

你好,奥努尔!
脸谱网
usernameoanure
十六进制转换器:


其次,您可能还应该打印errMessage来检查详细的错误。一旦JSON无法转换格式,它将始终向JSON对象返回nil(null)。

您的
responseData
包含HTML,而不是JSON。打印一个由数据组成的字符串。您可能会立即看到问题。
[NSURL URLWithString:@”http://192.168.1.8:5000/login?json“]
明确指定端口5000…服务器重定向到另一个页面(/profile),因此返回HTML。有趣的是,在Python中,它在/login页面停止,而不重定向到/profile。我猜图书馆的工作方式不同。我需要处理重定向的情况,或者就这样离开它。谢谢你的意见。昨天我意识到@martin-r确实回答了我面前的HTML问题。
<html>

<head>
<?title>Profile of oanure</title>

<sc?ript type="text/javaScript" src="sta?tic/jquery1.9.js"></script>


</head?>

<body>
<h1>Hello, oanure!</h1>
<t?able>
  <tr><th colspan=2>Facebook</?th></tr>
  <tr>
    <td>username</td?><td>oanure</td>
  </tr>
</table>
</?body>

</html>