在soap web服务中,如何获取用户数组中的值并将其存储在不同的字符串中。我必须在NSString
中存储标题,名字等。如何在目标c中做到这一点?
- (IBAction)loginbutton:(id)sender {
//calling the soap message
NSString *userid = _username.text;
NSString *password = _password.text;
soapMessage = [NSString stringWithFormat:@"my soap url",userid,password];
NSURL *url = [NSURL URLWithString:@"my soap url"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue:@"www.etownpanchayat.com" forHTTPHeaderField:@"Host"];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/LoginDetails" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
//initiate the request
NSURLConnection *connection =[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(connection)
{
webResponseData = [NSMutableData data] ;
}
else
{
NSLog(@"Connection is NULL");
}
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.webResponseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.webResponseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Some error in your Connection. Please try again.");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Received %d Bytes", [webResponseData length]);
NSString *theXML = [[NSString alloc] initWithBytes:
[webResponseData mutableBytes] length:[webResponseData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",theXML);
//now parsing the xml
NSData *myData = [theXML dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:myData];
//setting delegate of XML parser to self
xmlParser.delegate = self;
// Run the parser
@try{
BOOL parsingResult = [xmlParser parse];
NSLog(@"parsing result = %hhd",parsingResult);
}
@catch (NSException* exception)
{
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Server Error" message:[exception reason] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
return;
}
}
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentElement = elementName;
NSLog(@" %@ the current .......",currentElement);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"Parsed Element : %@", currentElement);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSArray *)userArray
{
if ([currentElement isEqualToString:@"LoginDetailsResult"]) {
NSLog(@"%@ the out value is ",userArray);
//In user array i am getting the following response [{"Title":"Mrs","FirstName":"Pavithra","LastName":"(null)","ContactNo":"8073243634","EmailId":"test123456","DoorNo":"","StreetName":"","City":"v","Pincode":"v","State":"b","Country":"v","Address":"vb","UserId":526722}]
}
}
答案 0 :(得分:1)
首先创建一个名为UserModel的新类,继承自NSObject并在UserModel.h类中编写以下代码
@interface UserModel : NSObject
@property (nonatomic, strong) NSString* title;
@property (nonatomic, strong) NSString* firstName;
@property (nonatomic, strong) NSString* lastName;
@property (nonatomic, strong) NSString* contactNo;
@property (nonatomic, strong) NSString* emailId;
@property (nonatomic, strong) NSString* doorNo;
@property (nonatomic, strong) NSString* streetName;
@property (nonatomic, strong) NSString* city;
@property (nonatomic, strong) NSString* pincode;
@property (nonatomic, strong) NSString* state;
@property (nonatomic, strong) NSString* country;
@property (nonatomic, strong) NSString* address;
@property (nonatomic, strong) NSString* userId;
- (id) initWithDictionary:(NSDictionary*)dict;
在UserModel.m类中添加此代码
- (id) initWithDictionary:(NSDictionary*)dict
{
self = [super init];
if (self)
{
self.title = dict[@"Title"];
self.firstName = dict[@"FirstName"];
self.lastName = dict[@"LastName"];
self.contactNo = dict[@"ContactNo"];
self.emailId = dict[@"EmailId"];
self.doorNo = dict[@"DoorNo"];
self.streetName = dict[@"StreetName"];
self.city = dict[@"City"];
self.pincode = dict[@"Pincode"];
self.state = dict[@"State"];
self.country = dict[@"Country"];
self.address = dict[@"Address"];
self.userId = dict[@"UserId"];
}
return self;
}
在您正在解析的类中导入UserModel.h然后在您已收到字典数组的代码中获取Global(您也可以取本地取决于您正在应用的逻辑)
#import UserModel.h
UserModel* _userObj
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSArray *)userArray
{
if ([currentElement isEqualToString:@"LoginDetailsResult"])
{
NSLog(@"%@ the out value is ",userArray);
for (NSDictionary* dict in modelsListArray)
{
//UserModel* obj = [[UserModel alloc] initWithDictionary:dict];
_userObj = [[UserModel alloc] initWithDictionary:dict]
}
}
}
现在如果您在UiTextFields用户的UIlabel上设置数据以下设置数据的代码
self.firstNameLabel.text = _userObj. firstName;
self.lastNameLabel.text = _userObj. lastName;
这是MVC设计模式。尝试始终遵循此模型。
答案 1 :(得分:0)
我可以说使用模型类来更好地处理这类数据。创建NSObject类,说“User”并将所有这些属性添加到类中。使用任何JSON解析器(如SBJSON),将响应转换为模型类,以便获得模型类对象的数组。然后,您将能够非常轻松地处理此响应(user.title,user.firstname等。)
答案 2 :(得分:-2)
首先:字典写错了 你可以试试这个:
NSArray * array = @ [@ {@“Title”:@“Mrs”,@“UserId”:@ 526722}];的NSLog(@ “%@”,数组);希望可以帮助你;