简体   繁体   English

从Xamarin形式的Rest API检索数据

[英]retrieving data from rest api in xamarin forms

I created a web service with wcf that get two string parameters and give an string value, this web service hosted in IIS and it work correctly, this is a part of my code and it gives json format: 我使用wcf创建了一个Web服务,该服务获得两个字符串参数并给出一个字符串值,该Web服务托管在IIS中,并且可以正常工作,这是我的代码的一部分,并且提供json格式:

[OperationContract]
    [WebInvoke(UriTemplate = "AuthUserPass", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    string AuthUserPass(string UserName);

Now i want to retrieve the data from this service in xamarin forms but i don't know how to do it: my code for this is: 现在,我想以Xamarin形式从此服务中检索数据,但是我不知道该怎么做:我的代码是:

    public async Task<int> AuthUserPassAsync(string username,string password)
    {
        int sessionResponseJson = 1000;
        var uri = new Uri(string.Format(Constants.AuthUrl, string.Empty));

        Users userJson = new Users();
        userJson.UserName = username;
        userJson.Password = password;

        var json = JsonConvert.SerializeObject(userJson);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        var response = await client.PostAsync("login", content);

        var result = response.Content.ReadAsStringAsync().Result;
        if (result != "")
        {
            sessionResponseJson = JsonConvert.DeserializeObject<int>(result);
        }
        return sessionResponseJson;

} }

From your web service method I can see, it requires only one string parameter that is 'UserName' 从您的Web服务方法中,我可以看到,它仅需要一个字符串参数“ UserName”

    string AuthUserPass(string UserName);

And when calling the web service method, you are passing an object with two fields 'UserName' and 'Password' 当调用Web服务方法时,您正在传递带有两个字段“ UserName”和“ Password”的对象

    Users userJson = new Users();
    userJson.UserName = username;
    userJson.Password = password;

which you can understand is not correct. 您可以理解的是不正确的。 I am sure fixing it will resolve your problem. 我敢肯定,解决它可以解决您的问题。 And to fix you can change your web service method parameter type as type with fields as UserName and Password. 为了解决此问题,您可以将您的Web服务方法参数类型更改为用户名和密码字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM