- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 NSURLSession 与后端进行通信,但是,后端未设置为正确读取 NSData,因此它将我发送的 NSData 登录详细信息视为错误,我想知道是否可以获取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];
最佳答案
您的后端已设置为处理 NSData
。通过 Internet 连接发送字符串是不可能的,您只能发送 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 编码,也许在某些服务器上您需要将其更改为其他编码。
关于ios - 是否有 'trick' 用于在 NSURLSession 连接中发送原始 NSString 而不是 NSData?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23187571/
trick大意 我对于这个trick的理解为:支持位运算的高精度 维护一个以 \(b\) 为基数的大数 \(N\) ,并支持以下功能: 给定(可能是负)整数 \(|x|
我正在为 Java 考试进行培训,我在去年的科目中遇到了一些我不理解的东西。这是代码 class Mother { int var = 2; int getVar() {
在一点帮助下,它几乎是华丽的:TheZoo.com/shapes 除此之外,标题图标和我想要控制的文本之间没有空格。那么,如何在 css pseudo:before 形状中添加边距(或填充),同时避免
是否有一些使用媒体查询的新标准。或者我应该说,应该做出一些新的考虑。 CSS tricks article似乎效果不是很好,因为“智能手机景观”似乎劫持了一切。 我直接使用的代码(打印屏幕应该是什么除
我有一个名为 memory_region 的类,有点像未类型化的 gsl::span (即它本质上是一个 void* 和一个 size_t ),我也将其用于类型删除。因此它有一个 as_span()方
在阅读了 Douglas Crockford 的“JavaScript:The Good Parts”和 Stoyan Stevanov 的“JavaScript Patterns”之后,我试图确定“
最近我发现,如果我需要查看变量是否为偶数(或奇数),我可以只查看变量的最后一位是否等于 0。这一发现在实现后取代了一些 modulo 2 计算,因此整个函数运行得更快。 是否有任何其他像这个一样的“技
我一直在玩这个,到目前为止我还没有找到任何方法来隐藏或欺骗 instanceof 通过隐藏它的类型来返回 false混淆层,但这并不意味着它是不可能的,因为我远不是最了解 Java 的人。所以才来请教
我知道特征散列 (hashing-trick) 用于降低维度和处理位向量的稀疏性,但我不明白它是如何工作的。谁能给我解释一下。是否有任何 python 库可用于进行特征散列? 谢谢。 最佳答案 在 P
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在查看“dlib”库,更具体地说是用于“优化”的“find_min”函数( http://dlib.net/optimization_ex.cpp.html)。 “find_min”函数可让您将自
有据可查的是,IE 中的原生 DOM 元素不包含 hasOwnProperty() 方法。有几个解决方案;其中最优雅的是直接在 Object.prototype 中访问 hasOwnProperty(
This question状态: It is amazing how many users don't know about the rm ./-rf or rm -- -rf tricks. 我不敢
我已经提到了 this site用于布局技巧。 在那里,一个属性被声明为 android:layout_weight="1"。 所以我的困惑是关于 android:layout_weight: and
我正在尝试使一个元素垂直居中,该元素使用 padding-bottom 来创建所需的宽高比(对于视频)。我想要顶部和底部的黑色边框,就像电影以比拍摄时更高的纵横比显示时一样: body{ pa
你能解释一下下一个代码示例背后的机制吗(我想我知道,但我需要第二个意见): 1)---------------------------- using namespace std; int * f(in
滚动浏览 http://git.suckless.org/st/plain/st.c我偶然发现 #define LEN(a) (sizeof(a) / sizeof(a)[0]) 我知道预编译魔法很难
我正在阅读有关 Java 泛型的文章,我偶然发现了这个主题,我对此感到有些困惑。 发件人:http://www.angelikalanger.com/GenericsFAQ/FAQSections/P
我有一点挑战。 在我们产品的早期版本中,我们有一个错误消息窗口(不得已,未处理的异常)显示异常消息、类型、堆栈跟踪 + 各种信息。 这个窗口是打印屏幕友好的,因为如果用户简单地进行打印屏幕捕获,并将屏
可以在 C 中将某些类型的泛型函数作为宏来执行,例如: #define SQRT(x) (sizeof(x) == sizeof(float) ? sqrtf((x)) : \
我是一名优秀的程序员,十分优秀!