简体   繁体   中英

Unable to save data Xamarin android application using web api

It is my first code in xamarin and I want to just save the data using web api but I am unable to do so after hours of hardwork. Can anybody help to identify the error.

[HttpPost]
    public HttpResponseMessage CreateEmpAttandance(string value)
    {
        if (value != "1234")
        {
            string json = @"{ data: 'Emp Code is not valid.'}";
            var jObject = JObject.Parse(json);

            var response = Request.CreateResponse(HttpStatusCode.OK);
            response.Content = new StringContent(jObject.ToString(), System.Text.Encoding.UTF8, "application/json");
            return response;

        }
        else
        {
            string json = @"{ data: 'data save sucessfully.'}";
            var jObject = JObject.Parse(json);

            var response = Request.CreateResponse(HttpStatusCode.OK);
            response.Content = new StringContent(jObject.ToString(), System.Text.Encoding.UTF8, "application/json");
            return response;


        }
    }

and my xamarin android code is

public async Task SaveTodoItemAsync(string EmpCode)
    {
        try
        {
            string url = "http://192.168.1.9/attandanceapi/api/attandance/";

            var uri = new Uri(string.Format(url));
            var json = JsonConvert.SerializeObject(EmpCode);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            HttpResponseMessage response = null;
            response = await client.PutAsync(url, content);

            var responses = response;
        }
        catch (Exception ex)
        {

            var w = ex.ToString();
        }
    }

Thanks in advance.

You are using the PutAsync, but your web api expects a POST. Have you tried the PostAsync?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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