在NSURLSession连接中发送原始NSString而不是NSData有“技巧”吗?

时间:2014-04-20 21:05:47

标签: ios objective-c json nsdata nsurlsession

我尝试使用NSURLSession与后端进行通信,但是,后端没有设置为正确读取NSData,因此它将我发送的NSData登录详细信息视为false,我想知道是否它可以让NSURLSession发送原始字符串而不是NSData对象。我已经看过书本和网页,而且我已经被困了几个星期。

重做后端不是一种选择,负责此事的工程师离开了。欢迎任何帮助。

感谢。这是我到目前为止所做的事情,以防任何人需要查看某些代码。

NSError *error;

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration ];

NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

NSString *rawUrlString = @"backend_url";

NSURL *url = [NSURL URLWithString:rawUrlString];

NSLog(@"%@", url);


NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                            login data here

                            nil];

//NSLog(@"%@", parameters);

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

NSString *myString = [[NSString alloc] initWithData:rawJson encoding:NSUTF8StringEncoding];

NSData *finalData = [myString dataUsingEncoding:NSUTF8StringEncoding];


NSMutableData *body = [NSMutableData data];

[body appendData:finalData];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:body];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"charset"];
[request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];

NSURLSessionDataTask *postTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
     NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]);

}];

[postTask resume];

`

更新:我已经清理了不必要的位,这是最终的代码(但仍然没有工作)

 NSError *error;

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration ];

NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

NSString *rawUrlString = @"backend_url";

NSURL *url = [NSURL URLWithString:rawUrlString];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:login details nil];


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

[request setHTTPMethod:@"POST"];
[request setHTTPBody:rawJson];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSURLSessionDataTask *postTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
     NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]);

}];

[postTask resume];

1 个答案:

答案 0 :(得分:2)

您的后端设置为处理NSData。通过互联网连接发送字符串是不可能的,您只能发送NSData并且所有服务器都期望它。

此代码错误:

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

NSString *myString = [[NSString alloc] initWithData:rawJson encoding:NSUTF8StringEncoding];

NSData *finalData = [myString dataUsingEncoding:NSUTF8StringEncoding];

相反,只需这样做:

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

NSData对象将包含正确格式的字符串,服务器也可以将其识别为字符串。请注意,它将采用UTF-8编码,可能在某些服务器上,您可能希望将其更改为其他内容。