2017-04-12 23 views
0

我一直在寻找遍布xamarin网站,并且有几十种方式来调用Web服务。到现在为止,每次我尝试重复教程中的示例时,都会出现问题。那么,怎样才能我只是调用PHP Web服务,它返回我JSON,我可以工作吗?如何在Xamarin.PCL项目中调用Web服务

这里是我做过什么:

private async Task<JsonValue> Connexion_Webservice(string url) 
     { 
      // Creates the HTTP Request 
      HttpWebRequest requete = (HttpWebRequest)HttpWebRequest.Create(new Uri(url)); 
      requete.ContentType = "application/json"; 
      requete.Method = "GET"; 


      //Sends the request and wait for the response 
      using (WebResponse response = await requete.BeginGetResponse() 
      { 
      // until here I dont know what to do, what should I do ? ; 
      } 
     } 

回答

0

这里是sample

using (WebResponse response = await request.GetResponseAsync()) 
{ 
    // Get a stream representation of the HTTP web response: 
    using (Stream stream = response.GetResponseStream()) 
    { 
     // Use this stream to build a JSON document object: 
     JsonValue jsonDoc = await Task.Run (() => JsonObject.Load (stream)); 
     Console.Out.WriteLine("Response: {0}", jsonDoc.ToString()); 

     // Return the JSON document: 
     return jsonDoc; 
    } 
} 

而且WebClient or httpClient作品!

using (var webClient = new System.Net.WebClient()) { 
var json = webClient.DownloadString(URL); } 
相关问题