我发现在处理Web服务调用时很难从一个页面导航到另一个页面。这是我的问题,
我能够成功调用Web服务并获取xml,我也能够解析xml并获取所需的标签。我的问题是我在按钮操作中执行所有这些过程。我想在网络服务&解析完成。但是,由于我的页面在服务电话和电话之前导航,我无法做到这一点。解析完成。
以下是我正在做的代码
- (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 Parser页面
-(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"];
}
}
我希望你理解我的疑问。感谢
答案 0 :(得分: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];
}
}
希望它有所帮助......快乐的编码:)
答案 1 :(得分: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];
}
}