When I make a JSON request with NSURLConnection, I get an “unrecognized selector” error [__NSCFDictionary length].
We changed from sending the request in the header as a UTF8String where it worked, to the body which requires NSData for some reason. We made that change because there will eventually be too much data for the header.
Why am I getting this error and how can I fix it? Is this somehow a problem with the object in memory? I’m baffled.
-(void)initWebserviceWithJSONRequest:(NSData *)jsonRequest url:(NSURL *)url
{
//initialize the responseData property //
self.responseData = [[NSMutableData alloc] init];
NSError *error = nil;
NSMutableData *requestData = [[NSMutableData alloc] init];
requestData = [NSJSONSerialization JSONObjectWithData:jsonRequest options:NSJSONWritingPrettyPrinted error:&error];
// create the URL request that will be passed to the NSURLConnection class //
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
// set up the request values //
[request setHTTPMethod:@"POST"];
// line below included to show that there is a length available for requestData
int jsonLength = requestData.description.length;// This works because it's .description.length
[request setHTTPBody:requestData ];
// Make a connection //
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
The call to this method is like this:
NSData *jsonData = [[NSData alloc] initWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[service createWebserviceWithJSONRequest:jsonData url:loginURL];
And here is the json that I’m sending in the body:
{
CURRENTDATETIME = "2012-11-16 18:11:26";
CURRENTUSERTOKEN = "";
REQUESTDATA = "{\"USERNAME\":\"testUserName\", \"PASSWORD\":\"TestPassword\", \"APPVERSION\":\"0.001.1\", \"CARRIER\":\"ATT\", \"DEVICENAME\":\"iPad Simulator\", \"DEVICETOKEN\":\"9A384B42-7766-55AE-B61D-0AAB74A4304B\", \"OSVERSION\":\"5.1\", \"LATITUDE\":10.72182, \"LONGITUDE\":-10.148127, \"ISWIFI\":\"true\", \"BATTERYLEVEL\":42.2, \"SCREENHEIGHT\":1024, \"SCREENWIDTH\":768, \"SCREENPPI\":132, \"SIGNALSTRENGTH\":4, \"APPPACKAGE\":1003}";
TRANSACTIONDATETIME = "2012-11-16 18:11:26";
TRANSACTIONUSERTOKEN = "";
}
NOTE
I’m using xcode Version 4.3.2 (4E2002)
And iOS Simulator Version 5.1 (272.21)
On Mac OS X 10.7.5
In general, when sending data via JSON, you’ve got some relatively complex structure in the phone (or whatever) that you want to convert to a JSON string for transmission.
JSON strings “map” pretty well to NSDictionaries and NSArrays, so the presumption is that you get your data on the phone side into a structure of NSDictionaries and NSArrays and then pass the “root” dictionary/array to
dataWithJSONObject(or, better, a different brand of JSON parser/serializer). The Apple API will return an NSData object from that conversion which can either be sent directly or converted to an NSString and sent, depending on the network interface you use.When/if you get JSON data back you send it through
JSONObjectWithDatato convert into a “nest” of NSArrays and NSDictionaries that your app can understand.You’ve got your inputs and outputs reversed.