如何使用Retrofit获取JSON响应

时间:2016-07-01 13:34:40

标签: android retrofit

{
"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错误,如使用JsonReader.setLenient(true)接受第1行第1行路径中的格式错误的JSON $

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);

String CATEGORY_API =“https://api.callingservice.com”;

请帮我解决这个问题如何将Json响应转换为Bean和My 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; }

3 个答案:

答案 0 :(得分:1)

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

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

     public List<CatResponseData> getResponseData() {
          return responseData;
     }
 }

答案 1 :(得分:0)

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

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

代替: {“records”:[{},{},{}]}

答案 2 :(得分:0)

要转换,Gson在类字段上需要相同的名称。 我建议使用site,你可以粘贴JSON,然后以Gson格式返回类

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

主要课程:

 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;
}

}