Ios 我如何打印来自我简单的工作POST方法的响应
我有一个post方法,如下所示:Ios 我如何打印来自我简单的工作POST方法的响应,ios,objective-c,post,Ios,Objective C,Post,我有一个post方法,如下所示: NSString *totalPostURL = [NSString stringWithFormat:@"%@registerDevice",self.textUrl]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:totalPostURL]]; NSE
NSString *totalPostURL = [NSString stringWithFormat:@"%@registerDevice",self.textUrl];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:totalPostURL]];
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:self.finalDict options:0 error:&error];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
代码运行良好,但我不知道如何打印这篇文章的回复。欢迎您提出任何建议,因为我是iOS开发新手。您将在连接代表中得到回复。寻找方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
您将在连接委托中得到响应。寻找方法
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
您可以按如下方式打印响应:
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"Response:%@",returnString);
但请记住,作为一名新手,不要忘记阅读有关调用Web服务的教程。您可以将响应打印如下:
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"Response:%@",returnString);
但请记住,作为一名新手,不要忘记阅读有关调用Web服务的教程。您可以这样使用
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *jsonString = [[NSString alloc] initWithString: receivedData];
NSData* cData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *WSerror;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:cData options:NSJSONReadingAllowFragments error:&WSerror];
}
打印响应项。您可以这样使用
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *jsonString = [[NSString alloc] initWithString: receivedData];
NSData* cData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *WSerror;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:cData options:NSJSONReadingAllowFragments error:&WSerror];
}
打印响应。@Ayan Khan是对的!在这里,我添加了http打印后响应的示例代码,并尽可能将其解析为JSON,它将异步处理所有内容,因此您的GUI将很好地刷新,并且不会冻结,这一点很重要
//POST DATA
NSString *theBody = [NSString stringWithFormat:@"parameter=%@",YOUR_VAR_HERE];
NSData *bodyData = [theBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//URL CONFIG
NSString *serverURL = @"https://your-website-here.com";
NSString *downloadUrl = [NSString stringWithFormat:@"%@/your-friendly-url-here/json",serverURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: downloadUrl]];
//POST DATA SETUP
[request setHTTPMethod:@"POST"];
[request setHTTPBody:bodyData];
//DEBUG MESSAGE
NSLog(@"Trying to call ws %@",downloadUrl);
//EXEC CALL
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(@"Download Error:%@",error.description);
}
if (data) {
//
// THIS CODE IS FOR PRINTING THE RESPONSE
//
NSString *returnString = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
NSLog(@"Response:%@",returnString);
//PARSE JSON RESPONSE
NSDictionary *json_response = [NSJSONSerialization JSONObjectWithData:data
options:0
error:NULL];
if ( json_response ) {
if ( [json_response isKindOfClass:[NSDictionary class]] ) {
// do dictionary things
for ( NSString *key in [json_response allKeys] ) {
NSLog(@"%@: %@", key, json_response[key]);
}
}
else if ( [json_response isKindOfClass:[NSArray class]] ) {
NSLog(@"%@",json_response);
}
}
else {
NSLog(@"Error serializing JSON: %@", error);
NSLog(@"RAW RESPONSE: %@",data);
NSString *returnString2 = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
NSLog(@"Response:%@",returnString2);
}
}
}];
希望这有帮助 @Ayan Khan是对的!在这里,我添加了http打印后响应的示例代码,并尽可能将其解析为JSON,它将异步处理所有内容,因此您的GUI将很好地刷新,并且不会冻结,这一点很重要
//POST DATA
NSString *theBody = [NSString stringWithFormat:@"parameter=%@",YOUR_VAR_HERE];
NSData *bodyData = [theBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//URL CONFIG
NSString *serverURL = @"https://your-website-here.com";
NSString *downloadUrl = [NSString stringWithFormat:@"%@/your-friendly-url-here/json",serverURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: downloadUrl]];
//POST DATA SETUP
[request setHTTPMethod:@"POST"];
[request setHTTPBody:bodyData];
//DEBUG MESSAGE
NSLog(@"Trying to call ws %@",downloadUrl);
//EXEC CALL
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(@"Download Error:%@",error.description);
}
if (data) {
//
// THIS CODE IS FOR PRINTING THE RESPONSE
//
NSString *returnString = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
NSLog(@"Response:%@",returnString);
//PARSE JSON RESPONSE
NSDictionary *json_response = [NSJSONSerialization JSONObjectWithData:data
options:0
error:NULL];
if ( json_response ) {
if ( [json_response isKindOfClass:[NSDictionary class]] ) {
// do dictionary things
for ( NSString *key in [json_response allKeys] ) {
NSLog(@"%@: %@", key, json_response[key]);
}
}
else if ( [json_response isKindOfClass:[NSArray class]] ) {
NSLog(@"%@",json_response);
}
}
else {
NSLog(@"Error serializing JSON: %@", error);
NSLog(@"RAW RESPONSE: %@",data);
NSString *returnString2 = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
NSLog(@"Response:%@",returnString2);
}
}
}];
希望这有帮助 您需要实现
nsurlconnectionelegate
协议方法,检测请求何时成功/完成,然后获取数据。请参阅苹果文档:谢谢,这是最简单的方法吗?阿扬·汗在下面的回答中建议的同步方法更简单,但它是同步的:如果需要任何时间,它会在请求完成时挂起你的应用程序。至少如果你是从主线程(管理UI/动画等)进行的。此外,正如他链接的博客文章所说,还有第三方库包装并隐藏了NSURLConnection
的复杂性(除其他外)。如ASIHTTPRequest
。如果你有时间和精力,如果你的应用程序足够简单,我会首先尝试学习NSURLConnection
,以便更好地掌握正在发生的事情。如果您很匆忙,需要快速解决方案,并且不想学习,请使用这些库。您需要实现nsurlconnectionelegate
协议方法,检测请求何时成功/完成,然后获取数据。请参阅苹果文档:谢谢,这是最简单的方法吗?阿扬·汗在下面的回答中建议的同步方法更简单,但它是同步的:如果需要任何时间,它会在请求完成时挂起你的应用程序。至少如果你是从主线程(管理UI/动画等)进行的。此外,正如他链接的博客文章所说,还有第三方库包装并隐藏了NSURLConnection
的复杂性(除其他外)。如ASIHTTPRequest
。如果你有时间和精力,如果你的应用程序足够简单,我会首先尝试学习NSURLConnection
,以便更好地掌握正在发生的事情。如果你很忙,需要一个快速的解决方案,并且不想学习,那么就使用图书馆。非常感谢。我真的很感激,非常感谢。我真的很感激。