Environment:
VS2012 Update 4
Windows Phone 8 SDK
a fresh new Windows Phone project based on WP OS 7.1
NuGet pack1: MS Async
NuGet pack2: MS HttpClient
What does the project do
a single test for a forum API:
to post a new thread in the forum and refresh the JSON to check if the "count" is accumulate.
What is the UI mean
1st textblock:
the response json will display here
3 buttons:
[refresh JSON]: use API1 to refresh the threads list
[New Post]: use API2 to post a new thread in the forum, title and text will display below
[IE]: open json in IE use API1
Thread information:
the thread you've just post use [New Post] button
What does the JSON mean
only need to focus 2 parts
count: 244 ---> total threads count in the forum
"57923" ---> the title of the 1st thread (the field "data" is a array of the threads list)
What is the test procedure
1. open the app
2. click [refresh JSON] to get the list
3. remember the "count" part and the "title" of the 1st thread
4. click [New Post] to post a new thread, the title of the thread will display below
5. click [refresh JSON] to see the latest threads list
6. expected: the "count" accumulate by 1 and the "title" part should be the same as below
Problem:
1. after clicking the [New Post], No matter how many times you hit [refresh JSON], the count will not accumulate!
2. but open the url in Chrome(Desktop), you will find that the JSON has already been updated!
3. close the app and reopen it, hit [refresh JSON], the JSON will updated as expected, but when you post a new thread, the problem occurs again
Why and How to solve it?
The code
private async void RefreshJSONButton_Click(object sender, RoutedEventArgs e)
{
this.JsonView.Text = string.Empty;
HttpClient client = new HttpClient();
string url =
"API1 Address Here";
string x = await client.GetStringAsync(url);
this.JsonView.Text = x;
client.Dispose();
}
private async void NewPostButton_Click_1(object sender, RoutedEventArgs e)
{
Random rnd = new Random();
this.num = rnd.Next(1, 99999);
// generate the title and content with random number
string threadTitle = this.num.ToString();
string threadContent = "111" + num.ToString();
// generate the url
string url =
"API2 Address Here";
url = url + "&title=" + threadTitle + "&content=" + threadContent;
// use HttpClient to send the new thread
HttpClient client = new HttpClient();
string x = await client.GetStringAsync(url);
client.Dispose();
// display the thread informartion for further check
this.titleView.Text = threadTitle;
this.contentView.Text = threadContent;
}
It seems, you are hitting the same URL on every refresh click. Sometime Windows Phone stores the request and response in cache.
If possible, you add one more parameter to your request and on every click just change the value of this parameter. You can use GUID or Unix Timestamp.
You can try like: