回收站视图(Xamarin Android)

尤金

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

我尝试制作回收者视图。

这是我的json:http : //pastebin.com/rL214a2T

我有这样的课程:

 public class Billing
{
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string company { get; set; }
    public string address_1 { get; set; }
    public string address_2 { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string postcode { get; set; }
    public string country { get; set; }
    public string email { get; set; }
    public string phone { get; set; }
}

public class Shipping
{
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string company { get; set; }
    public string address_1 { get; set; }
    public string address_2 { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string postcode { get; set; }
    public string country { get; set; }
}

public class Result
{
    public int id { get; set; }
    public int parent_id { get; set; }
    public string status { get; set; }
    public string order_key { get; set; }
    public int number { get; set; }
    public string currency { get; set; }
    public string version { get; set; }
    public bool prices_include_tax { get; set; }
    public string date_created { get; set; }
    public string date_modified { get; set; }
    public int customer_id { get; set; }
    public string discount_total { get; set; }
    public string discount_tax { get; set; }
    public string shipping_total { get; set; }
    public string shipping_tax { get; set; }
    public string cart_tax { get; set; }
    public string total { get; set; }
    public string total_tax { get; set; }
    //public List<Billing> billing { get; set; }
    public Billing billing { get; set; }
    public Shipping shipping { get; set; }
    public string payment_method { get; set; }
    public string payment_method_title { get; set; }
    public string transaction_id { get; set; }
    public string customer_ip_address { get; set; }
    public string customer_user_agent { get; set; }
    public string created_via { get; set; }
    public string customer_note { get; set; }
    public string date_completed { get; set; }
    public string date_paid { get; set; }
    public string cart_hash { get; set; }
    public List<object> line_items { get; set; }
    public List<object> tax_lines { get; set; }
    public List<object> shipping_lines { get; set; }
    public List<object> fee_lines { get; set; }
    public List<object> coupon_lines { get; set; }
    public List<object> refunds { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
}

我写这样的课:

 using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using ModernHttpClient;
using Newtonsoft.Json;

namespace StarWars.Api.Repository
{
    public class MoviesRepository
    {
        public async Task<RootObject> GetAllFilms()
        {
            var httpClient = GetHttpClient();

            var response = await httpClient.GetAsync(ServiceEndPoints.StartWarsApiBaseUri).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                var content = response.Content;

                string jsonString = await content.ReadAsStringAsync().ConfigureAwait(false);


                return JsonConvert.DeserializeObject<RootObject>(jsonString);
            }
            return new RootObject();
        }

        private HttpClient GetHttpClient()
        {
            var httpClient = new HttpClient(new NativeMessageHandler())
            {
                BaseAddress = new Uri(ServiceEndPoints.StartWarsApiBaseUri)
            };


            httpClient.DefaultRequestHeaders.Accept.Clear();

            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            return httpClient;
        }
    }

    public class ServiceEndPoints
    {
        public static readonly string StartWarsApiBaseUri = "http://api.simplegames.com.ua/index.php/?wc_orders=processing_orders";
       // public static readonly string GetFilmsUri = "films";
    }
}

但是当我尝试启动应用程序时出现此错误

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'StarWars.Api.Repository.RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

据我了解,我需要设置

我该如何解决?

更新

使用Resharper,我重新制作了这样的代码

 public async Task<List<RootObject>> GetAllFilms()
    {
        var httpClient = GetHttpClient();

        var response = await httpClient.GetAsync(ServiceEndPoints.StartWarsApiBaseUri).ConfigureAwait(false);

        if (response.IsSuccessStatusCode)
        {
            var content = response.Content;

            string jsonString = await content.ReadAsStringAsync().ConfigureAwait(false);


            return JsonConvert.DeserializeObject<List<RootObject>>(jsonString);
        }
        return new List<RootObject>();
    }

    private HttpClient GetHttpClient()
    {
        var httpClient = new HttpClient(new NativeMessageHandler())
        {
            BaseAddress = new Uri(ServiceEndPoints.StartWarsApiBaseUri)
        };


        httpClient.DefaultRequestHeaders.Accept.Clear();

        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        return httpClient;
    }
}

public class ServiceEndPoints
{
    public static readonly string StartWarsApiBaseUri = "http://api.simplegames.com.ua/index.php/?wc_orders=processing_orders";
   // public static readonly string GetFilmsUri = "films";
}

}

但是我在这一行的MainActivity中有错误 var moviesAdapter = new MovieAdapter(films.results)

这是错误

错误CS1061“列表”不包含“结果”的定义,并且找不到找到接受类型为“列表”的第一个参数的扩展方法“结果”(您是否缺少using指令或程序集引用?)

维希尼基

您需要更改模型类,如果您不知道模型类的外观,则可以使用json2sharp之类的工具您不能JSON使用您的课程反序列化使用此类代替您的类。

