如何在Label中显示GET请求(How to show GET request in Label)

编程入门 行业动态 更新时间:2024-10-27 23:19:08
如何在Label中显示GET请求(How to show GET request in Label)

我的get请求仅在命令行NSLog中有效。 我需要在Label中显示数据,但它不起作用。

-(void)getRequest{ NSURLSessionConfiguration *getConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *getSession = [NSURLSession sessionWithConfiguration: getConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]]; NSURL * getUrl = [NSURL URLWithString:@"http://localhost:3000/get"]; NSURLSessionDataTask * getDataTask = [getSession dataTaskWithURL:getUrl completionHandler:^(NSData *getData, NSURLResponse *getResponse, NSError *getError) { if(getError == nil){ NSString * getString = [[NSString alloc] initWithData: getData encoding: NSUTF8StringEncoding]; [self.label setText:getString];// doesn't work! NSLog(@"Data = %@",getString);}// it works!! MainViewController*l=[[MainViewController alloc]init]; [l getRequest]; } ]; [getDataTask resume]; }

My get request works only in command line NSLog. I need to show a data in Label, but it doesn't works.

-(void)getRequest{ NSURLSessionConfiguration *getConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *getSession = [NSURLSession sessionWithConfiguration: getConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]]; NSURL * getUrl = [NSURL URLWithString:@"http://localhost:3000/get"]; NSURLSessionDataTask * getDataTask = [getSession dataTaskWithURL:getUrl completionHandler:^(NSData *getData, NSURLResponse *getResponse, NSError *getError) { if(getError == nil){ NSString * getString = [[NSString alloc] initWithData: getData encoding: NSUTF8StringEncoding]; [self.label setText:getString];// doesn't work! NSLog(@"Data = %@",getString);}// it works!! MainViewController*l=[[MainViewController alloc]init]; [l getRequest]; } ]; [getDataTask resume]; }

最满意答案

dataTaskWithURL不在主线程上工作,这是更新UI所必需的。

if (getError == nil) { NSString * getString = [[NSString alloc] initWithData: getData encoding: NSUTF8StringEncoding]; dispatch_async(dispatch_get_main_queue(), ^{ [self.label setText: getString]; NSLog(@"Data = %@", getString); }); }

此代码适用于您。

您还可以使用:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self.label setText:getString]; }];

真的更多这里为什么我应该选择GCD而不是NSOperation并阻止高级应用?

dataTaskWithURL is not working on the main-thread and that's necessary to update your UI.

if (getError == nil) { NSString * getString = [[NSString alloc] initWithData: getData encoding: NSUTF8StringEncoding]; dispatch_async(dispatch_get_main_queue(), ^{ [self.label setText: getString]; NSLog(@"Data = %@", getString); }); }

This code will work fine for you.

You can also use:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self.label setText:getString]; }];

Real more here Why should I choose GCD over NSOperation and blocks for high-level applications?

更多推荐

本文发布于:2023-08-06 14:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451134.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Label   request   show

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!