在Xamarin PCL中使用php webservice [英] consuming a php webservice in Xamarin PCL

查看:135
本文介绍了在Xamarin PCL中使用php webservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php中具有以下网络服务

I have the following webservice in php

function w_getLesVisites($idVisiteur)
{
    return json_encode($pdo->getLesVisiteur($idVisiteur));
}

在Xamarin形式的PCL项目中,我具有以下RestService类,该类旨在使用phpwebservice并从我的MySQL本地数据库中检索数据

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

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值?

My question is: how can I call the php webservice with an id so that it returns a json value as expected ?

推荐答案

要从Web服务检索单个项目,只需创建以下另一种方法:

To retrieve a single item from the webservice, simply create another method as below:

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格式可能会有所不同,具体取决于您实现服务的方式.但是,只要您的网址正确,上面的代码就可以正常工作.

As suggested by @Jason, your url format may vary depend on how your service is implemented. But the above code will work as long as your url is correct.

这篇关于在Xamarin PCL中使用php webservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