在发送POST请求时,我执行调试器从NSURLSessionDataTask跳跃和日志中生成的代码来休息基于URL IOS
问题描述:
-(void)showData {
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/posts"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"1", @"number",nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(error == nil)
{
NSString *text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
NSLog(@"data is %@", data);
NSLog(@"response is %@" , response);
}];
[postDataTask resume];
}
nsurlsessiondatatask返回nil是__NSCFLocalDataTask:0x7ff061751960> {taskIdentifier:1} {悬浮}并且不NSData和NSResponse中的任何数据。在发送POST请求时,我执行调试器从NSURLSessionDataTask跳跃和日志中生成的代码来休息基于URL IOS
答
@interface UrClass:NsObject<NSURLConnectionDelegate>{ NSMutableData *_receivedData;
}
-(void)showData{
NSString * urlStr = [NSString stringWithFormat:@"%@", path];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
[theRequest setURL:[NSURL URLWithString:urlStr]];
NSString *requestStr = [dictionary JSONRepresentation];
NSData *requestData = [requestStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", (unsigned int)[requestData length]];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:requestData];
[theRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setTimeoutInterval:REQUEST_TIME_OUT_SECS];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:NO];
[theConnection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[theConnection start];
if (theConnection)
_receivedData = [NSMutableData data];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[_receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}
答
试试这个代码:
NSURL *url=[NSURL URLWithString:@"https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/posts"];
NSData *contactData=[NSData dataWithContentsOfURL:url];
NSMutableArray *allContectData=[NSJSONSerialization JSONObjectWithData:contactData options:0 error:nil];
NSLog(@"%@",allContectData);
否则使用此代码
-(void)showData{
NSString *urlString = @"https://public-api.wordpress.com/rest/v1.1/sites/en.blog.wordpress.com/posts";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NO timeoutInterval:20.0f];
responseData = [[NSMutableData alloc] init];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
使用NSURLConnectionDataDelegate
委托方法
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSMutableArray *allContectData=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",allContectData);
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%@",error);
}
请参考[这里](HTTP://计算器.COM /问题/ 23731043 /如何到获得服务器,RESP onse-data-in-nsurlsession-without-completion-block)链接,我想你会从中得到一个想法: –