[英]Getting data from a NSURLConnection POST method
I have subclassed an NSData
object into my .h and .m files like so: 我已经将
NSData
对象细分为我的.h和.m文件,如下所示:
@interface Test : NSData // and so forth
I have created a function that generates JSON
data to send back to the server as a POST
. 我创建了一个函数,该函数生成
JSON
数据以POST
发送回服务器。 I get to this part in the code: 我在代码的这一部分:
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Question 1: 问题1:
How do I tell when the connection/post is done? 如何确定连接/发布时间? Is it in the if/else below?
在下面的if / else中吗?
if(conn) {
NSLog(@"Connection Successful");
} else {
NSLog(@"Connection could not be made");
}
Question 2: 问题2:
How do I use these delegate methods below to get the data that is returned? 我如何使用下面的这些委托方法获取返回的数据?
// 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
this worked for me 这对我有用
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:^{
}];
}
}];
for checking connection you can use this statements after NSURLConnection statement 为了检查连接,您可以在NSURLConnection语句之后使用此语句
if(response){NSLog(@"Connection Established");}else{NSLog(@"Connection not established");}
You have to use delegate methods for verification of operation success and to get the data in returned. 您必须使用委托方法来验证操作是否成功并获取返回的数据。
- (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");
});
});
}
set HTTP method (POST or GET). 设置HTTP方法(POST或GET)。 Write this lines as it is in your code.
按照您的代码编写此行。
[request setHTTPMethod:@"POST"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.