How to deal a multiple page REST reply
I have a WP7 silverlight (technology may or may not be relevant) application that calls multiple (10 or more) REST web services for similar data and them puts them all together in a single collection. The data itself is not relevant to the question.
How do I deal with REST services when the reply has more than one page. What I mean is, all of these services I call reply with data, but at the top it says "Page 1 of 11". So I have to call it again and again with something like ...&page=1...&page=2... I've found that writing a custom framework for this is a pain and can be a little fragile. Basicall开发者_JS百科y I have a thread that goes out to all of the services initially, then finds out how many pages there are. From there I'm not sure how to proceed. I don't know the best way to spawn the threads from there.
Is there a best practice for this or maybe even a frame work I can follow?
You should use simple requests - on wp7 they are async so you have to do something like:
- fetch the number of pages, store it in TOTAL_NUM, set NUM = 1
- call request to fetch data on PAGE == NUM
- when req is back, parse it, NUM++ if NUM != TOTAL_NUM then goto pt. 2
Something like this:
public void FetchData(int pageNum)
{
Uri address = ...;
var request = WebRequest.Create(address);
// ... init request
request.BeginGetRequestStream(asyncResp => {
var response = (WebResponse) request.EndGetResponse(asyncRes);
ParseAndSave(response, pageNum);
NUM++;
if (NUM != TOTAL_NUM)
FetchData(NUM);
}, null);
}
keep in mind that I didn't compile it or anything... it may be not the best way to do it, but should work with minimum effort :)
精彩评论