当我使用 NSURLConnection 发出 JSON 请求时,我收到“无法识别的选择器”错误 [__NSCFDictionary 长度]。
我们从将请求在标头中作为 UTF8String 发送到它工作的地方,更改为由于某种原因需要 NSData 的正文。我们进行了更改,因为最终会有太多的数据用于标题。
为什么我会收到此错误,我该如何解决?这在某种程度上是内存中的对象的问题吗?我很困惑。
-(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];
}
这个方法的调用是这样的:
NSData *jsonData = [[NSData alloc] initWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[service createWebserviceWithJSONRequest:jsonData url:loginURL];
这是我在正文中发送的 json:
{
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 = "";
}
注意我使用的是 xcode 版本 4.3.2 (4E2002)
和 iOS 模拟器版本 5.1 (272.21)
在 Mac OS X 10.7.5 上