【问题标题】:Post Method in Xamarin AndroidXamarin Android 中的 Post 方法
【发布时间】:2019-05-31 05:08:01
【问题描述】:

我使用 wcf 编写了一个插入服务并将其托管在 iis 中 现在我想在 xamarin android 中使用它来开发应用程序
我的一行代码有问题,其他的没问题
以下是我的代码的一部分:

_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();
            }   

我的问题在于这条线:

oTaskPostAsync.ContinueWith((oHttpResponseMessage) =>   {}

部署后我尝试打印 oHttpResponseMessage,但代码没有进入 ContinueWith。

【问题讨论】:

    标签: c# json wcf xamarin.android


    【解决方案1】:

    既然使用了HttpClient,那么就让客户端静态来避免套接字问题

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

    并将异步 API 与事件处理程序一起使用

    _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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多