【问题标题】:How to getting JSON Response using Retrofit如何使用 Retrofit 获取 JSON 响应
【发布时间】:2016-11-03 22:49:23
【问题描述】:
{
"records": [
    {
        "id": "744",
        "categoryname": "Floor Tiles",
        "photo": "",
        "width": "0",
        "height": "0",
        "secondlevelcategory": [
            {
                "id": "833",
                "categoryname": "Digital Vitrified Tiles",
                "photo": "http://image.com",
                "width": "1031",
                "height": "700"
            }
        ]
    },
    {
        "id": "744",
        "categoryname": "Floor Tiles",
        "photo": "",
        "width": "0",
        "height": "0",
        "secondlevelcategory": [
            {
                "id": "833",
                "categoryname": "Digital Vitrified Tiles",
                "photo": "http://image.com",
                "width": "1031",
                "height": "700"
            }
        ]
    }
] }

如何将此 Json 响应转换为 Retrofit bean 我收到 Gson 错误,例如 Use JsonReader.setLenient(true) to accept 格式错误的 JSON 在第 1 行第 1 列路径 $

private void callCategoryApi() {
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(GithubUserAPI.CATEGORY_API)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    GithubUserAPI githubUserAPI = retrofit.create(GithubUserAPI.class);
    Call<List<CatResponseData>> call = githubUserAPI.getCategory(Utils.key);
    call.enqueue(this);

}

@Override
public void onResponse(Call<List<CatResponseData>> call, Response<List<CatResponseData>> response) {
    int code = response.code();
    if (code == 200) {
        List<CatResponseData> res = response.body();
        for(CatResponseData resDat : res){
            System.out.println("=== Response : " + resDat.getCategoryname());
        }
    }else{
        Toast.makeText(this, "Did not work: " + String.valueOf(code), Toast.LENGTH_LONG).show();
    }
}

@Override
public void onFailure(Call<List<CatResponseData>> call, Throwable t) {
    System.out.println("=== Error : " + t.getMessage());
}

API 调用是

@POST("/apikey/{apikey}")
Call<List<CatResponseData>> getCategory(@Path("apikey") String user);

字符串 CATEGORY_API = "https://api.callingservice.com";

请帮我解决这个问题我如何将 Json 响应转换为 Bean,我的 Bean 类就像

public class CategoryBean {
List<CatResponseData> responseData = new ArrayList<CatResponseData>(); } class CatResponseData {
String id;
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getCategoryname() {
    return categoryname;
}
public void setCategoryname(String categoryname) {
    this.categoryname = categoryname;
}
String categoryname; }

