Question

I have an error when I try to make a wcf method work. One of the parameters are NSMutableArray and the Object type is TypeClass. I checked everything with the NSMutableArray and everything seems fine.

My code for the parameter is:

NSData *myPostData = [[NSString stringWithFormat:@"{\"parameter1\":\"%@\"}",mynsmutablearraylist] dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *myMutablePostData = [NSMutableData dataWithData:myPostData];

The parameter type in wcf is like List< TypeClass>.

I can see the values of the object and keys of the mynsmutablearraylist. But I can't understand why it doesn't work.

This wcf method works for the Win8 metro application but I can't make it work for IOS.

The server encountered an error processing the request. The exception message is 'The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:typeclass. The InnerException message was 'There was an error deserializing the object of type System.Collections.Generic.List`1[[ServiceOfMine.Entities.TypeClass, ServiceOfMine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Encountered invalid character ' '.'. Please see InnerException for more details.'. See server logs for more details. The exception stack trace is:

Added Info:

When I test the parameters with the exactly the same values , it works fine. The part of the Request in SOAPUI:

> >....
> >      <tem: parameter4>parvalue</tem: parameter4>
> >      < tem: typeclass>
> >         <!--Zero or more repetitions:-->
> >         <ser:TypeClass>
> >            
> >            <ser:drugId></ser:drugId>
> >            <ser:drugname></ser:drugname>
> >         </ser:TypeClass>
> >         <ser:TypeClass>
> >            
> >            <ser:drugId></ser:drugId>
> >            <ser:drugname></ser:drugname>
> >         </ser:TypeClass>
> >      </ tem: typeclass>
> >      ...
Was it helpful?

Solution

On the basis of your original question's @"{\"parameter1\":\"%@\"}" format string, I assumed you were trying to build a JSON request. But that's probably not the best way to create JSON request, so my original answer (below) was focusing on how to properly build that JSON request.

But to step back, there are at least three different types of common web service interfaces:

  • JSON, which I (perhaps incorrectly) inferred you wanted on the basis of your original question, hence my original answer, below;

  • XML, which your revised question suggests you might want for your SOAP request; or

  • some standard HTTP request (e.g. application/x-www-form-urlencoded or other common variants).

Regardless of which of these you are trying to do, using stringWithFormat, using %@ for the NSMutableArray parameter is undoubtedly incorrect.

So, the question is precisely what format the request needs. If, as your revised question suggests, you need to create a SOAP request, you either need to build that manually, adding the XML tags yourself, or you have to use some library. If you are doing this for Mac OS X, see Web Services Core Programming Guide. If you are doing for iOS, you'd have to find a third-party library. Perhaps this Stack Overflow question is a good starting place: How to access SOAP services from iPhone. Or search Stack Overflow for "[ios] soap request" and you'll get lots of other hits.

Personally, when testing SOAP services from iOS app, I've built the request manually, but it's clumsy and awkward. If your web service offered a JSON interface, that might be a preferable, as that's an interface with far greater iOS support.

Anyway, my original answer, showing how to build the POST body for a JSON request is below.


Original answer:

It's impossible for us to get more specific without more details about the complete format of the request, but if you're just trying to create a JSON request, you'd create that NSData using NSJSONSerialization, not stringWithFormat, e.g. probably do something like:

NSDictionary *dictionary = @{@"parameter1": mynsmutablearraylist,
                             @"parameter2": someOtherObject};
NSError *error = nil;
NSData *postData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSAssert(postData, @"%s: dataWithJSONObject error: %@", __FUNCTION__, error);

You also refer to the "objects and keys of the mynsmutablearraylist", but I'm confused by the name (which would suggest that its an NSMutableArray and your reference to "objects and keys"). But I'll presume that was an array dictionaries, in which case the above syntax still works. But, again, it's hard to say without knowing precisely what your service is looking for.

I also don't understand why you're creating the NSMutableData rendition, but if you're trying to add more parameters to this JSON, you really should add those to the original dictionary first and then use NSJSONSerialization once you've completely built your dictionary/array structure that you want in JSON format. For example:

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:mynsmutablearraylist forKey:@"parameter1"];
[dictionary setObject:someOtherObject      forKey:@"parameter2"];
// ... repeat for all of the objects in the dictionary

NSError *error = nil;
NSData *postData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSAssert(postData, @"%s: dataWithJSONObject error: %@", __FUNCTION__, error);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top