1
votes

I have visual studio 2017 with following Xamarin version in system.

Microsoft Visual Studio Professional 2017 Version 15.6.1 VisualStudio.15.Release/15.6.1+27428.2002 Microsoft .NET Framework Version 4.7.02556

Xamarin 4.9.0.749 (9b0fce36d) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 4.10.58 (cee1369d0) Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin.Android SDK 8.2.0.15 (HEAD/22d97e153) Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK 11.8.0.20 (1c6f300) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. And i have create simple Xamarin android with some .net class library add as reference.

I have try to post some data using xamarin application, but always get null value in webapi.

Below WebApi code in c#

 public class DataPostController : ApiController
    {

        [HttpPost]
        public string Post(CSVItem data)
        {

            // CSVData data = JsonConvert.DeserializeObject<CSVData>(json);
            try
            {
                string error = string.Empty;
                if (data != null)
                {


                    error += ImportService.clsGlobal.AddItem(data.SerialNumber, data);

                }
                error = string.IsNullOrEmpty(error) ? "-1" : error;
                return error;
            }
            catch (Exception ex)
            {

                return ex.Message;
            }
        }

        public string Get()
        {
            return "Hello";
        }
    }

Calling post method using xamarin app

   CSVItem item = GetItems();
    var url = string.Format("{0}/{1}?", "http://myurl.com", "api/DataPost");
                            string sContentType = "application/json"; // or application/xml

                            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "cHRlc3Q5OnB1bXB0ZXN0OUAxMjM0NTY=");
                            var oTaskPostAsync = client.PostAsync(url, new StringContent(item.ToString(), Encoding.UTF8, sContentType));
                            var res = oTaskPostAsync.ContinueWith((oHttpResponseMessage) =>
                           {
                               Task<string> r1 = oHttpResponseMessage.Result.Content.ReadAsStringAsync();
                               var strResult = r1.Result;

                               // response of post here
                               Pt9Log.WriteLog("HistoryDownload PostCSV end. StatusCode" + oTaskPostAsync.Result.StatusCode.ToString());
                               Pt9Log.WriteLog("HistoryDownload PostCSV end. Result===" + oTaskPostAsync.Result.ToString());

                               Pt9Log.WriteLog("HistoryDownload PostCSV end. Post Response===" + strResult);

                               if (oTaskPostAsync.Result.StatusCode == System.Net.HttpStatusCode.OK)
                                   result = true;
                               else
                                   result = false;

                           });

Model class used same as both side with in application & same as in webApi.

 public class CSVItem
    {
        public string SerialNumber { get; set; }
        public double Input1 { get; set; }
        public double Input2 { get; set; }
        public double Input3 { get; set; }
        public double Input4 { get; set; }
        public double Input5 { get; set; }
        public double Input6 { get; set; }
        public string Input7 { get; set; }
        public double Input8 { get; set; }
    }
1
with a name that includes "CSV" are you sure that the data being sent is properly formatted JSON?Nkosi
your server requires a json object of CSVItem with a property SerialNumber, but in your client you are sending string content, either change your server to receive plain string, or change your client to send json object { "SerialNumber" : "data from client" }Ben Croughs
Both side CSVItem class object are same, when i send using simple .net application everything working fine but when i'm going to send with xamarin app it always send null into CSVItem object. webapi always receive null.user619
Nkosi check in full code i have post simple class object & both side having same class.user619

1 Answers

1
votes

You should use JsonConvert.SerializeObject() convert item to JSON, instead of using item.ToString().
For example:

var oTaskPostAsync = client.PostAsync(url, new StringContent(JsonConvert.SerializeObject(item), Encoding.UTF8, sContentType));

You could refer to Consuming a RESTful Web Service for more information.