【问题讨论】:

    标签: android retrofit


    【解决方案1】:

    这应该是你给 Retrofit 的类。不是 bean 的列表,因为它们被封装在一个对象中。

     public class CategoryResponse {
         @SerializedName("records")
         List<CategoryBean> responseData;
    
         public List<CatResponseData> getResponseData() {
              return responseData;
         }
     }
    

    【讨论】:

    • 嗨 toshkinl 感谢您的回复,在创建 CategoryResponse 类之后,我如何访问 id、categoryname、height、secondlevelcategory 以及如何调用 api,请指导我,谢谢
    • 首先,这就是你的 bean 应该看起来像 pastebin.com/fqx3k1KH 的样子。然后当你加入呼叫时
    【解决方案2】:

    抱歉,我无法发表评论,但我想提供帮助;难道改造只是在等待没有键名的项目数组吗?我的意思是,也许你需要告诉 Json 在迭代之前获取对象“记录”

    在我的改造中,我收到了 json 数组,例如: [{},{},{}]

    改为: {“记录”:[{},{},{}]}

    【讨论】:

      【解决方案3】:

      要转换,Gson 需要在类字段上使用相同的名称。 我推荐使用site,你可以粘贴你的JSON,他会返回Gson格式的类

      对于您拥有的每个列表,您需要一个包含对象的类。喜欢:

      主类:

       import java.util.ArrayList;
          import java.util.List;
          import javax.annotation.Generated;
          import com.google.gson.annotations.Expose;
          import com.google.gson.annotations.SerializedName;
      
          @Generated("org.jsonschema2pojo")
          public class Example {
      
          @SerializedName("records")
         @Expose
         private List<Record> records = new ArrayList<Record>();
      
          /**
          * No args constructor for use in serialization
      * 
      */
      public Example() {
      }
      
      /**
      * 
      * @param records
      */
      public Example(List<Record> records) {
      this.records = records;
      }
      
      /**
      * 
      * @return
      * The records
      */
      public List<Record> getRecords() {
      return records;
      }
      
      /**
      * 
      * @param records
      * The records
      */
      public void setRecords(List<Record> records) {
      this.records = records;
      }
      
      public Example withRecords(List<Record> records) {
      this.records = records;
      return this;
      }
      
      }
      

      Record.java

      package com.example;
      
      import java.util.ArrayList;
      import java.util.List;
      import javax.annotation.Generated;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;
      
      @Generated("org.jsonschema2pojo")
      public class Record {
      
      @SerializedName("id")
      @Expose
      private String id;
      @SerializedName("categoryname")
      @Expose
      private String categoryname;
      @SerializedName("photo")
      @Expose
      private String photo;
      @SerializedName("width")
      @Expose
      private String width;
      @SerializedName("height")
      @Expose
      private String height;
      @SerializedName("secondlevelcategory")
      @Expose
      private List<Secondlevelcategory> secondlevelcategory = new ArrayList<Secondlevelcategory>();
      
      /**
      * No args constructor for use in serialization
      * 
      */
      public Record() {
      }
      
      /**
      * 
      * @param id
      * @param secondlevelcategory
      * @param height
      * @param width
      * @param categoryname
      * @param photo
      */
      public Record(String id, String categoryname, String photo, String width, String height, List<Secondlevelcategory> secondlevelcategory) {
      this.id = id;
      this.categoryname = categoryname;
      this.photo = photo;
      this.width = width;
      this.height = height;
      this.secondlevelcategory = secondlevelcategory;
      }
      
      /**
      * 
      * @return
      * The id
      */
      public String getId() {
      return id;
      }
      
      /**
      * 
      * @param id
      * The id
      */
      public void setId(String id) {
      this.id = id;
      }
      
      public Record withId(String id) {
      this.id = id;
      return this;
      }
      
      /**
      * 
      * @return
      * The categoryname
      */
      public String getCategoryname() {
      return categoryname;
      }
      
      /**
      * 
      * @param categoryname
      * The categoryname
      */
      public void setCategoryname(String categoryname) {
      this.categoryname = categoryname;
      }
      
      public Record withCategoryname(String categoryname) {
      this.categoryname = categoryname;
      return this;
      }
      
      /**
      * 
      * @return
      * The photo
      */
      public String getPhoto() {
      return photo;
      }
      
      /**
      * 
      * @param photo
      * The photo
      */
      public void setPhoto(String photo) {
      this.photo = photo;
      }
      
      public Record withPhoto(String photo) {
      this.photo = photo;
      return this;
      }
      
      /**
      * 
      * @return
      * The width
      */
      public String getWidth() {
      return width;
      }
      
      /**
      * 
      * @param width
      * The width
      */
      public void setWidth(String width) {
      this.width = width;
      }
      
      public Record withWidth(String width) {
      this.width = width;
      return this;
      }
      
      /**
      * 
      * @return
      * The height
      */
      public String getHeight() {
      return height;
      }
      
      /**
      * 
      * @param height
      * The height
      */
      public void setHeight(String height) {
      this.height = height;
      }
      
      public Record withHeight(String height) {
      this.height = height;
      return this;
      }
      
      /**
      * 
      * @return
      * The secondlevelcategory
      */
      public List<Secondlevelcategory> getSecondlevelcategory() {
      return secondlevelcategory;
      }
      
      /**
      * 
      * @param secondlevelcategory
      * The secondlevelcategory
      */
      public void setSecondlevelcategory(List<Secondlevelcategory> secondlevelcategory) {
      this.secondlevelcategory = secondlevelcategory;
      }
      
      public Record withSecondlevelcategory(List<Secondlevelcategory> secondlevelcategory) {
      this.secondlevelcategory = secondlevelcategory;
      return this;
      }
      
      }
      

      Secondlevelcategory.java

      package com.example;
      
      import javax.annotation.Generated;
      import com.google.gson.annotations.Expose;
      import com.google.gson.annotations.SerializedName;
      
      @Generated("org.jsonschema2pojo")
      public class Secondlevelcategory {
      
      @SerializedName("id")
      @Expose
      private String id;
      @SerializedName("categoryname")
      @Expose
      private String categoryname;
      @SerializedName("photo")
      @Expose
      private String photo;
      @SerializedName("width")
      @Expose
      private String width;
      @SerializedName("height")
      @Expose
      private String height;
      
      /**
      * No args constructor for use in serialization
      * 
      */
      public Secondlevelcategory() {
      }
      
      /**
      * 
      * @param id
      * @param height
      * @param width
      * @param categoryname
      * @param photo
      */
      public Secondlevelcategory(String id, String categoryname, String photo, String width, String height) {
      this.id = id;
      this.categoryname = categoryname;
      this.photo = photo;
      this.width = width;
      this.height = height;
      }
      
      /**
      * 
      * @return
      * The id
      */
      public String getId() {
      return id;
      }
      
      /**
      * 
      * @param id
      * The id
      */
      public void setId(String id) {
      this.id = id;
      }
      
      public Secondlevelcategory withId(String id) {
      this.id = id;
      return this;
      }
      
      /**
      * 
      * @return
      * The categoryname
      */
      public String getCategoryname() {
      return categoryname;
      }
      
      /**
      * 
      * @param categoryname
      * The categoryname
      */
      public void setCategoryname(String categoryname) {
      this.categoryname = categoryname;
      }
      
      public Secondlevelcategory withCategoryname(String categoryname) {
      this.categoryname = categoryname;
      return this;
      }
      
      /**
      * 
      * @return
      * The photo
      */
      public String getPhoto() {
      return photo;
      }
      
      /**
      * 
      * @param photo
      * The photo
      */
      public void setPhoto(String photo) {
      this.photo = photo;
      }
      
      public Secondlevelcategory withPhoto(String photo) {
      this.photo = photo;
      return this;
      }
      
      /**
      * 
      * @return
      * The width
      */
      public String getWidth() {
      return width;
      }
      
      /**
      * 
      * @param width
      * The width
      */
      public void setWidth(String width) {
      this.width = width;
      }
      
      public Secondlevelcategory withWidth(String width) {
      this.width = width;
      return this;
      }
      
      /**
      * 
      * @return
      * The height
      */
      public String getHeight() {
      return height;
      }
      
      /**
      * 
      * @param height
      * The height
      */
      public void setHeight(String height) {
      this.height = height;
      }
      
      public Secondlevelcategory withHeight(String height) {
      this.height = height;
      return this;
      }
      
      }
      

      【讨论】:

      • 嗨 Lucas 感谢您的回复,我只是按照您说的创建了一个 pojo 类,并且我调用 api 像 @POST("/apikey/{apikey}") Call getCategory(@Path ("apikey") 字符串用户);你能说我在哪里做错了吗,因为我仍然有错误,比如 Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
      • 在您的 onResponse 上,您更改了正确的课程吗?之前它是一个对象列表,现在它只是一个对象(其中包含其他对象的列表)
      猜你喜欢
      • 2016-11-18
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多