0

在处理 Web 服务调用时,我发现很难从一个页面导航到另一个页面。这是我的问题,

我能够成功调用 Web 服务并获取 xml,还能够解析 xml 并获取所需的标签。我的问题是我在按钮操作中完成所有这些过程。Web 服务和解析完成后,我想导航页面。但我不能这样做,因为我的页面在服务调用和解析完成之前正在导航。

下面是我正在做的代码

- (IBAction)CIS_Login:(id)sender
{

WebServiceAuth *service = [[WebServiceAuth alloc]init];
NSString *username,*password;

username = CIS_Username.text;
password = CIS_Password.text;
[service getUsername:username getPassword:password]; (Calling Web service)

NSString *getSessionToken = [USERDEFAULTS objectForKey:@"sessionToken"];

if (getSessionToken)
{
    [self receivedSessionTokenFromCloud];
}
else
{
    [self receivedErrorMessageFromCloud];
}
}

网络服务页面

-(void)getUsername:(NSString *)usernameAuth getPassword:(NSString *)passwordAuth
{
NSString *soapMessage = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                         "<session xmlns=\"http://schemas.XYZ.com/ABC/2011\">"
                         "<account>\n"
                         "<username>%@</username>\n"
                         "<password>%@</password>\n"
                         "</account>\n"
                         "</session>",usernameAuth,passwordAuth];

NSLog(@"%@",soapMessage);


NSURL *url = [NSURL URLWithString:@"https://XYZ.com/sessions"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

[theRequest addValue:@"Token" forHTTPHeaderField:@"Code"];
[theRequest addValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"application/xml" forHTTPHeaderField:@"Accept"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSLog(@"%@",[theRequest allHTTPHeaderFields]);

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection)
{
    Data = [NSMutableData data];
}
else
{
    NSLog(@"theConnection is NULL");
}
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [Data length]);
NSString *theXML = [[NSString alloc] initWithBytes: [Data mutableBytes] length:[CiscoData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",theXML);
UserOAuthXML *xmlOAuth = [[UserOAuthXML alloc]init]; (Xml Parsing is done here)
[xmlOAuth getDataFromUserOAuth:Data];
}

Xml 解析器页面

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"code"])
{
    recordResult = FALSE;
    NSLog(@"%@",soapResults);
    [USERDEFAULTS setValue:soapResults forKey:@"code"];
}
else if ([elementName isEqualToString:@"message"])
{
    recordResult = FALSE;
    NSLog(@"%@",soapError);
    [USERDEFAULTS removeObjectForKey:@"value"];

}
}

我希望你能理解我的询问。谢谢

4

2 回答 2

0
-(IBAction)send_report
{

NSLog(@"self.get_hardstring-------------===:%@",self.get_hardstring);
NSLog(@"self.get_softstring-------------===:%@",self.get_softstring);


NSString *sopMessage=[NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                      "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                      "<soap:Body>"
                      "<BreakDown xmlns=\"http://swag.test.com/swag/\">"
                       "<hardstring>%@</hardstring>"
                      " <softstring>%@</softstring>"
                      "</BreakDown>"
                      "</soap:Body>"
                      "</soap:Envelope>",self.get_hardstring,self.get_softstring];

NSURL *url=[NSURL URLWithString:@"http://swag.test.com/webservice.asmx"];//
NSMutableURLRequest *req=[NSMutableURLRequest requestWithURL:url];
NSString *msglength=[NSString stringWithFormat:@"%d",[sopMessage length]];
[req addValue:@"text/xml;charset=utf-8"  forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://swag.test.com/swag/BreakDown" forHTTPHeaderField:@"SOAPAction"]; //



[req addValue: msglength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[sopMessage dataUsingEncoding:NSUTF8StringEncoding]];



if (conn) 
{
    webData = [[NSMutableData data]retain];

}




NSError *error1;

error1 = [[[NSError alloc] init] autorelease];

NSData *returnData1 = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:&error1];
NSString *returnString = [[NSString alloc] initWithData:returnData1 encoding:NSUTF8StringEncoding];
NSLog(@"returnString===:%@",returnString);

NSRange match;
NSRange match1;

match = [returnString rangeOfString: @"<BreakDownResult>"];

//NSLog (@"match found at index %i", match.location);   [soapResults setString:@""];

match1 = [returnString rangeOfString: @"</BreakDownResult>"];

//NSLog (@"match found at index %i", match1.location);  [soapResults setString:@""];    

//
//NSString *str=match.location
int t1= match.location+match.length;
int t2=match1.location-t1;


NSString *string2;

string2 = [returnString substringWithRange: NSMakeRange (t1, t2)];


if ([string2 isEqualToString:@"inserted SuccessFully!"])
{
    UIAlertView *alrt1 = [[UIAlertView alloc]initWithTitle:nil message:@"Statement Successfully Send" delegate:self
                                         cancelButtonTitle:nil 
                                         otherButtonTitles:@"Ok",nil];
    [alrt1 show];
    [alrt1 release];


}
else 
{
    UIAlertView *alrt1 = [[UIAlertView alloc]initWithTitle:nil message:@"Statement Not Send" delegate:self
                                         cancelButtonTitle:nil 
                                         otherButtonTitles:@"Ok",nil];
    [alrt1 show];
    [alrt1 release];    
}

}
于 2012-10-30T06:44:06.640 回答
0

尝试使用这个

- (IBAction)CIS_Login:(id)sender
{
    [self performSelectorOnMainThread:@selector(Action:) withObject:sender waitUntilDone:YES];   
}




-(void) Action{
WebServiceAuth *service = [[WebServiceAuth alloc]init];
NSString *username,*password;

username = CIS_Username.text;
password = CIS_Password.text;
[service getUsername:username getPassword:password]; (Calling Web service)

NSString *getSessionToken = [USERDEFAULTS objectForKey:@"sessionToken"];

if (getSessionToken)
{
    [self receivedSessionTokenFromCloud];
}
else
{
    [self receivedErrorMessageFromCloud];
}
}

希望它有帮助..快乐编码:)

于 2012-05-29T10:41:19.893 回答