我是ios noob ..我正在开发一个使用soap web服务进行登录操作的iphone应用程序。 我读了很多类似的问题,但没有人帮助我.. 我复制粘贴了soap官方代码示例,只更改了url和请求的xml,以便从两个textview中获取用户名和密码。连接正常,我有http错误200(不是真正的错误..我知道,所以连接正常)但返回的webData对象长度为0字节,所以我有一个空的xml响应..
这是我的代码:
- (IBAction)actBtnLogin:(id)sender {
recordResults = NO;
NSString *soapMessage = [NSString stringWithFormat:
@"<SOAP-ENV:Envelope "
"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
"xmlns:ns1=\"urn:dbmanager\" "
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
"xmlns:ns2=\"http://xml.apache.org/xml-soap\" "
"xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" "
"SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> "
"<SOAP-ENV:Body> "
"<ns1:camiop> "
"<strOp xsi:type=\"xsd:string\">login</strOp> "
"<strTessera xsi:type=\"xsd:string\"/> "
"<anyParametri xsi:type=\"ns2:Map\"> "
"<item> "
"<key xsi:type=\"xsd:string\">CRM_USERNAME</key> "
"<value xsi:type=\"xsd:string\">%@</value> "
"</item> "
"<item> "
"<key xsi:type=\"xsd:string\">CRM_PASSWORD</key> "
"<value xsi:type=\"xsd:string\">%@</value> "
"</item> "
"<item> "
"<key xsi:type=\"xsd:string\">CRM_SITEAPP</key> "
"<value xsi:type=\"xsd:int\">2</value> "
"</item> "
"</anyParametri> "
"</ns1:camiop> "
"</SOAP-ENV:Body> "
"</SOAP-ENV:Envelope> ", _outTxtuser.text, _outTxtpass.text];
NSLog(@"REQUEST = %@", soapMessage);
NSURL *url = [NSURL URLWithString:@"http://srvb.mysite.com/dbmgr.class.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"srvbop" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection)
{
//webData = [[NSMutableData data] retain];
NSLog(@"theConnection OK");
}
else
{
NSLog(@"theConnection is null");
}
}
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
[_webData setLength:0];
NSHTTPURLResponse * httpResponse;
httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"HTTP error %zd", (ssize_t) httpResponse.statusCode);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
{
[_webData appendData:data];
NSLog(@"DID RECEIVE DATA");
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError*)error
{
NSLog(@"error with the connection");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received bytes %d", [_webData length]);
//NSString *theXML = [[NSString alloc] initWithBytes:[_webData mutableBytes] length:[_webData length] encoding:NSUTF8StringEncoding];
//NSLog(@"xml %@",theXML);
NSString *theXML = [[NSString alloc] initWithData: _webData encoding:NSUTF8StringEncoding];
_webData = nil;
NSLog(@"Response: %@", theXML);
[_outLabelrich setText:[NSString stringWithFormat:@"Response: %@", theXML]];
/*xmlParser = [[NSXMLParser alloc] initWithData:webData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
*/
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:@"Symbol"] || [elementName isEqualToString:@"Last"] || [elementName isEqualToString:@"Time"] )
{
if(!_soapResults)
{
_soapResults = [[NSMutableString alloc]init];
}
recordResults = YES;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(recordResults)
{
[_soapResults appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"Symbol"] || [elementName isEqualToString:@"Last"] || [elementName isEqualToString:@"Time"] )
{
recordResults = NO;
NSLog(@"%@", _soapResults);
_soapResults = nil;
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (IBAction)backgroundClick:(id)sender {
[_outTxtuser resignFirstResponder];
[_outTxtpass resignFirstResponder];
}
感谢您的帮助
答案 0 :(得分:0)
很抱歉没有直接回答您的问题,但我强烈建议不要编写自定义网络/解析代码,因为有许多经过充分开发和高度测试的库可以为您处理脏工作。查看AFNetworking,它将为您处理连接和XML解析的脏工作,同时使用块的便利性。
例如,这里的AFNetworking相当于访问和解析JSON资源(在您的情况下替代AFJSONRequestOperation用于AFJSONRequestOperation)
NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"App.net Global Stream: %@", JSON);
} failure:nil];
[operation start];