public class Billing
{
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string company { get; set; }
    public string address_1 { get; set; }
    public string address_2 { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string postcode { get; set; }
    public string country { get; set; }
    public string email { get; set; }
    public string phone { get; set; }
}

public class Shipping
{
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string company { get; set; }
    public string address_1 { get; set; }
    public string address_2 { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string postcode { get; set; }
    public string country { get; set; }
}

public class LineItem
{
    public int id { get; set; }
    public string name { get; set; }
    public string sku { get; set; }
    public int product_id { get; set; }
    public int variation_id { get; set; }
    public int quantity { get; set; }
    public string tax_class { get; set; }
    public string price { get; set; }
    public string subtotal { get; set; }
    public string subtotal_tax { get; set; }
    public string total { get; set; }
    public string total_tax { get; set; }
    public List<object> taxes { get; set; }
    public List<object> meta { get; set; }
}

public class ShippingLine
{
    public int id { get; set; }
    public string method_title { get; set; }
    public string method_id { get; set; }
    public string total { get; set; }
    public string total_tax { get; set; }
    public List<object> taxes { get; set; }
}

public class RootObject
{
    public int id { get; set; }
    public int parent_id { get; set; }
    public string status { get; set; }
    public string order_key { get; set; }
    public int number { get; set; }
    public string currency { get; set; }
    public string version { get; set; }
    public bool prices_include_tax { get; set; }
    public string date_created { get; set; }
    public string date_modified { get; set; }
    public int customer_id { get; set; }
    public string discount_total { get; set; }
    public string discount_tax { get; set; }
    public string shipping_total { get; set; }
    public string shipping_tax { get; set; }
    public string cart_tax { get; set; }
    public string total { get; set; }
    public string total_tax { get; set; }
    public Billing billing { get; set; }
    public Shipping shipping { get; set; }
    public string payment_method { get; set; }
    public string payment_method_title { get; set; }
    public string transaction_id { get; set; }
    public string customer_ip_address { get; set; }
    public string customer_user_agent { get; set; }
    public string created_via { get; set; }
    public string customer_note { get; set; }
    public string date_completed { get; set; }
    public string date_paid { get; set; }
    public string cart_hash { get; set; }
    public List<LineItem> line_items { get; set; }
    public List<object> tax_lines { get; set; }
    public List<ShippingLine> shipping_lines { get; set; }
    public List<object> fee_lines { get; set; }
    public List<object> coupon_lines { get; set; }
    public List<object> refunds { get; set; }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android:控制平滑滚动浏览回收站视图

来自分类Dev

返回后的Kotlin Android片段空回收站视图

来自分类Dev

如何在Android的回收站视图中运行for循环?

来自分类Dev

Android:在回收站视图中设置文本

来自分类Dev

Android:片段内的多个回收站视图

来自分类Dev

Xamarin Android搜索回收站查看项目

来自分类Dev

Android-如何使用回收站视图创建类似的视图?

来自分类Dev

Android-如何使用回收站视图创建类似的视图?

来自分类Dev

Android:从微调框选择的项目上更新回收站视图所需的建议

来自分类Dev

像回收站视图android中的添加朋友按钮的Snapchat

来自分类Dev

如何在Android中将数据从服务器显示到回收站视图中

来自分类Dev

在Android片段中的回收站视图上方删除不需要的空白空间

来自分类Dev

如何知道是否点击了回收站视图项中的android

来自分类Dev

将项目值保存在回收站视图对象 android 中

来自分类Dev

Android:回收站视图没有滚动到最后一个位置

来自分类Dev

回收站视图错误

来自分类Dev

如何在没有 div 的情况下使用 jsoup 解析 android studio 中的 ul 、 li 标签并将其显示在回收站视图中?

来自分类Dev

回收站视图不刷新

来自分类Dev

回收站视图滚动搞砸了

来自分类Dev

无法设置回收站视图

来自分类Dev

回收站视图位置比较

来自分类Dev

回收站视图不刷新

来自分类Dev

Android:如何在回收站布局适配器中动态填充片段?

来自分类Dev

Android滤清器回收站查看来自翻新的数据

来自分类Dev

从回收站视图的适配器内重新加载回收站视图

来自分类Dev

回收站视图与滚动视图内的线性布局?

来自分类Dev

当回收站视图中的数据项增加时,回收站视图数据将位于文本视图下

来自分类Dev

无休止的回收站视图重复json数据

来自分类Dev

回收站视图中的无线电组

Related 相关文章

  1. 1

    Android:控制平滑滚动浏览回收站视图

  2. 2

    返回后的Kotlin Android片段空回收站视图

  3. 3

    如何在Android的回收站视图中运行for循环?

  4. 4

    Android:在回收站视图中设置文本

  5. 5

    Android:片段内的多个回收站视图

  6. 6

    Xamarin Android搜索回收站查看项目

  7. 7

    Android-如何使用回收站视图创建类似的视图?

  8. 8

    Android-如何使用回收站视图创建类似的视图?

  9. 9

    Android:从微调框选择的项目上更新回收站视图所需的建议

  10. 10

    像回收站视图android中的添加朋友按钮的Snapchat

  11. 11

    如何在Android中将数据从服务器显示到回收站视图中

  12. 12

    在Android片段中的回收站视图上方删除不需要的空白空间

  13. 13

    如何知道是否点击了回收站视图项中的android

  14. 14

    将项目值保存在回收站视图对象 android 中

  15. 15

    Android:回收站视图没有滚动到最后一个位置

  16. 16

    回收站视图错误

  17. 17

    如何在没有 div 的情况下使用 jsoup 解析 android studio 中的 ul 、 li 标签并将其显示在回收站视图中?

  18. 18

    回收站视图不刷新

  19. 19

    回收站视图滚动搞砸了

  20. 20

    无法设置回收站视图

  21. 21

    回收站视图位置比较

  22. 22

    回收站视图不刷新

  23. 23

    Android:如何在回收站布局适配器中动态填充片段?

  24. 24

    Android滤清器回收站查看来自翻新的数据

  25. 25

    从回收站视图的适配器内重新加载回收站视图

  26. 26

    回收站视图与滚动视图内的线性布局?

  27. 27

    当回收站视图中的数据项增加时,回收站视图数据将位于文本视图下

  28. 28

    无休止的回收站视图重复json数据

  29. 29

    回收站视图中的无线电组

热门标签

归档