我已经成功地实现了NSURLSessionUploadTask,并在后台和前台工作。但是在读取响应数据时存在一个问题。
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
NSLog(@"1 DATA:\n%@\nEND DATA\n", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);
[self.responseData appendData:data];
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
if (!error) {
NSLog(@"AT THE END DATA:\n%@\nEND DATA\n", [[NSString alloc] initWithData: self.responseData encoding: NSUTF8StringEncoding]);
[self parsingJSONResponse:self.responseData];
} else {
NSLog(@"HTTP uploading error : %@", error);
}
}这些是上述两个NSLogs的输出。
1数据:{“成功”:真,"data":{"uuid":"8BE7DF37-9DA1-44D2-B48C-D012F699A9B1","id":266626},{"uuid":"3406D865-1A41-4FC6-BA0B-0638F17757CC","id":266656},"errors":[],"entityName":"LeadProfile"} END DATA 在数据末尾:{“成功”:真,"data":{"uuid":"8BE7DF37-9DA1-44D2-B48C-D012F699A9B1","id":266626},{"uuid":"3406D865-1A41-4FC6-BA0B-0638F17757CC","id":266656},“错误”:[],“entityName”:“LeadProfile”}{“成功”:true,"data":{"uuid":"8BE7DF37-9DA1-44D2-B48C-D012F699A9B1","id":266626},{"uuid":"3406D865-1A41-4FC6-BA0B-0638F17757CC","id":266656},“错误”:[],"entityName":"LeadProfile"}
我不知道为什么这会给我两种不同的回应一个上传任务。self.responseData在每个位置有什么不同?
有人认为这是因为苹果网站上提到的原因吗?(因为NSData对象通常是从许多不同的数据对象拼凑在一起的,所以只要有可能,就使用NSData的enumerateByteRangesUsingBlock:方法来迭代数据,而不是使用字节方法(它将NSData对象扁平成一个内存块)developer.apple.com
发布于 2014-07-18 10:02:18
我已经找到了克服这个问题的方法。但这可能不是一个恰当的答案。
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
//Get Hex string from 'data'. There can be a solution directly add bytes to NSData (using 'enumerateByteRangesUsingBlock') rather than convert to Hex string
NSMutableString *string = [NSMutableString stringWithCapacity:data.length * 3];
[data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop){
for (NSUInteger offset = 0; offset < byteRange.length; ++offset) {
uint8_t byte = ((const uint8_t *)bytes)[byteRange.location + offset];
if (string.length == 0)
[string appendFormat:@"%02X", byte];
else
[string appendFormat:@" %02X", byte];
}
}];
//Hex string to NSdata
NSString *command = string;
command = [command stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
for (int i = 0; i < ([command length] / 2); i++) {
byte_chars[0] = [command characterAtIndex:i*2];
byte_chars[1] = [command characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[commandToSend appendBytes:&whole_byte length:1];
}
NSLog(@"1 >>>>>>>>>>>>>>>>>>> %@", [NSString stringWithUTF8String:[commandToSend bytes]]);
}发布于 2014-08-02 03:39:33
你问:
我不知道为什么这会给我两种不同的回应一个上传任务。
self.responseData在每个位置有什么不同?
这无疑是因为responseData没有被正确地或在正确的时间实例化。(我倾向于在didReceiveResponse中这样做。)注意,在您的responseData中没有看到didReceiveData。你在看data。我建议在附加data in didReceiveData之后立即检查data,我相信您也会看到它翻了一番。问题是为什么它没有正确地实例化/初始化。
有人认为这是因为苹果网站上提到的原因吗? 由于
NSData对象通常是从许多不同的数据对象拼凑在一起的,所以只要有可能,就使用NSData的enumerateByteRangesUsingBlock:方法来迭代数据,而不是使用字节方法(它将NSData对象扁平成一个内存块)。
不,这是一个完全不相关的问题。我敢肯定,这里的问题要普通得多。
不幸的是,我们这里没有足够的代码来诊断精确的问题。
发布于 2014-07-17 16:50:19
您只收到部分数据。您的委托方法应该连接每个交付的数据对象的内容,以便使用NSMutableData对象为URL加载构建完整的数据。
https://stackoverflow.com/questions/24802257
复制相似问题