2015-10-15 33 views
1

我想連接到服務器使用肥皂請求來獲取一些數據。我設置如下:AFNetworking如何處理_NSInlineData?

NSString *soapmsg = [NSString stringWithFormat: 
        @"<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ns=\"http://www.schneider-electric.com/common/dataexchange/2011/05\">" 
         @"<soap:Header/>" 
         @"<soap:Body>" 
          @"<ns:GetValuesRequest>" 
           @"<ns:GetValuesIds>" 
            @"<ns:Id>%@</ns:Id>" 
           @"</ns:GetValuesIds>" 
          @"</ns:GetValuesRequest>" 
         @"</soap:Body>" 
        @"</soap:Envelope>", 
        KTempId]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:KUrl]]; 
[request addValue: @"application/soap+xml; charset=utf8" forHTTPHeaderField:@"Content-Type"]; 
[request addValue:[NSString stringWithFormat:@"%@", KUrl] forHTTPHeaderField:@"SOAPAction"]; 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody:[soapmsg dataUsingEncoding:NSUTF8StringEncoding]]; 

NSURLCredential *credential = [NSURLCredential credentialWithUser:kWebPassword password:kWebPassword persistence:NSURLCredentialPersistenceForSession]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
[operation.securityPolicy setAllowInvalidCertificates:YES]; 
[operation.securityPolicy setValidatesDomainName:NO]; 
[operation setCredential:credential]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) 
{ 
    NSLog(@"type: %@", [responseObject class]); 
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil]; 
    NSString *tmpString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; 
    NSLog(@"dict count: %lu\n And string: %@", (unsigned long)[responseDictionary count], tmpString); 
} 
failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) 
{ 
    NSLog(@"Failure \n%@", [error localizedDescription]); 
}]; 

[[NSOperationQueue mainQueue] addOperation:operation]; 

這工作並返回以下NSLogs:

type: _NSInlineData dict count: 0

"And string: <?xml version="1.0" encoding="UTF-8"?>" 
"<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:EWSv122="http://www.schneider-electric.com/common/dataexchange/2011/05/DataExchangeInterface/Fault" xmlns:EWSv121="http://www.schneider-electric.com/common/dataexchange/2011/05">" 
"<SOAP-ENV:Header></SOAP-ENV:Header>" 
    "<SOAP-ENV:Body>" 
     "<EWSv121:GetValuesResponse>" 
      "<EWSv121:GetValuesItems>" 
       "<EWSv121:ValueItem> 
       "<EWSv121:Id>01/HC RT/Verdieping1/Noord/201/T</EWSv121:Id>" 
        "<EWSv121:State>0</EWSv121:State>" 
        "<EWSv121:Value>20.769998550415</EWSv121:Value>" 
       "</EWSv121:ValueItem>" 
      "</EWSv121:GetValuesItems>" 
     "<EWSv121:GetValuesErrorResults>" 
     "</EWSv121:GetValuesErrorResults>" 
     "</EWSv121:GetValuesResponse>" 
    "</SOAP-ENV:Body>" 
"</SOAP-ENV:Envelope>" 

所以連接工作。但我只是得到一個NSString回來,似乎無法解析它到一個NSDictionary或任何。

我試過設置:

operation.responseSerializer = [AFJSONResponseSerializer serializer]; 

但後來我得到一個錯誤說:

失敗 請求失敗:無法接受的內容類型:應用程序/肥皂+ XML

所以我應該怎麼用responseObject做什麼?

編輯: 我添加了一個NSError到NSJsonSerializer,這就是它記錄:

錯誤解析:無法讀取數據,因爲它是不正確的格式。

+1

您是否在等待JSON或XML?由於您發佈XML,我猜你正在等待XML答案。您沒有使用'JSONObjectWithData:options:error:'的錯誤參數。如果您還可以發佈日誌中的「andString:」部分來顯示我認爲是XML的內容(嘗試引用它)。 – Larme

+0

嗨Larme, 我加入了andString部分向開封后,我真的不知道會發生什麼,因爲沒有服務我試圖連接到的文檔。我也試過使用: operation.responseSerializer = [AFXMLParserResponseSerializer serializer]; 但是,這也返回: 失敗請求失敗:不可接受的內容類型:應用程序/肥皂+ xml – Tripwire

回答

0

我傻,我確實期待XML(SOAP)數據。我很久以前解決了這個問題,但忘了回答我自己的問題,這樣可以關閉。

+0

爲了提高你的(自己)的質量答案請解釋你做了什麼來解決這個問題。包括顯示解決方案的代碼示例(如果可行)。請包含指向有助於制定解決方案的任何資源的鏈接。這是任何人在研究類似問題時都會遇到這個問題和答案的時候,最有可能找到幫助。此外,請接受您自己的答案(如果可以的話),以便問題不再出現在未答覆的問題列表中。最後,恭喜你自己解決問題! :) – toonice