[英]consuming a php webservice in Xamarin PCL
I have the following webservice in php 我在php中有以下webservice
function w_getLesVisites($idVisiteur)
{
return json_encode($pdo->getLesVisiteur($idVisiteur));
}
In my Xamarin form PCL project I have the following RestService class that aims to consume the phpwebservice and retrieve the data from my MySQL local database 在我的Xamarin表单PCL项目中,我具有以下RestService类,该类旨在使用phpwebservice并从MySQL本地数据库中检索数据。
public class RestService
{
HttpClient client;
public List<Visite> L_Visites { get; private set; }
public RestService()
{
client = new HttpClient();
client.MaxResponseContentBufferSize = 25600;
}
public async Task<List<Visite>> RefreshDataAsync()
{
string restUrl = "localhost/ppe3JoJuAd/gsbAppliFraisV2/w_visite";
var uri = new Uri(string.Format(restUrl, string.Empty));
try
{
var response = await client.GetAsync(uri);
if(response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
L_Visites = JsonConvert.DeserializeObject<List<Visite>>(content);
}
}
catch (Exception ex)
{
Debug.WriteLine(@"ERROR {0}", ex.Message);
}
return L_Visites;
}
}
My question is: how can I call the php webservice with an id so that it returns a json value as expected ? 我的问题是:我怎么能用id调用php webservice,以便它按预期返回json值?
To retrieve a single item from the webservice, simply create another method as below: 要从Web服务检索单个项目,只需创建以下另一种方法:
public async Task<Visite> GetSingleDataAsync(int id)
{
//append the id to your url string
string restUrl = "localhost/ppe3JoJuAd/gsbAppliFraisV2/w_visite/" + id;
var uri = new Uri(string.Format(restUrl, string.Empty));
//create new instance of your Visite object
var data = new Visite();
try
{
var response = await client.GetAsync(uri);
if(response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
data = JsonConvert.DeserializeObject<Visite>(content); //do not use list here
}
}
catch (Exception ex)
{
Debug.WriteLine(@"ERROR {0}", ex.Message);
}
return data;
}
As suggested by @Jason, your url format may vary depend on how your service is implemented. 正如@Jason所建议的那样,您的url格式可能取决于您的服务实现方式。 But the above code will work as long as your url is correct. 但是,只要您的网址正确,上述代码就可以使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.