2013-07-02 27 views
0

我正在向Web服務發送HTTP請求,我應該得到一個JSON作爲響應。在Objective C中,responseData不是空的,但它作爲JSON的序列化爲null。這是我的代碼:Objective C JSON對象null但responseData不爲空

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

我想我沒有做的系列化權利......

正是如此,證明了網絡服務的工作原理,我用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() 

在這種情況下,控制檯打印出來: 200 OK { 「的loggedIn」:0, 「錯誤」: 「不登錄」}

我想獲得同樣的事情在目標C.

請幫幫忙!

+1

您的'responseData'包含HTML,而不是JSON。 –

+0

打印出由數據組成的字符串。您可能會立即看到問題。 –

回答

1

我試圖轉換響應數據使用十六進制字符串在線轉換器,它是在HTML(不是JSON格式)。請檢查服務器端是否正確發送數據。這裏是轉換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> 

十六進制轉換器:

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

其次,也許你應該打印出errMessage以及檢查詳細的錯誤。一旦JSON無法轉換格式,它將始終返回nil(null)到json對象。

+0

服務器重定向到另一個頁面(/ profile),因此返回HTML。有趣的是,在Python情況下,它停在/ login頁面而不重定向到/ profile。我猜這些圖書館工作不同。我需要處理重定向的情況,或者就這樣離開。感謝您的意見。 –

+0

昨天我意識到@ martin-r在我面前的HTML問題上做過回覆。 – morph85

0

可能會出現類似問題,因爲您正在使用端口URL。在Python的情況下,您打開指定端口的連接。也許你需要找到一個解決方案,以便與ASIFormDataRequest中的端口正常工作,或嘗試使用另一個庫。

+0

'[NSURL URLWithString:@「http://192.168.1.8:5000/login?json」]'明確指定端口5000 ... –