通过回收站视图的发布请求发送数据(Xamarin C#)

时间:2016-12-13 18:35:44

标签: c# android visual-studio xamarin xamarin.android

我使用Xamarin为Android编写应用程序。

我有一些文本视图和按钮的回收站视图。

我需要从其中一个文本视图中获取数据并通过邮件请求发送。

以下是我在Adapter中的代码:

 public class FormAdapter : RecyclerView.Adapter
{
    public event EventHandler<int> ItemClick;

    private readonly List<Result> form;

    public FormAdapter(List<Result> form_)
    {
        this.form = form_;
    }

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        var movieViewHolder = (FormViewHolder)holder;
        movieViewHolder.OrderId.Text = "Id заказа: " + form[position].id;
        movieViewHolder.MovieNameTextView.Text = form[position].date_created;
        movieViewHolder.DirectedByTextView.Text = "Имя: " + form[position].billing.first_name;
        movieViewHolder.ReleasedOnTextView.Text = "Телефон: " + form[position].billing.phone;
        movieViewHolder.Total.Text = " Сумма: " + form[position].total;
        movieViewHolder.Street.Text = "Улица/Дом: " + form[position].billing.address_1;
        movieViewHolder.House.Text = "Квартира: " + form[position].billing.address_2;
        movieViewHolder.Porch.Text = "Подъезд: " + form[position].billing.company;
        movieViewHolder.Floor.Text = "Этаж: " + form[position].billing.state;
        movieViewHolder.Order.Text = "Состав заказа:" + form[position].line_items[0].name;
        movieViewHolder.Done.Click += async delegate
        {
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(@"http://api.simplegames.com.ua/");
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("utf-8"));
                string endpoint = @"post/changeStatusToCompleted.php";

                try
                {
                    HttpContent content = new StringContent(form[position].id.ToString(), Encoding.UTF8, "application/json");
                    HttpResponseMessage response = await httpClient.PostAsync(endpoint, content);

                    if (response.IsSuccessStatusCode)
                    {
                        string jsonResponse = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(jsonResponse.ToString());
                    }
                }
                catch (Exception)
                {

                }
            }
        };

以下是应用

的屏幕截图

enter image description here

我在回收站视图中有大约5个元素。因此,当我点击第一个元素上的按钮时,我需要在回收站视图中从第一个元素发送ID。

我尝试这样做,但它没有发送任何东西。

 movieViewHolder.Done.Click += async delegate
    {
        using (HttpClient httpClient = new HttpClient())
        {
            httpClient.BaseAddress = new Uri(@"http://api.simplegames.com.ua/");
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("utf-8"));
            string endpoint = @"post/changeStatusToCompleted.php";

            try
            {
                HttpContent content = new StringContent(form[position].id.ToString(), Encoding.UTF8, "application/json");
                HttpResponseMessage response = await httpClient.PostAsync(endpoint, content);

                if (response.IsSuccessStatusCode)
                {
                    string jsonResponse = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(jsonResponse.ToString());
                }
            }
            catch (Exception)
            {

            }
        }
    };

我的麻烦在哪里?

感谢您的帮助。

更新

我对方法做了一些编辑 这是代码

string result = movieViewHolder.OrderId.Text;
                Console.WriteLine("TUT" + result);
                // Console.WriteLine("TUT"+ movieViewHolder.OrderId.Text);
                using (HttpClient httpClient = new HttpClient())
                {
                    httpClient.BaseAddress = new Uri(@"http://api.simplegames.com.ua/");
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("utf-8"));
                    string endpoint = @"post/changeStatusToCompleted.php";

                    try
                    {
                        HttpContent content = new StringContent(result, Encoding.UTF8, "application/json");
                        HttpResponseMessage response = await httpClient.PostAsync(endpoint, content);

                        if (response.IsSuccessStatusCode)
                        {
                            string jsonResponse = await response.Content.ReadAsStringAsync();
                            Console.WriteLine(jsonResponse.ToString());
                        }
                    }

0 个答案:

没有答案