[英]consuming a php webservice in Xamarin PCL
我在php中有以下webservice
function w_getLesVisites($idVisiteur)
{
return json_encode($pdo->getLesVisiteur($idVisiteur));
}
在我的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;
}
}
我的問題是:我怎么能用id調用php webservice,以便它按預期返回json值?
要從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;
}
正如@Jason所建議的那樣,您的url格式可能取決於您的服務實現方式。 但是,只要您的網址正確,上述代碼就可以使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.