Web服务在Apple Iphone连接中

时间:2012-03-13 09:16:30

标签: iphone

我有网络服务。需要我的苹果代码连接并将3个参数传递给Web服务。对Objective C来说是新手。任何人都可以提供示例代码或指导我完成此操作吗?在StackOverflow中搜索过链接。但无法理解它们。

提前致谢。

3 个答案:

答案 0 :(得分:1)

   NSString *urlString= [NSString stringWithFormat:@"http://demo.xyz.com/proj/webservices/display_records/1/%@%@%@",myid,name,password];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
conn= [[NSURLConnection alloc]initWithRequest:request delegate:self];

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

使用默认连接方法

答案 1 :(得分:0)

获得ws结果的简单方法是:

NSString *urlString= [NSString stringWithFormat:@"http://demo.xyz.com/proj/webservices/display_records/1/%@%@%@",myid,name,password];
NSString *wsRes = [NSString stringWithContentsOfURL:urlStribng encoding: NSUTF8StringEncoding error:nil];

如果要使用它,请考虑使用错误处理来扩展此示例。

答案 2 :(得分:0)

您使用soap message okey然后akshar首先创建一个NSObject类,然后使用以下代码中给出的3参数在.h文件中定义一个方法: -

<。>文件中的

@interface GetPartParser : NSObject <NSXMLParserDelegate>
{
    NSXMLParser *xmlParser;
    NSMutableArray *getPartArr;
    NSMutableDictionary *item;
    NSString *currentElement;
    NSMutableString *part_urlStr, *partNameStr;
}
@property (nonatomic,retain) NSMutableArray *getPartArr;
-(void) fetchPartXMLData:(NSInteger)empid:(NSInteger)serid:(NSInteger)seaid:(NSInteger)episodeid;

并在.m文件中: -

-(void) fetchPartXMLData:(NSInteger)empid:(NSInteger)serid:(NSInteger)seaid :(NSInteger)episodeid
{
    NSURL *myURL = [NSURL URLWithString:@"http://webservices.asmx"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: myURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    NSString *soapMessage = [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>"
                             "<GetPart xmlns=\"http://tempuri.org/\">"
                             "<empid>%d</empid>"
                             "<serid>%d</serid>"
                             "<seaid>%d</seaid>"
                             "<episodeId>%d</episodeId>"
                             "</GetPart>"
                             "</soap:Body>"
                             "</soap:Envelope>",empid,serid,seaid,episodeid];
    NSLog(@"soapMessage :%@",soapMessage);
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"http://tempuri.org/GetPart" forHTTPHeaderField:@"SOAPAction"];

    NSString *msgLength = [NSString stringWithFormat:@"%d",[soapMessage length]];
    [request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPMethod: @"POST"];
    [request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLResponse *returnedResponse = nil;
    NSError *returnedError = nil;
    NSData *barData  = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnedResponse error:&returnedError];
    NSString *barString = [[NSString alloc] initWithBytes:[barData bytes] length:[barData length] encoding:NSUTF8StringEncoding];
    NSLog(@"barString :%@",[[barString stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"] stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"]);

    NSData* data=[[[barString stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"] stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"] dataUsingEncoding:NSUTF8StringEncoding];

    xmlParser = [[NSXMLParser alloc] initWithData:data];       
    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities:NO];
    [xmlParser setShouldProcessNamespaces:NO];
    [xmlParser setShouldReportNamespacePrefixes:NO];
    [xmlParser parse];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    getPartArr =[[NSMutableArray alloc] init];
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i )", [parseError code]];
    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    {
        currentElement = [elementName copy];
        if ([elementName isEqualToString:@"part"])
        {
            item = [[NSMutableDictionary alloc] init];
            part_urlStr = [[NSMutableString alloc]init];
            partNameStr = [[NSMutableString alloc]init];
        }
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if ([currentElement isEqualToString:@"part_url"])
    {
        [part_urlStr appendString:string];
    }
    if ([currentElement isEqualToString:@"partName"])
    {
        [partNameStr appendString:string];
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"part"])
    {
        [item setObject:part_urlStr forKey:@"part_url"];
        [item setObject:partNameStr forKey:@"partName"];
        [getPartArr addObject:[item copy]];
    }
    NSLog(@"item dictionary....%@",partNameStr);
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{    
    NSLog(@"getPartArr :%@",getPartArr);

}

它可以帮助你并给你想要的东西...... 如果你愿意,那就把它标记为已接受