2014-05-15 109 views
0

我試圖在POST中發送JSON,但出現錯誤:JSON文本沒有以數組或對象和選項允許片段未設置。通過POST發送JSON時出錯

如果我通過郵差(用於測試請求的瀏覽器應用程序)發送這個數據 - 它工作正常(返回數據)

NSString*params = @"{ \"devID\" : \"I:73899EAB-BB4F-4AE5-A691-8505E6AF0C3A\", \"msisdn\": \"+380503424248\"}"; 

NSURL* url = [NSURL URLWithString:@"https://link.privatbank.ua/dms/gpsMobile/commonSMART/init?app=gpsMobile"]; 
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; 
request.HTTPMethod = @"POST"; 

request.HTTPBody = [params dataUsingEncoding:NSUTF8StringEncoding]; 


NSURLResponse *response; 
NSError *error; 
NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 

NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil; 
+1

你在請求的響應收到此錯誤? – Ashutosh

+1

順便說一句,如果你發送JSON請求,約定會建議你的請求指定它正在這樣做,例如, '[request setValue:@「application/json」forHTTPHeaderField:@「Content-type」];'。有時編寫Web服務代碼是爲了驗證請求的內容類型是否符合預期。 – Rob

+0

是的!這是我的代碼中的很多錯誤,但有助於你的幫助,我已經糾正它,並得到正確的要求。 –

回答

0

此代碼的工作權:

NSDictionary *params = @{@"devID" : [self getUUID], 
         @"msisdn" : [[NSUserDefaults standardUserDefaults] stringForKey:@"phone_number"]}; 

NSError *jsonError; 
NSData *body = [NSJSONSerialization dataWithJSONObject:params options:0 error:&jsonError]; 

NSURL* url = [NSURL URLWithString:@"https://link.privatbank.ua/dms/gpsMobile/commonSMART/init?app=gpsMobile"]; 

request.URL = url; 
request.HTTPMethod = @"POST"; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"]; 
request.HTTPBody = body; 

NSURLResponse *response; 
NSError *error; 
NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 

NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil; 
0

只是試圖將params串並URLRequest改成這樣:

NSString *params = @"devID=73899EAB-BB4F-4AE5-A691-8505E6AF0C3A&msisdn=+380503424248"; 
NSData *postData = [params dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
[request setValue:postLenght forHTTPHeaderField:@"Content-Lenght"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPShouldHandleCookies:YES]; 
[request setHTTPBody:postData]; 
+0

這是發送請求的另一種方式,但這顯然假定服務器不像他在他的問題中所建議的那樣期待JSON,而是「application/x-www-form-urlencoded」。這完全取決於他的服務器代碼是如何配置的。 – Rob

+0

我已更正上面的代碼 – Pancho

3

它看起來像你的JSON字符串是無效的。 之間\"msisdn\"\"+380503424248\"應該是一個:

NSString*params = @"{\"devID\" : \"I:73899EAB-BB4F-4AE5-A691-8505E6AF0C3A\", \"msisdn\" : \"+380503424248\"}"; 
+0

我已更正它,但問題仍然存在。 –

1

您可能要檢查jsonData您接收響應,並確保它的實際JSON。所以,正如Ashutosh所建議的那樣,您應該從sendSynchronousRequest檢查NSError。此外,如果JSON解析失敗,請檢查NSData對象:

NSError *requestError; 
NSURLResponse *response; 
NSData *jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError]; 

// examine the HTTP status code (if any) 

if ([response isKindOfClass:[NSHTTPURLResponse class]]) { 
    int statusCode = [(NSHTTPURLResponse *)response statusCode]; 
    if (statusCode != 200) { 
     NSLog(@"%s: sendSynchronousRequest responded with statusCode = %d; expected 200", __PRETTY_FUNCTION__, statusCode); 
    } 
} 

// examine the `requestError` (if any) 

if (!jsonData) { 
    NSLog(@"%s: sendSynchronousRequest error = %@", __PRETTY_FUNCTION__, requestError); 
} 

// now try to parse the response, reporting the JSON object if successful; reporting the `NSString` representation of the `jsonData` if not 

NSError *parseError; 
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&parseError]; 
if (results) { 
    NSLog(@"%s: JSON parse succeeded; results = %@", __PRETTY_FUNCTION__, results); 
} else { 
    NSLog(@"%s: JSON parse failed; parseError = %@"; jsonData = %@", __PRETTY_FUNCTION__, parseError, [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]); 
} 

有了類似的東西,你就可以準確地診斷是怎麼回事。由於VaaChar觀察到(+1),您原始問題的JSON請求無效(儘管您後來修復了該問題)。上面的代碼可以幫助你識別錯誤,就像你已經看到了服務器的迴應。

您可以使用NSJSONSerialization爲您構建它,以確保您擁有有效的JSON,而不是手動構建您的JSON,而不是使用這種簡單的錯誤。因此:

NSDictionary *params = @{@"devID" : @"I:73899EAB-BB4F-4AE5-A691-8505E6AF0C3A", 
         @"msisdn" : @"+380503424248"}; 

NSError *jsonError; 
NSData *body = [NSJSONSerialization dataWithJSONObject:params options:0 error:&jsonError]; 
if (!body) { 
    NSLog(@"%s: dataWithJSONObject failed: %@", __PRETTY_FUNCTION__, jsonError); 
} else { 
    request.HTTPBody = body; 
}