ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – client.GetAsync不刷新我的数据

2019-07-02 15:52:52  阅读:341  来源: 互联网

标签:c dotnet-httpclient windows-phone-8-1 win-universal-app getasync


我使用MVVM-Light开发了au Universal App.

在页面上,有一个来自WebService的注释列表.如果当前用户是评论的作者,我会显示一个FlyoutMenu,允许他“编辑”或“删除”其评论.还有一个用于添加新评论的AppBarButton:

enter image description here

我的问题是,在第一次加载此页面后,注释永远不会刷新…

我在ViewModel中使用了一个“LoadComments()”方法,它允许我在到达页面时获取注释,但也可以在编辑,删除或添加项目后获取注释:

private async void LoadComments()
{
    List<Comment> commentsWS = await WebServiceGetCommentList();
    if (commentsWS != null)
        Comments = new ObservableCollection<Commentaire>(commentsWS);
}

此方法调用另一个方法“WebServiceGetCommentList()”,该方法在同一ViewModel中准备对WebService的调用:

private async Task<List<Comment>> WebServiceGetCommentList()
{
    // Parameters
    List<KeyValuePair<string, string>> parametres = new List<KeyValuePair<string, string>>();
    parametres.Add(new KeyValuePair<string, string>("option", _currentUserAccount.option));
    parametres.Add(new KeyValuePair<string, string>("id_article", Article.id_article.ToString()));

    // Call WebService and deserialize
    Exception custEx = null;
    try
    {
        List<Comment> comments = await WebServices.GetCommentList(_currentUserAccount.url, parametres, "");
        return comments;
    }
    // Exceptions 
    catch (Exception e)
    {
        ...

    }
    return null;
}

然后我进入“WebServices”类的“GetComments()”方法:

public static async Task<List<Comment>> GetCommentList(String url, List<KeyValuePair<String, String>> parametres, String protocol)
{
    // Call WebService and deserialize
    var response = await JSONParser.getJSONFromUrl(url, parametres, "");
    List<Comment> comments = new List<Comment>();
    WsResponse wsResponse = ManageWsReponse(response, Constants.WebServiceTask.GetCommentList.Value);
    try
    {
        WsResponseResult wsResponseResult = JsonConvert.DeserializeObject<WsResponseResult>(wsResponse.data.ToString());
        comments = JsonConvert.DeserializeObject<List<Comment>>(wsResponseResult.result.ToString());
        return comments;
    }
    catch (Exception e)
    {
        throw new DeserializeException("Deserialize exception", e, DateTime.Now, "Comment");
    }
}

此方法调用“JSONParser”类中的“getJSONFromUrl()”方法,该类启动“client.GetAsync()”:

public static async Task<string> getJSONFromUrl(String url, List<KeyValuePair<String, String>> parameters, String protocol)
{
    var client = new HttpClient();

    // Preparing URI
    string sParameters = null;
    int i = 1;
    foreach (var param in parameters)
    {
        sParameters += param.Key + "=" + param.Value;
        sParameters += i != parameters.Count ? "&" : string.Empty;
        i++;
    }
    var uri = new Uri(url + "?" + sParameters);

    // Calls the WebService
    var response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);

    // Code and results
    var statusCode = response.StatusCode;
    // EnsureSuccessStatusCode throws exception if not HTTP 200
    response.EnsureSuccessStatusCode();
    // responseText
    var responseText = await response.Content.ReadAsStringAsync();
    return responseText;
}

我可以成功地添加,删除或编辑注释,但是当我回到此方法“LoadComments()”时,不会考虑更改,并且我得到的列表与第一次调用时相同…

我还在“GetJSONFromURL()”方法中放置了断点,我没有在响应var中看到添加,删除或编辑的注释.

同时,如果我在浏览器中复制URI,为了使用相同的参数调用相同的WebService,则会考虑更改.

=&GT我认为在client.GetAsync()上有一个缓存,但是我没有看到如何去除它,或者强制它刷新数据……

我尝试了这个对我不起作用的解决方案httpclient-caching.

我认为当我有缓存管理原因时

解决方法:

这是平台缓存.我没有使用Cache-Control标头取得任何成功,解决问题的最可靠方法是确保请求不同.

添加其他参数 – 时间戳.由于请求不同,平台无法使用缓存的响应.

parametres.Add(new KeyValuePair<string, string>("mytmstmp", DateTime.Now.Ticks);

或者:使用允许日期的附加标题.我用过:

"If-Modified-Since", DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)

标签:c,dotnet-httpclient,windows-phone-8-1,win-universal-app,getasync
来源: https://codeday.me/bug/20190702/1357768.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有