1

大家好,我在处理 wcf json web-services 时遇到了一个奇怪的问题。当从 android 调用相同的 web 服务时,它返回有效的 JSON,但是当我从 iOS 调用时,我得到 null 并且只有 null ,我尽我所能尝试,但我没有弄错..我调用 wcf Web 服务的代码如下。

    NSString *urlStringRequest = [NSString stringWithFormat:@"%@%@",urlPath,parameterName]; //its my URL to connect to web server.

       NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStringRequest]];
       [request setHTTPMethod:@"POST"];
       [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

       NSError *error = nil;
       NSURLResponse *response = nil;
       NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

       NSArray *jsonArrayOne = [self parseJsonResult:result];

       NSLog(@"Json Resul: %@", jsonArrayOne);

函数 parseJsonResult 如下

     - (id)parseJsonResult:(NSData *)result
    {
        if( ! result)
            return nil;
        NSError *error = nil;


        jsonArray = [NSJSONSerialization JSONObjectWithData: result options: NSJSONReadingMutableContainers error: &error];

        if (!jsonArray) {
            NSLog(@"Error parsing JSON: %@", error);
        } else {
            for(NSDictionary *item in jsonArray) {
                NSLog(@"Item: %@", item);
            }
        }
        return jsonArray;
}

网址就像

http://xxx/xxx/xxx.svc/login?parameter={\"username\":\"abc.xyz\",\"password\":\"Password123\"}

并从 android 调用这个 wcf web-service 返回正确的 json 响应,但从 iOS 调用返回 null 。我在哪里弄错了,请帮助我。

4

1 回答 1

0

试试这个代码,当你得到正确的 JSOn 然后 c & p 你的 json 到jsonformatter 块然后点击继续按钮

-(void)yourMethod
{
    NSString *urlStringRequest = [NSString stringWithFormat:@"%@%@",urlPath,parameterName]; //its my URL to connect to web server.

       NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStringRequest] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
       [request setHTTPMethod:@"POST"];

       NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
       NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];

       [request addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];

       NSMutableData *postBody = [NSMutableData data];

      // media part
       [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

       [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

       [request setHTTPBody:postBody];

       NSHTTPURLResponse* response =[[NSHTTPURLResponse alloc] init];
       NSError* error = [[NSError alloc] init] ;

      //synchronous filling of data from HTTP POST response
      NSData *responseData = [NSURLConnection sendSynchronousRequest:dataRqst returningResponse:&response error:&error];

       //convert data into string
       NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];

      NSLog(@"responseString %@ ",responseString);

    if (!error) {
        NSLog(@"Error parsing JSON: %@", error);
    } else {
        if([[[[responseString objectForKey:@"LoginResult"] objectForKey:@"result"] objectForKey:@"success"] isEqualToString:@"1"];)
         {
            jsonArray= [[[[responseString objectForKey:@"LoginResult"] objectForKey:@"result"] objectForKey:@"success"] objectForKey:@"data"];
             NSLog(@"jsonArray : %@",jsonArray);
         }
    }
    return jsonArray;
}

希望对你有帮助……!

于 2013-05-29T13:46:03.103 回答