2

有下面這段代碼在PCLMVVMCross PCL REST調用工作在Android中沒有的Windows Phone

class CallSAPService : ICallSAPService 
    { 
     public string MakeSAPRestCall(string url, string jsondata, string username, string password) 
     { 
      string retmessage = ""; 
      try 
      { 
       NetworkCredential cr = new NetworkCredential(); 
       cr.UserName = username; 
       cr.Password = password; 
       HttpClientHandler handler = new HttpClientHandler(); 
       handler.Credentials = cr; 
       HttpClient httpClient = new HttpClient(handler); 
       Uri Auri = new Uri(url); 
       StringContent content = new System.Net.Http.StringContent(jsondata.ToString(), Encoding.UTF8, "application/json"); 
       var result = httpClient.PostAsync(Auri, content).Result; 
       Task<string> ff = result.Content.ReadAsStringAsync(); 
       retmessage = ff.Result; 

       //StringContent content2 = new System.Net.Http.StringContent(jsondata.ToString(), Encoding.UTF8, "application/json"); 
       //var httpClient2 = new HttpClient(new HttpClientHandler()); 
       //Task<HttpResponseMessage> response = httpClient2.PostAsync(url, content2); 
       //response.Result.EnsureSuccessStatusCode(); 
       //var res = response.Result; 

       // retmessage = res.Content.ReadAsStringAsync().ToString(); 
      }  
      catch (Exception Ex) 
      { 
       retmessage = Ex.Message; 
      } 

      return retmessage; 
     } 
    } 
} 

這部作品在Andorid的但不是在Windows Phone的項目。在Windows Phone的項目行: Task<string> ff = result.Content.ReadAsStringAsync();不出來執行

回答

0

我敢肯定,這條線是你的問題:

retmessage = ff.Result; 

你不能在UI線程上執行同步I/O Windows Phone。使用異步並等待而不是Task.Result。

相關問題