【问题标题】:Parser error Error Domain=NSXMLParserErrorDomain Code=4 "The operation couldn’t be completed."解析器错误 Error Domain=NSXMLParserErrorDomain Code=4 “操作无法完成。”
【发布时间】:2011-11-10 09:59:03
【问题描述】:

我正在调用如下网络服务:

-(void)verifyEmailAndPasswordWebservices
{    
 NSString  *MD5Password = [ self MD5];
 NSLog(@"text field password %@",txt_Password.text);
 NSLog(@"converted password %@",MD5Password);    

NSString *soapMsg =[NSString stringWithFormat:
                    @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                    "<SOAP-ENV:Envelope SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:tns=\"http://php:8393/includes/webservice\">"
                    "<SOAP-ENV:Body>"
                    "<ns1:LoginUserRequest xmlns:ns1=\"http://schemas.xmlsoap.org/soap/envelope/\">"                        
                    "<email xsi:type=\"xsd:string\">%@</email>"
                    "<pass xsi:type=\"xsd:string\">%@</pass>"                   
                    "</ns1:LoginUserRequest>"
                    "</SOAP-ENV:Body>"
                    "</SOAP-ENV:Envelope>",txt_EmailOrMobile.text,MD5Password];



//---print it to the Debugger Console for verification---
NSLog(@"soapMsg..........%@",soapMsg);

NSURL *url = [NSURL URLWithString:@"http://10.0.0.115:8393/includes/webservice/login1.php"];
req = [NSMutableURLRequest requestWithURL:url];

//---set the headers---

NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMsg length]];
[req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://10.0.0.115/includes/webservice/login1.php/LoginUser" forHTTPHeaderField:@"SoapAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

//---set the HTTP method and body---

[req setHTTPMethod:@"POST"];
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

// [activityIndicator startAnimating];

conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn)
{
    webData = [[NSMutableData data] retain];
}   
}

当我收到回复时,它给了我以下错误:

完成。接收字节数:422 显示 XML (null) 解析器错误 Error Domain=NSXMLParserErrorDomain Code=4 "操作无法完成。(NSXMLParserErrorDomain error 4.)"

我在互联网上搜索了很多。但无法找到任何东西。请对此有任何帮助。

【问题讨论】:

    标签: ios objective-c xml web-services nsxmlparser


    【解决方案1】:

    在您的编码方法上尝试此代码

    NSString *theXML = [[NSString alloc] initWithBytes: [_responseData mutableBytes] length:[_responseData length] encoding:NSUTF8StringEncoding];
    

    【讨论】:

      【解决方案2】:

      另一个可能的原因可能是没有根标签。 例子:一个这样的xml数据--->


      2011-12-22

      <days_remaining>
                  0
      </days_remaining>
      

      【讨论】:

        【解决方案3】:

        NSXMLParserErrorDomain error 4 表示给 XML 解析器一个空文档。这向我表明,输入要么为空,要么您已设法在将其提供给解析器之前对其进行了更改。

        我更好地查看了您的代码。问题不在于你没有得到回应。只是你没有等待它。您发送的请求是异步的,但您正试图立即(即同步)填充您的 webData 变量。

        【讨论】:

        • 这是否意味着我必须与网络服务人员交谈以检查他发送的响应?同样的网络服务也在 Android 应用程序中运行。
        • 我已经添加到我的答案中,如果这确实是问题,请告诉我。
        • 我已经从我的代码中删除了以下几行: if (conn) { webData = [[NSMutableData data] retain];现在我在以下函数中填充 weData 变量:-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 它不再给出 NSXMLParserErrorDomain 错误 4。但它现在给出以下错误。解析器错误 Error Domain=NSXMLParserErrorDomain Code=5 “操作无法完成。(NSXMLParserErrorDomain 错误 5。)”我正在使用 PHP Web 服务
        • 错误代码 5 是:NSXMLParserPrematureDocumentEndError = 5。我不确定这对您的代码意味着什么,但我希望您没有完整的响应。例如&lt;root&gt;&lt;tag1&gt;entry 1&lt;/tag1&gt;&lt;t请不要忘记接受我的回答,因为它是正确的。
        • 请注意,如果您看到这样的错误,您可以在 Xcode 中按 Cmd+Shift+o,输入“NSXMLParserErrorDomain”并直接跳转到类型定义以及(希望)所有错误代码定义.
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-19
        • 1970-01-01
        • 2012-12-24
        • 2011-08-27
        相关资源
        最近更新 更多