Refit
Create response data model
public class UserData { public string name { get; set; } public int age { get; set; } public string sex { get; set; } }
Create API Interface
using System; using System.Threading.Tasks; using Refit; namespace Test.Mobile { [Headers("Cache-Control: max-age=0, Content-Type: application/json")] public interface IAPI { [Get("/getuserdata/?userid={userId}")] Task<UserData> GetUserData(string userId); } }
Call API
using Refit; async Task<bool> FetchData() { // 1. Check connectivity if (!Connectivity.IsConnected) { return false; } // 2. Declare base Url var url = "https://xxx.xx"; // 3. Call API try { IAPI api = RestService.For<IAPI>(url); var response = await api.GetUserData("1"); return true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(url); System.Diagnostics.Debug.WriteLine("GetUserData: {0}", ex); } return false; }
Leave a Reply