Http post调用缓慢的处理问题

时间:2014-03-05 13:07:18

标签: c# http windows-phone-8

我的wp8应用程序遇到了一些处理速度问题。

我的帖子中有一些问题和答案。我尝试了整个问题和ID,但它是在同一时间处理的。

if (ansNo.Visibility == Visibility.Visible || ansYes.Visibility == Visibility.Visible)
{
    if (ansNo.IsChecked == true || ansYes.IsChecked == true)
    {
        string token = Globals.token;
        int counter = Globals.counter;
        Globals.counter = Globals.counter + 1;
        quizStatus.Text = "";
        if (ansNo.IsChecked == true)
        {
            values.Add(new KeyValuePair<string, string>(quesId.Text, "f"));
        }
        else
        {
            values.Add(new KeyValuePair<string, string>(quesId.Text, "t"));
        }
        getQuiz(token, Globals.counter);
        ansYes.IsChecked = false;
        ansNo.IsChecked = false;
    }
    else
    {
        quizStatus.Text = "Please Select An Answer";
    }
}
else
{
    calculateType(values);
}   

getquiz得到下一个问题,quesId包含问题ID,calculatetype是我面临问题的函数。

以下是calculatetype

public async Task calculateType(List<KeyValuePair<string, string>> values) 
{
    //quizText.Text = values.ToString();
    quizText.Text = "Calculating Type...";
    nextButton.Visibility = Visibility.Collapsed;
    var httpClient = new HttpClient(new HttpClientHandler());
    HttpResponseMessage response = await httpClient.PostAsync("http://107.170.34.31:3000/calculateusertype", new FormUrlEncodedContent(values));
    response.EnsureSuccessStatusCode();
    var typeResponse = await response.Content.ReadAsStringAsync();
    JToken r = JToken.Parse(typeResponse);


    userResult.Visibility = Visibility.Visible;
    ansYes.Visibility = Visibility.Collapsed;
    ansNo.Visibility = Visibility.Collapsed;
    ContentPanel.Visibility = Visibility.Collapsed;
    userType.Text = r["type"].ToString();
}

这是我的getquestion函数

public async Task getQuiz(String token,int counter)
    {
        if (counter > 0)
        {
            JToken p = JToken.Parse(Globals.responseString);
            int arryCount = p["questions"].Count() - 1;
            if (counter <= arryCount)
            {
                //quizText.Text = p["questions"][counter]["_id"].ToString();
                quizText.Text = p["questions"][counter]["question"].ToString();
                quesId.Text = p["questions"][counter]["_id"].ToString();
            }
            else 
            {
                quizText.Text = "You Have Completed The Test. Click Next to Get Your Type.";
                ansYes.Visibility = Visibility.Collapsed;
                ansNo.Visibility = Visibility.Collapsed;
            }
        }
        else
        {
            var values = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("token", token)
                };

            var httpClient = new HttpClient(new HttpClientHandler());
            HttpResponseMessage response = await httpClient.PostAsync("http://107.170.34.31:3000/quiz", new FormUrlEncodedContent(values));
            response.EnsureSuccessStatusCode();
            Globals.responseString = await response.Content.ReadAsStringAsync();

            JToken o = JToken.Parse(Globals.responseString);
            Globals.quizcount = o["questions"].Count();
            Globals.answers = new string[Globals.quizcount];
            //o = JObject.Parse(responseString);
            if (Globals.responseString != "")
            {

                //quizText.Text = o["questions"][counter]["_id"].ToString();
                quizText.Text = o["questions"][counter]["question"].ToString();
                quesId.Text = o["questions"][counter]["_id"].ToString();
            }
        }
    }

有什么建议可以加快速度吗?

1 个答案:

答案 0 :(得分:0)

我建议您先进行一些分析,以确保解决问题。

尽管如此,它很可能是按需点击Web服务的结果。您的Web服务API应该“厚实,不健谈”。换句话说,尝试在用户开始测验时加载所有问题(和答案)。