Objective C JSON object null but responseData not empty
Question: I am sending an HTTP request to a web service and I should get a JSON as
response. In Objective C the responseData is not empty, but the serialization
of it as JSON is null. This is my code:

    
    
       - (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";
         }
         }
    

The console prints out:

    
    
        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)
    

I suppose I am not doing the serialization right...

Just so to prove that the web service works, I wrote a small script in Python:

    
    
       import httplib, urllib
       params = urllib.urlencode({
           'logincode' : 'oanure!1',
       })
       conn = httplib.HTTPConnection("192.168.1.8:5000")
       conn.request("POST", "/login?json",
             params)
       response = conn.getresponse()
       print response.status, response.reason
       data = response.read()
       print data
       conn.close()
    

In this case the console printed out: 200 OK {"loggedin": 0, "error": "Not
logged in"}

I would like to get the same thing in Objective C.

Please help!


Answer: I have tried to convert the responseData using hex to string online converter
and it is in HTML (not JSON format). Please check in server side whether is it
sending data correctly or not. Here is the converted responseData:

    
    
    <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>
    

Hex converter:

<http://www.string-functions.com/hex-string.aspx>

Secondly, probably you should print out the errMessage as well to check the
detailed error. Once JSON couldn't convert the format, it will always return
nil (null) to json object.

