Suggestion for getting data from a web server?

倖福魔咒の 提交于 2020-01-05 05:30:08

问题


I have a web server which contains a list of "interesting locations". These are "hard coded" on the web server, and maintained from an administrator on the server-side. Users can't add or remove anything.

From within my app, I want to make a button "fetch locations", and the app should then contact the web server and ask for that "interesting locations" list.

I'm using Core Data and have an InterestingLocation entity in my model. The goal is to get the data from the web server and persist it on the device with Core Data, so that it is accessible even without internet connection.

My idea was to use XML on the server side and output the InterestingLocation "objects" in an XML file. There are about 100 of them only, so not really huge.

On the client side (device), maybe XML-RPC?

Would be happy about some suggestions and further information where/how to get started :)


回答1:


My advice is keep it simple. On the server side, use RESTful principles to make the interface as simple as possible.

Then, all you'll need to do in your iPhone app is use an NSURLConnection to fetch the URL. NSURLConnection is a very nice, asynchronous way of downloading files from remote locations.

After that, it's a simple matter of parsing the XML or JSON and creating the appropriate Core Data objects. I generally like to do import operations like this on a separate managed object context in a separate thread. When you save the managed object context, use the NSManagedObjectContextDidSaveNotification to merge in the changes with your main managed object context.




回答2:


What kind of server do you have? If the server is java based I'd recommend looking at HessianKit by Fredrik Olsson. Encode/Decode to ordinary Objective-C types and put in NSArrays and NSDictionaries will make the experience smoother.



来源:https://stackoverflow.com/questions/2096299/suggestion-for-getting-data-from-a-web-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!