Webservice n sqlite [英] Webservice n sqlite

查看:191
本文介绍了Webservice n sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有网络服务,我想将这些数据保存在iPhone中的SQLite中,并且还希望检索该数据。 Web服务包括14个参数,还包括图像URL。 Web服务是.NET中的SOAP。

I have web services and I want to save that data in SQLite in iPhone and also want to retrieve that data. Web services include 14 parameters, also includes image URL as well. Web service is SOAP in .NET.

请帮助我并提供完整的代码如何做到这一点。

Please help me and provide me with the complete code how to do that.

推荐答案

Web服务可能使用Java,PHP,.NET等...但您必须使用相同的过程来发出请求。
这里我给出了一个示例代码来发出请求并从webservices获得响应。

Webservices may be in Java, PHP, .NET and etc... But you have to use same procedure to make a request. Here I have given sample code to make a request and get the response from webservices.


-(void)performRequest{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"url"]];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
    [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue: soapAction forHTTPHeaderField:@"SOAPAction"];
    [request addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if( theConnection )
    {
        webData = [[NSMutableData data] retain];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
    [pool release];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    [webData setLength: 0];
    self.resultArray = [[NSMutableArray alloc] init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"ERROR with theConenction");
    NSDictionary *errorDic = [NSDictionary dictionaryWithObject:error forKey:@"error"];
    [self.resultArray addObject:errorDic];
    [connection release];
    [webData setLength:0];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@", theXML);
    [theXML release];
    if([webData length] > 0){
        parser = [[NSXMLParser alloc] initWithData:webData];
        [parser setDelegate:self];
        [parser parse]; 
    }
}

在此示例中,webData (NSData)具有响应数据。请求应采用XML格式,响应数据也采用XML格式。使用NSXMLParser可以解析数据。
有一些委托方法。你必须使用下面指定的方法;

In this example, "webData"(NSData) having the response data. The request should be in XML format and also the response data will be in XML format. Using NSXMLParser you can parse the data. There is some delegate methods. You have to use below specified methods;


1. - (void)parserDidStartDocument:(NSXMLParser *)parser
2. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
3. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
4. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
5. - (void)parserDidEndDocument:(NSXMLParser *)parser
6. - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError

在第二个委托方法中,你将获取元素名称(xml标记名称)。在第3个委托方法中
,您将获得元素名称的值。

in 2nd delegate method, you will get the element name (xml tag name). in 3rd delegate method, you will get the value for the element name.

我希望,它会对您有帮助。

I hope, it will help you.

这篇关于Webservice n sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