How to Consume Web Api in xamarin CrossPlatform Application

后端 未结 1 1173
醉梦人生
醉梦人生 2021-02-04 16:39

I have created web api that retrieves data from SQL database. I need to consume web api in xamrin for android and xamarin for iOS. as of now xamarin for android. I am not sure h

相关标签:
1条回答
  • 2021-02-04 17:19

    Here is how I would use this service from just about any project (including Xamarin projects).

    First, get rid of the line in your Global.asax file that says this:

    GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
    

    Generally speaking this is bad practice. You want to have at least the XML and JSON formatters available because in a restful environment it is up to the consumers to determine what type of serialization they need/want.

    Next, I would use the System.Net.Http.HttpClient class to interact with this service.

    Interacting with the Web Api service from this point is fairly straightforward. Here are some examples.

    public async List<Dept> GetDepts()
    {
         using(var client = new HttpClient())
         {
              client.BaseAddress = new Uri("http://<ipAddress:port>/");
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
              return await client.GetAsync("/api/dept");
         }
    }
    
    public async Task<Dept> GetDept(int id)
    {
         using(var client = new HttpClient())
         {
              client.BaseAddress = new Uri("http://<ipAddress:port>/");
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
              var result = await client.GetAsync(string.format("/api/dept/{0}", id));
    
              return JsonConvert.DeserializeObject<Dept>(await result.Content.ReadAsStringAsync());
         }
    }
    
    public async Task AddDept(Dept dept)
    {
         using(var client = new HttpClient())
         {
              client.BaseAddress = new Uri("http://<ipAddress:port>/");
    
              await client.PostAsJsonAsync("/api/dept", dept);
         }
    }
    
    public async Task UpdateDept(Dept dept)
    {
         using(var client = new HttpClient())
         {
              client.BaseAddress = new Uri("http://<ipAddress:port>/");
    
              await client.PutAsJsonAsync(string.format("/api/dept/{0}", dept.no), dept);
         }
    }
    
    public async Task DeleteDept(int id)
    {
         using(var client = new HttpClient())
         {
              client.BaseAddress = new Uri("http://<ipAddress:port>/");
    
              client.DeleteAsync(string.format("/api/dept/{0}", id));
         }
    }
    

    That should get you started in the right direction anyway. This could definitely get cleaned up and I wrote it freehand so there may be some typos.

    Update

    Update your MainActivity.cs

    Instead of int id = Convert.ToInt32(txtValue1)

    Use

    int id = Convert.ToInt32(txtValue1.Text);
    

    Instead of GetButton.Click += GetDept(id);

    Use

    GetButton.Click += async (sender, args) => {
       var dept = await GetDept(id);
    
       No.Text = dept.no;
       Name.Text = dept.name
    }
    
    0 讨论(0)
提交回复
热议问题