我在这里问一般问题。我真的不知道该在这里做什么。我开发了一个带有后端的Windows Phone 8移动应用程序作为Web API服务的Web服务。它有大约4到5个屏幕。windows phone 8(与web api连接)问题
问题: 当我第一次加载我的应用程序,让一切负载(未由应用程序栏要到第二个窗口或第三窗口或第四窗口和启动另一个开断从的WebAPI操作取提取记录)。它工作正常。
但如果我曾经让第一次加载运行,并去第二次获取记录。它会产生巨大的问题。 (延迟,返回null等)。任何想法如何在第一次抓取正在运行时用第二次抓取来克服这个问题。这是一个普遍的问题?或者只有我有这个问题?
这是我的代码,我使用
private static readonly HttpClient client;
public static Uri ServerBaseUri
{
get { return new Uri("http://169.254.80.80:30134/api/"); }
}
static PhoneClient()
{
client =new HttpClient();
client.MaxResponseContentBufferSize = 256000;
client.Timeout = TimeSpan.FromSeconds(100);
client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
}
public async static Task<List<Categories>> GetDefaultCategories()
{
HttpResponseMessage getresponse = await client.GetAsync(ServerBaseUri + "Categorys");
string json = await getresponse.Content.ReadAsStringAsync();
json = json.Replace("<br>", Environment.NewLine);
var categories = JsonConvert.DeserializeObject<List<Categories>>(json);
return categories.ToList();
}
你必须使用的HttpClient的新实例,为每个请求。在你的例子中一切都是静态的。这就是为什么你有两个或更多的请求意想不到的结果。 –