gpt4 book ai didi

ios - 从 xcode 获取 json

转载 作者:行者123 更新时间:2023-11-29 04:33:11 24 4
gpt4 key购买 nike

我正在从 xcode 4 调用用 .net 编写的 Web 服务。输出是一个 json 字符串。当使用静态 IP 地址使用 url 进行调用时,它会给出错误。HTTP 错误 404.0 - 未找到。但在调用相同的 url 时有效在 localhost 中,代码如下

- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url=[NSURLURLWithString:@"http:// static ip address where serviceis located(169.254.216.8)/restGroceryService/WebService/insertList?prods=apples,bananas,milk"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *pageSource = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"page source%@",pageSource);
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSArray *array = [json objectForKey:@"result"];
NSDictionary *store = [array objectAtIndex:0];
NSNumber *storeId = [store objectForKey:@"storeId"];
NSNumber *total = [store objectForKey:@"TOTAL"];
self.priceTextField.text =[total stringValue];
NSString *storeName = [store objectForKey:@"storeName"];
self.storNameTextField.text = storeName;
}

最佳答案

ithink the error was here try now:


NSURL *url=[NSURL URLWithString:@"your address"];/<------ here was the error
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *pageSource = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"page source%@",pageSource);
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSArray *array = [json objectForKey:@"result"];
NSDictionary *store = [array objectAtIndex:0];
NSNumber *storeId = [store objectForKey:@"storeId"];
NSNumber *total = [store objectForKey:@"TOTAL"];
self.priceTextField.text =[total stringValue];
NSString *storeName = [store objectForKey:@"storeName"];
self.storNameTextField.text = storeName;

关于ios - 从 xcode 获取 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11420682/

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