Ios 从NSURLConnection POST方法获取数据
我已将Ios 从NSURLConnection POST方法获取数据,ios,objective-c,iphone,ios9,nsurlconnection,Ios,Objective C,Iphone,Ios9,Nsurlconnection,我已将NSData对象子类化到我的.h和.m文件中,如下所示: @interface Test : NSData // and so forth 我创建了一个函数,该函数生成JSON数据,以POST的形式发送回服务器。我在代码中找到了这一部分: NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 问题1: 如何判断连接/post何时完成?是否在下面的if/else中
NSData
对象子类化到我的.h和.m文件中,如下所示:
@interface Test : NSData // and so forth
我创建了一个函数,该函数生成JSON
数据,以POST
的形式发送回服务器。我在代码中找到了这一部分:
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
问题1:
如何判断连接/post何时完成?是否在下面的if/else中
if(conn) {
NSLog(@"Connection Successful");
} else {
NSLog(@"Connection could not be made");
}
问题2:
如何使用下面这些委托方法获取返回的数据
// This method is used to receive the data which we get using post method.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data
// This method receives the error report in case of connection is not made to server.
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
// This method is used to process the data after connection has made successfully.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
您必须使用委托方法来验证操作是否成功,并获取返回的数据
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
NSLog("@Resp received");
return;
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
NSLog("@Data received");
return
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
NSLog("@ERROR: Achtung !: %@",[error localizedDescription]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH , 0),, ^{
NSLog(@"FinishedLoading: In bg thread, do something with data here");
dispatch_async( dispatch_get_main_queue(), ^{
NSLog(@"FinishedLoading: In Main thread, access the UI here");
});
});
}
您必须使用委托方法来验证操作是否成功,并获取返回的数据
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
NSLog("@Resp received");
return;
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
NSLog("@Data received");
return
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
NSLog("@ERROR: Achtung !: %@",[error localizedDescription]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH , 0),, ^{
NSLog(@"FinishedLoading: In bg thread, do something with data here");
dispatch_async( dispatch_get_main_queue(), ^{
NSLog(@"FinishedLoading: In Main thread, access the UI here");
});
});
}
这对我有用
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"API link"]
NSLog(@"%@",url.standardizedURL);
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError)
{
NSLog(@"balh %@",connectionError);
if (data.length > 0 && connectionError == nil)
{
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:data
options:0
error:NULL];
NSString * string = [[NSString alloc] initWithData:data encoding:NSStringEncodingConversionAllowLossy];
NSLog(@"%@ %@",response,string);
[self dismissViewControllerAnimated:YES completion:^{
}];
}
}];
要检查连接,可以在NSURLConnection语句之后使用此语句
if(response){NSLog(@"Connection Established");}else{NSLog(@"Connection not established");}
这对我有用
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"API link"]
NSLog(@"%@",url.standardizedURL);
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data, NSError *connectionError)
{
NSLog(@"balh %@",connectionError);
if (data.length > 0 && connectionError == nil)
{
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:data
options:0
error:NULL];
NSString * string = [[NSString alloc] initWithData:data encoding:NSStringEncodingConversionAllowLossy];
NSLog(@"%@ %@",response,string);
[self dismissViewControllerAnimated:YES completion:^{
}];
}
}];
要检查连接,可以在NSURLConnection语句之后使用此语句
if(response){NSLog(@"Connection Established");}else{NSLog(@"Connection not established");}
设置HTTP方法(POST或GET)。按代码中的方式编写这行代码
[request setHTTPMethod:@"POST"];
设置HTTP方法(POST或GET)。按代码中的方式编写这行代码
[request setHTTPMethod:@"POST"];
我是否从DidReceiveData返回数据,如[_theDataAppendData];然后使用_thedatain my if(conn)loop?@cdub-是的,
didReceiveData
应该附加到NSMutableData
,但是您在connectiondifinishload
中处理它,而不是回到您实例化NSURLConnection
的地方。我是否像[_thedataappenddata]那样从didReceiveData返回数据;然后使用_thedatain my if(conn)loop?@cdub-是的,didReceiveData
应该附加到NSMutableData
,但是您在connectiondFinishLoading
中处理它,而不是回到您实例化NSURLConnection
的地方。顺便说一句,如果您使用NSURLSession
,您可以享受基于块的完成处理程序的简单性(有点像NSURLConnection
的sendAsynchronousRequest
…如果您不想,无需担心委托方法),但是(a)它是可取消的;(b)避免使用现在已弃用的NSURLConnection
。此外,我不清楚您为什么要麻烦对NSData
进行子类化。这通常是不必要的。通常您只需使用NSMutableData
@Rob谢谢。是的,我将使用NSURLSession。应该先检查它是否被弃用。顺便说一句,如果您使用NSURLSession
,您可以享受基于块的完成处理程序的简单性(有点像NSURLConnection
的sendsynchronousrequest
…如果您不想,无需担心委托方法),但是(a)它是可取消的;(b)避免使用现在已弃用的NSURLConnection
。此外,我不清楚您为什么要麻烦对NSData
进行子类化。这通常是不必要的。通常您只需使用NSMutableData
@Rob谢谢。是的,我将使用NSURLSession。应该先检查它是否已弃用。