2011-04-21 16 views
0

是一个.Net程序员和新手到iPhone应用程序和对象C.iPhone应用程序访问.net WCF服务,并得到结果在标签

我的要求如下。

我需要一个iPhone应用程序,它使用.net WCF服务中的方法,并将结果显示在iPhone中的标签或文本框中。

我有用于乘以2个数字的wcf。当我做NSLog时,它在那里得到结果。但我只需要值即我给5 * 10,我需要50在我的标签。相反,它给予整个东西..

请在这里找到我的代码。

的.h文件

#import <UIKit/UIKit.h> 

@interface iServiceViewController : UIViewController { 
     UILabel *label; 
     NSMutableData *webData; 
} 
@property(nonatomic,retain) IBOutlet UILabel *label; 
@end 

M档

#import "iServiceViewController.h" 

@implementation iServiceViewController 
@synthesize label; 



// Implement viewDidLoad to do additional setup after loading the 
view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
     NSString *soapMessage = [NSString stringWithFormat: 
                 @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 
                 "<SOAP-ENV:Envelope \n" 
                 "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n" 
                 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" 
                 "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" \n" 
                 "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" 
\n" 
                 "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n" 
                 "<SOAP-ENV:Body> \n" 
                 "<GetProduct xmlns=\"http://tempuri.org/\">" 
                 "<first>100</first><second>10</second>" 
                 "</GetProduct>\n" 
                 "</SOAP-ENV:Body> \n" 
                 "</SOAP-ENV:Envelope>"]; 
     NSURL *url = [NSURL 
URLWithString:@"http://192.168.0.115:1010/Service1.svc"]; 
    NSMutableURLRequest *theRequest = [NSMutableURLRequest 
requestWithURL:url]; 
    NSString *msgLength = [NSString stringWithFormat:@"%d", 
[soapMessage length]]; 
    [theRequest addValue: @"text/xml; charset=utf-8" 
forHTTPHeaderField:@"Content-Type"]; 
    [theRequest addValue: @"http://tempuri.org/IService1/GetProduct" 
forHTTPHeaderField:@"Soapaction"]; 
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
    [theRequest setHTTPMethod:@"POST"]; 
    [theRequest setHTTPBody: [soapMessage 
dataUsingEncoding:NSUTF8StringEncoding]]; 
    NSURLConnection *theConnection = [[NSURLConnection alloc] 
initWithRequest:theRequest delegate:self]; 

    if(theConnection) { 
     webData = [[NSMutableData data] retain]; 
    } 
    else { 
     NSLog(@"theConnection is NULL"); 
     } 
} 

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

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

- (void)connection:(NSURLConnection *)connection 
didFailWithError:(NSError *)error { 
     label.text = [NSString stringWithFormat:@"Connection failed: %@", 
[error description]]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
     [connection release]; 

     NSLog(@"Data has been loaded"); 

     //NSString *responseString = [[NSString alloc] initWithData:webData 
encoding:NSUTF8StringEncoding]; 
     //NSInteger *responseInt = [[NSInteger alloc] initWithData:webData 
encoding:NSInteger]; 
     NSString *responseString = [[NSString alloc] initWithData:webData 
encoding:NSUTF8StringEncoding]; 
     [webData release]; 

     //label.text = responseString; 
     NSLog(responseString); 

     [responseString release]; 

} 

- (void)dealloc { 
    [super dealloc]; 
} 

@end 

我NSLog的输出

[Session started at 2011-04-21 12:04:48 +0300.] 
2011-04-21 12:04:49.579 iService[1978:207] Data has been loaded 
2011-04-21 12:04:49.580 iService[1978:207] <s:Envelope 
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetProductResponse 
xmlns="http://tempuri.org/"><GetProductResult>1000</GetProductResult></GetProductResponse></s:Body></s:Envelope> 

-

我试图用10乘以100,我得到结果也。但如何我可以在标签或任何出口..

感谢事先打印只需1000 ..

连珠

回答

0

您需要首先解析XML。但是,你为什么不从服务器响应与JSON文件{"value" = 1000}在你的iPhone应用程序也myLabel.text = [responseDict objectForKey:@"value"];

解析JSON使用sbjson

编辑:

Event-Driven XML Programming Guide

Introducing JSON

+0

Alexandru ..感谢您的快速回复。但我该如何解析XML以及如何制作json文件.. – Renju 2011-04-21 11:36:09

相关问题