2011-04-21 3 views
0

의 결과를 얻을 수는 닷넷 프로그래머 다음과 같이아이폰 APP에 접근 .NET의 WCF 서비스와 라벨

내 요구 사항이 아이폰 애플 리케이션과의 Obj C.에 초보자입니다.

.net WCF 서비스에서 메소드를 사용하는 iPhone 앱이 필요하며 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 

#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> 

는 M 파일 - 나는 10 100을 곱 시도하고 내가 가진

그 결과. 하지만 내가 어떻게

렌주

답변

0

당신은 먼저 해당 XML을 구문 분석 할 필요가 ... 내 라벨 또는 콘센트 .. 사전에

감사 단지 1000를 인쇄 할 수 있습니다. 그런데 왜 당신은 JSON 사용 myLabel.text = [responseDict objectForKey:@"value"];

구문 분석하기 위해 할 JSON 파일 {"value" = 1000}로하고 아이폰 응용 프로그램에 서버에서 응답하지 않는 sbjson

편집 :

Event-Driven XML Programming Guide

Introducing JSON

+0

Alexandru .. 빠른 답장을 보내 주셔서 감사합니다.하지만 XML을 구문 분석하고 json 파일을 만드는 방법을 어떻게 알 수 있습니까? – Renju

관련 문제