gpt4 book ai didi

ios - 如何在一个 UIViewController 中维护两个 api?

转载 作者:行者123 更新时间:2023-11-29 01:19:44 25 4
gpt4 key购买 nike

我在一个 UIImage 中有一个 ViewController,在我想打印数据的两个 UITextField 中有一个 UITableView。这些数据来自 api。

从第一个 api 开始,我想显示 UIImage 和 UITextField 的数据。从第二个 Api 将数据加载到 UITableView 中。怎么做到的?

到目前为止,我已经尝试过了

-(void)viewDidLoad
{
[super viewDidLoad];
NSString *urlString = [NSString stringWithFormat:@"API"];
NSString *jsonString = @"";
NSData *myJSONData =[jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:myJSONData]];
[request setHTTPBody:body];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
if(str.length > 0){
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]);
mainBannerArray =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
}
else{
NSLog(@"Error");
}
}

这是我的连接委托(delegate)方法。

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
myDataQA = [[NSMutableData alloc]init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[myDataQA appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if ((NSNull *)myDataQA != [NSNull null])
{
if(myDataQA.length > 0)
{
mainBannerArray =[NSJSONSerialization JSONObjectWithData:myDataQA options:NSJSONReadingAllowFragments error:nil];
}
}

我的屏幕是这样的

enter image description here

最佳答案

使用来自 Github 的 AFNetworking 3.0。并在第一个 API 的成功 block 中调用第二个 API,如下所示,

NSString *urlString = [NSString stringWithFormat:@"API"]
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// this for Image and Textfield
[manager GET:urlString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
// Load image and text field

// And then call sencod url
AFHTTPSessionManager *manager2 = [AFHTTPSessionManager manager];
[manager2 GET:link2 parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
// reload the tableview
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

关于ios - 如何在一个 UIViewController 中维护两个 api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34760725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com