将NSMutableDictionary转换为NSData时崩溃

时间:2014-09-09 10:27:14

标签: objective-c nsdictionary nsdata

在这一行

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error];

我尝试将参数(NSMutableDictionary)转换为NSData,因为我需要它用于HTTPRequest。 它一直工作到现在,但现在崩溃说

Crash: Invalid type in JSON write (NSURL)

我提供的所有参数都是NSString,但其中2个是URL格式但仍然是NSString,那就是错误,如果我评论这两个参数一切都很好。 有什么想法吗?

这是代码

+(NSDictionary*)facebookLogIn:(NSString*)facebookID fbEmail:(NSString*)fbEmail fbFullName:(NSString*)fbFullName profilePhoto:(NSString*)profilePhoto coverPhoto:(NSString*)coverPhoto
{
NSError *error;

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *requestString = [NSString stringWithFormat:@"%@login",API];
[request setURL:[NSURL URLWithString:requestString]];
[request setHTTPMethod:@"POST"];
[request setValue:@"Connection" forHTTPHeaderField:@"Keep-Alive"];
[request setCachePolicy:NSURLCacheStorageNotAllowed];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-type"];

NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];

[parameters setObject:APIKEY forKey:@"APIKEY"];
[parameters setObject:facebookID forKey:@"fbUserID"];
[parameters setObject:fbEmail forKey:@"email"];
[parameters setObject:fbFullName forKey:@"fullName"];
[parameters setObject:profilePhoto forKey:@"profilePhoto"];
[parameters setObject:coverPhoto forKey:@"coverPhoto"];

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];

NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSASCIIStringEncoding];

NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: [theReply dataUsingEncoding:NSUTF8StringEncoding]
                                                     options: NSJSONReadingMutableContainers
                                                       error: &error];

0 个答案:

没有答案
相关问题