简体   繁体   中英

Post Method in Xamarin Android

I coded an insert service with wcf and hosted it in iis Now I want to use it in xamarin android to develop an app
I have problem with a line of my code, other things in ok
The below is apart of My Code:

_btnAdd.Click += delegate
        {
            string sUrl = "http://192.168.43.31/Service1.svc/insertIntoTableAdvertise";
            string sContentType = "application/json";

            JObject oJsonObject = new JObject();
            oJsonObject.Add("TxtGroup", _edtAdvGrouping.Text);
            oJsonObject.Add("TxtTitle", _edtTitle.Text);
            oJsonObject.Add("TxtDate", "0");
            oJsonObject.Add("TxtLocation","شاهین");
            oJsonObject.Add("TxtNumber", _edtNumber.Text);
            oJsonObject.Add("TxtPrice", _edtPrice.Text);
            oJsonObject.Add("TxtExpression", _edtExpression.Text);

            HttpClient oHttpClient = new HttpClient();
            var oTaskPostAsync = oHttpClient.PostAsync(sUrl, new StringContent(oJsonObject.ToString(), Encoding.UTF8, sContentType));
            try
            {
                oTaskPostAsync.ContinueWith((oHttpResponseMessage) =>
                {
                    // response of post here
                    //Debug ("webserviceresponse:>" + oHttpResponseMessage);
                    Toast.MakeText(this, oHttpResponseMessage.ToString(), ToastLength.Short);
                });
                oTaskPostAsync.Wait();
            }
            catch (Exception ex)
            {

                Toast.MakeText(this, ex.Message.ToString(), ToastLength.Short).Show();
            }   

My Problem is With this Line :

oTaskPostAsync.ContinueWith((oHttpResponseMessage) =>   {}

After deploying i try to print oHttpResponseMessage, But code didn't enter inside ContinueWith.

Since using HttpClient then make the client static to avoid socket issues

//this should be a field in the class.
static HttpClient httpClient = new HttpClient();

//...

and use the async API with the event handler

_btnAdd.Click += async (sender, args) => {
    string sUrl = "http://192.168.43.31/Service1.svc/insertIntoTableAdvertise";
    string sContentType = "application/json";

    JObject oJsonObject = new JObject();
    oJsonObject.Add("TxtGroup", _edtAdvGrouping.Text);
    oJsonObject.Add("TxtTitle", _edtTitle.Text);
    oJsonObject.Add("TxtDate", "0");
    oJsonObject.Add("TxtLocation","شاهین");
    oJsonObject.Add("TxtNumber", _edtNumber.Text);
    oJsonObject.Add("TxtPrice", _edtPrice.Text);
    oJsonObject.Add("TxtExpression", _edtExpression.Text);

    try {
        var content = new StringContent(oJsonObject.ToString(), Encoding.UTF8, sContentType);

        var response = await httpClient.PostAsync(sUrl, content);

        var responseContent = await response.Content.ReadAsStringAsync();

        Toast.MakeText(this, responseContent, ToastLength.Short).Show();

    } catch (Exception ex) {
        Toast.MakeText(this, ex.Message.ToString(), ToastLength.Short).Show();
    }
}

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