2012-11-01 2 views
0

NSArray를 웹 서비스에 보낼 수있는 성공이 없습니다.
서비스 메소드가 호출되지 않습니다. 그러나 다른 서비스 방법은 작업을 호출합니다.
웹 서비스 방법은 다음과 같습니다웹 서비스에 NSArray를 보내는 방법

NSString *soapMsg = 
    [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>" 
    "<Method xmlns=\"http://www.mysite.com/\">" 
    "<items>" 
    "%@" 
    "</items>" 
    "</Method>" 
    "</soap:Body>" 
    "</soap:Envelope>", myArray 
    ]; 

    NSURL *url = [NSURL URLWithString: @"http://...."]; 

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]]; 

    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
    [req addValue:@"http://www.mysite.com/Method" forHTTPHeaderField:@"SOAPAction"]; 
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
    [req setHTTPMethod:@"POST"]; 
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; 

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; 

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



:이 같은 데이터를 보내려고 목표 C에서

@interface Item : NSObject 
{ 
    double prop1; 
    double prop2; 
    double prop3; 
} 
@property double prop1; 
@property double prop2; 
@property double prop3; 

:

[WebMethod] 
     public int Method(IList items){... 

내 배열에 객체 항목이 가득 업데이트

이 같은 배열 할 때 :

NSArray * myArrDate = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil]; 

그리고 만들 : 그것은 작동

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myArrDate 
                 options:0 
                 error:nil]; 

합니다. 하지만 배열이 User이고 배열은 UserId, FirstName, LastName, Email, Username, DisplayName, Country, City 등입니다. 배열로 위의 코드를 시도하면 해당 행에 응용 프로그램 충돌이 발생합니다.

답변

6

당신은 NSArray

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:yourArray options:NSJSONWritingPrettyPrinted error:nil]; 
     NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding]; 
     NSLog(@"FemaleFriendList:\n%@", jsonString); 
+0

같은 왕자 대답. 이 앱을 실행할 때 충돌이 발생합니다. 어떤 오류도없이. – 1110

+0

질문이 업데이트되었습니다. – 1110

+0

좋아, NSJSONSerialization을 사용하려면 동일한 속성을 가진 새로운 클래스를 만들 필요가 있지만 모두 NSString이되어야한다는 것을 알았다. 내가 이것을 할 때 그것은 작동한다. 나는이 대답을 받아 들일 것이다. – 1110

0

NSString (% @, myArray)을 매개 변수로 보내고 webservice는 IList 객체를 필요로합니다. NSArray를 보내기 전에 직렬화하지 않는 이유는 무엇입니까? 예를 들어 JSON을 사용할 수 있습니다.

+0

당신은 몇 가지 코드 예제를 제공 Ca는? – 1110

+0

JSON에 직렬화하는 방법은 무엇입니까? –

+0

예. 이 작업을 위해 일부 프레임 워크를 사용할 필요가 없다면 좋을 것입니다. 보낸 인식 할 수없는 선택 : - – 1110

1

당신은 웹 서비스에있는 NSArray를 보내지 만 먼저 그럼 수행하려는 경우 NSArray를있는 NSString에 같은 다음

NSString *stringDelete = [YourNSArray componentsJoinedByString:@","]; 

이하로하는 것이있는 NSString은 내가 그것을인지 알고 webservice.Let 것을 전달할 것을 변환 할 수 없습니다 일하는지 아닌지 !!!!!! 해피 코딩 ... !!!

1

webservice에 옵션을 보낼 수있는 그 이상을 convert-nsdictionary-or-nsarray-to-json-nsstring-and-vice-versa 링크를 참조하십시오 5

NSArray *myArrDate = any Data .... 
NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myArrDate 
                options:0 
                error:nil]; 
if (!jsonData) { 
    NSLog(@"error"); 
} else { 
    NSString *JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@",JSONString); 
} 

이다 문자열과 그 반대의 가능한 형태의 아이폰 OS로 배열을 변환 NSJSONSerialization를 사용하다 정보.

+0

이 앱을 실행할 때 충돌이 발생합니다. 어떤 오류도없이. – 1110

+0

질문이 업데이트되었습니다. – 1110

관련 문제