2012-09-18 3 views
1

저는 개인 서버에서 온라인 검색을하고 (희망적으로) 사용자에게 결과를 보여주는 앱을 프로그래밍하고 있습니다. 테이블보기를 사용하여 결과를 표시하려고합니다.서버에서 XML 응답을 사용하는 방법은 무엇입니까?

내가 할 수있는 것 : 데이터를 보내고 응답을 받고 XML을 구문 분석하고 결과를 배열에 저장합니다.

문제 : - (void)connectionDidFinishLoading:(NSURLConnection *)connection에서 응답 데이터를 저장하는 배열에 액세스 할 수 없습니다.

그러나이 메서드 내에서 동일한 배열에 액세스 할 수 있습니다. 나는 총 초보자이고 혼란 스럽다. 나는 인스턴스 변수를 생성하는 방법에의 SearchResult 배열 초기화 : connectionDidFinishLoading,하지만 난이 방법에서이 배열에 액세스하려고하면 단지

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds for empty array

내가 : connectionDidFinishLoading를, 난 항상 오류를 얻고있다 이 인스턴스 변수가 로컬에서만 초기화 된 이유를 이해할 수 없습니다.

그래서 질문 : 내가 어떻게 밖으로 서버에서 얻은 데이터에 액세스 할 수 있습니다 : connectionDidFinishLoading?

도움이 매우 대단히 감사합니다. 미리 감사드립니다. 건배 페티 P.

- (void)transmitXMLRequest:(NSData *)data{ 
    NSURL *webServiceURL = [NSURL URLWithString:@"some_url"]; 

    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:webServiceURL]; 
    [urlRequest setHTTPMethod:@"POST"]; 
    [urlRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-type"]; 
    [urlRequest setHTTPBody:data]; 

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
    responseData = [[NSMutableData alloc] init]; 
    [connection start]; 


    if (!connection) { 
     NSLog(@"Failed to submit request"); 
    } else { 
     NSLog(@"Request submitted"); 
    }  
}´. 

`과 위임 방법 :

`이 여기

내 코드는 ... 아무것도 내 질문에 이상이있는 경우 내 첫 번째 질문은 지금, 실례이다

-  (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{  
    // responseData is an instance variable. 
    [responseData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    // responseData is an instance variable. 
    [responseData appendData:data]; 

} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 

    connection = nil; 
    responseData = nil; 

    NSLog(@"Connection failed! Error - %@ %@", 
      [error localizedDescription], 
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
} 

이 방법으로 만 데이터에 액세스 할 수 있습니다 :

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

    NSLog(@"Succeeded! Received %d bytes of data",[responseData length]); 

    responseString = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]; 

    NSLog(@"Received file: \n%@", responseString); 


    NSData *_data= [responseString dataUsingEncoding:NSUTF8StringEncoding]; 
    xmlParser = [[XMLParser alloc]loadXML:_data]; // _data is the response from server 

    controller.searchResults = [[NSMutableArray alloc] init]; 
    controller.searchResults = [xmlParser jobs]; 

빠른 응답을 위해! 제 생각에는

+0

은 당신의 코드 예제를 붙여 넣기 – secondflying

답변

0

는 SOAP 사용해야 http://en.wikipedia.org/wiki/SOAP

SOAP, originally defined as Simple Object Access Protocol, is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks. It relies on Extensible Markup Language (XML) for its message format, and usually relies on other Application Layer protocols, most notably Hypertext Transfer Protocol (HTTP) and Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission.

관련 문제