在我的iOS应用程序中,我需要在进入该特定屏幕时显示默认的5个用户

时间:2014-11-03 09:27:47

标签: ios parsing xml-parsing

我正在该应用程序中设计一个应用程序,我有一个名为SearchViewController的View控制器,我可以从我的应用程序中搜索其他用户。用户可从服务获得。

主要任务是当我进入此视图控制器时需要显示默认的5个用户,默认的5个用户在服务本身,我只需要从我的应用程序和该视图控制器调用该服务。我是这个iOS开发的新手,数据是服务中的XML形式,如何将这个XML数据解析到我的应用程序。

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

首先将变量声明为

    NSMutableURLRequest             *req;
    NSURLConnection                 *conn;
    NSMutableData                   *webMerchantLoginAndProfileDataIphone;
<。>在.h文件中然后使用NSURLConnection委托方法,您可以设置连接&amp;从服务中获取数据如下: -

NSString *str_url = [ NSString stringWithFormat:@"%@",xmlWebservicesUrl];
NSURL *url = [NSURL URLWithString:str_url];
req = [NSMutableURLRequest requestWithURL:url];

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

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
   [webMerchantLoginAndProfileDataIphone setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
   [webMerchantLoginAndProfileDataIphone appendData:data];
}

-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error
{
   NSLog(@"error...................%@",[error description]);

   [delegate processMerchantLoginAndProfileData:@"error" success:0];

   alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"You are not connected to the internet. Please check your internet connection and try again." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
   [alert show];

   return;
}

 -(void) connectionDidFinishLoading:(NSURLConnection *) connection
{ 
    //Check the request and returns the response.
    //NSLog(@"DONE. Received Bytes get iphone Data: %d", [webMerchantLoginAndProfileDataIphone length]);
   theXMLMerchantLoginAndProfileDataIphone = [[NSString alloc]
                              initWithBytes: [webMerchantLoginAndProfileDataIphone mutableBytes]
                              length: [webMerchantLoginAndProfileDataIphone length]
                              encoding:NSUTF8StringEncoding];

   //---shows the XML---

   (@"shows the XML %@",theXMLMerchantLoginAndProfileDataIphone);

   if(xmlParserMerchantLoginAndProfileDataIphone)
   {
      xmlParserMerchantLoginAndProfileDataIphone = [[NSXMLParser alloc] initWithData: webMerchantLoginAndProfileDataIphone];
      [xmlParserMerchantLoginAndProfileDataIphone setDelegate: self];
      [xmlParserMerchantLoginAndProfileDataIphone setShouldResolveExternalEntities:YES];
      [xmlParserMerchantLoginAndProfileDataIphone parse];    
   }
}

然后有xml arsing的委托方法,如下所示

//---when the start of an element is found---
-(void)  parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI  qualifiedName:(NSString *) qName  attributes:(NSDictionary *) attributeDict
{
}

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
}

-(void)parserDidEndDocument:(NSXMLParser *)parser
{
}

你可以根据你得到的答案解析xml