【发布时间】:2015-03-02 06:34:02
【问题描述】:
我目前正在使用以下代码从我的 iOS 应用程序中的静态 JSON 文件中提取数据:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
navigationController.delegate = self;
NSString* jsonFile = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/app.json"];
NSString* jsonString = [NSString stringWithContentsOfFile:jsonFile encoding:NSUTF8StringEncoding error:nil];
@try {
RootViewController* rootViewController = (RootViewController*)[navigationController.viewControllers objectAtIndex:0];
rootViewController.feed = [[jsonString JSONValue] objectForKey:@"thefeed"];
[self.window addSubview:navigationController.view];
}
@catch (NSException * e) { }
[self.window makeKeyAndVisible];
return YES;
}
它就像我希望它用于静态数据一样工作,但我需要它通过实时 url 动态工作。我正在使用 Wordpress REST API 来踢出我想要的 JSON:http://milknsugar.com/wp-json/posts?filter[posts_per_page]=50&filter[order]=DESC
我尝试了以下几种变体,但运气不佳:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
navigationController.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:MILKNSUGAR_URL]];
NSURLConnection* Connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (Connection)
self.feed = [NSMutableData data];
[self.window addSubview:loadingView];
[self.window makeKeyAndVisible];
return YES;
}
有什么建议或建议吗?谢谢!
【问题讨论】:
-
你实现了 NSURLConnection 的委托方法了吗?
-
@jailani 是的,我有。
-
如果您想以一种简单的方式在您的应用程序中获取您的帖子,并且您可以访问对象,那么请查看此示例:github.com/evermeer/AlamofireJsonToObjects/blob/master/…
标签: ios json wordpress rest feed