改造Pojo模型

时间:2018-03-29 11:50:56

标签: android retrofit

我通过http://www.jsonschema2pojo.org/从我的JSON自动生成了POJO 我用它来改造。 这是JSON:

[{"id":1942,"name":"The Witcher 3: Wild Hunt","first_release_date":1431993600000,"screenshots":[{"url":"//images.igdb.com/igdb/image/upload/t_thumb/z5t0yuhyiiui1ickwhgj.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/farvemmmxav0bgt6wx7t.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/em1y2ugcwy2myuhvb9db.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/usxccsncekxg0wd1v6ee.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/mnljdjtrh44x4snmierh.jpg"}],"videos":[{"video_id":"ZpiczsigQto"},{"video_id":"5nLipy-Z4yo"},{"video_id":"FP7no968jVU"},{"video_id":"_IBAovRNCuA"},{"video_id":"QrwGXAcE6ZA"},{"video_id":"6f8TbvsZ5Mk"},{"video_id":"bcEAsOC_8L0"},{"video_id":"xQGam9OHSUo"},{"video_id":"p14dHAwLOmo"},{"video_id":"8ZLfJjlZKvc"},{"video_id":"sb81f-ejNSI"},{"video_id":"yowv6_rspoM"}],"cover":{"url":"//images.igdb.com/igdb/image/upload/t_thumb/tri1c6vbydeosoqajwt1.jpg"}},{"id":80,"name":"The Witcher","first_release_date":1193356800000,"screenshots":[{"url":"//images.igdb.com/igdb/image/upload/t_thumb/qzgxs5t8zw40o2m290li.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/qwrg0zmgl3hue6zs8bbb.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/jquhmu9izv8i2zf3tb8l.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/jhc0fbq7v6xnwqs9c4pm.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/ekchkvznt1pkre0cvbpl.jpg"}],"cover":{"url":"//images.igdb.com/igdb/image/upload/t_thumb/nrkdzmkevbbevdvm9bxh.jpg"}},{"id":44549,"name":"The Witcher 3: Wild Hunt Collector's Edition","first_release_date":1434672000000,"screenshots":[{"url":"//images.igdb.com/igdb/image/upload/t_thumb/y8caw9l1xoy5ucqoz2ge.jpg"}],"cover":{"url":"//images.igdb.com/igdb/image/upload/t_thumb/wej7rkkjys0lohit1pic.jpg"}},{"id":22439,"name":"The Witcher 3: Wild Hunt - Game of the Year Edition","first_release_date":1472515200000,"screenshots":[{"url":"//images.igdb.com/igdb/image/upload/t_thumb/zvijqp04apwmzqvlgxej.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/a4tiz3sfoblnau6olx10.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/pziw7giojvmp8baw617b.jpg"}],"videos":[{"video_id":"qy8jmm9kY4A"},{"video_id":"YmLeUJgzsXY"},{"video_id":"5wmNeg5WVck"}],"cover":{"url":"//images.igdb.com/igdb/image/upload/t_thumb/whmf3el1gv3ujreysrrh.jpg"}},{"id":20275,"name":"The Witcher: Enhanced Edition","first_release_date":1221523200000,"screenshots":[{"url":"//images.igdb.com/igdb/image/upload/t_thumb/iqmzjkk9xiy7lxjadjcv.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/hcemi3tnjzajiog5p1sk.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/xrry4grvrwvzehqb3guz.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/k3r3djcocmdhn00vwxs8.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/i1ucakdnk52r3i2rbpdm.jpg"}],"videos":[{"video_id":"LMStRRVPt6g"}],"cover":{"url":"//images.igdb.com/igdb/image/upload/t_thumb/iu0fwtdxbhxq3nueup9w.jpg"}},{"id":13166,"name":"The Witcher 3: The Wild Hunt - Blood and Wine","first_release_date":1464652800000,"screenshots":[{"url":"//images.igdb.com/igdb/image/upload/t_thumb/bhvtikx5as53awacxlac.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/iuzxf8iboeyxkggywbsj.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/gynhuum2yb8imujgjlxn.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/k6zrdwofvuccfkkj6qus.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/ixuz3ti1kiqnlzrtodnf.jpg"}],"videos":[{"video_id":"6gpHJWkPYto"},{"video_id":"2OOPrprSwC0"},{"video_id":"k-QWCiHZPNc"},{"video_id":"rIoPrbzI5Z4"}],"cover":{"url":"//images.igdb.com/igdb/image/upload/t_thumb/yqbnxszx6mnpg2ymfdsw.jpg"}},{"id":12503,"name":"The Witcher 3: The Wild Hunt - Hearts of Stone","first_release_date":1444694400000,"screenshots":[{"url":"//images.igdb.com/igdb/image/upload/t_thumb/wrke9dxtduvuubfrm0kq.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/rzpm2ms8e15uekzlwrvy.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/wiw1xlpfihxbe1xlft3t.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/lz3gmntnjo5fojxzvrg5.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/wv5bxefrjdgzxkdufqjg.jpg"}],"videos":[{"video_id":"vyoi-aVU0mw"},{"video_id":"mTHvlito464"}],"cover":{"url":"//images.igdb.com/igdb/image/upload/t_thumb/slppva5kydxsw4ykw0zf.jpg"}},{"id":8765,"name":"The Witcher: Adventure Game","first_release_date":1417046400000,"screenshots":[{"url":"//images.igdb.com/igdb/image/upload/t_thumb/wonicxamvtkyaxdwmvrz.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/qihqvlqmlhr6gjcxqg2k.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/ocwcjm3dn8b2mhe9mwsp.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/bfmiksnjx0d6poi4qa7i.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/gaiyxvvjnvszbf0yeeni.jpg"}],"cover":{"url":"//images.igdb.com/igdb/image/upload/t_thumb/lchergnnb5liqfyatkad.jpg"}},{"id":9689,"name":"The Witcher Battle Arena","first_release_date":1421884800000,"screenshots":[{"url":"//images.igdb.com/igdb/image/upload/t_thumb/wmszzamngmfue3953he7.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/sf8u8xqf0d569pihrhxe.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/olufs65pklmiylkhko2s.jpg"},{"url":"//images.igdb.com/igdb/image/upload/t_thumb/lidn5pjatpkb8skabk8d.jpg"}],"videos":[{"video_id":"2mWw7onLGs0"}],"cover":{"url":"//images.igdb.com/igdb/image/upload/t_thumb/xwkyw8mxtatbcbzvnjnk.jpg"}},{"id":89846,"name":"The Witcher 3: Wild Hunt Prima Guide Edition"}]

这些是生成的POJOS:

-----------------------------------com.example.Cover.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Cover {

@SerializedName("url")
@Expose
private String url;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

}
-----------------------------------com.example.Result.java-----------------------------------

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Result {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("first_release_date")
@Expose
private Integer firstReleaseDate;
@SerializedName("screenshots")
@Expose
private List<Screenshot> screenshots = null;
@SerializedName("videos")
@Expose
private List<Video> videos = null;
@SerializedName("cover")
@Expose
private Cover cover;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getFirstReleaseDate() {
return firstReleaseDate;
}

public void setFirstReleaseDate(Integer firstReleaseDate) {
this.firstReleaseDate = firstReleaseDate;
}

public List<Screenshot> getScreenshots() {
return screenshots;
}

public void setScreenshots(List<Screenshot> screenshots) {
this.screenshots = screenshots;
}

public List<Video> getVideos() {
return videos;
}

public void setVideos(List<Video> videos) {
this.videos = videos;
}

public Cover getCover() {
return cover;
}

public void setCover(Cover cover) {
this.cover = cover;
}

}
-----------------------------------com.example.Screenshot.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Screenshot {

@SerializedName("url")
@Expose
private String url;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

}
-----------------------------------com.example.Video.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Video {

@SerializedName("video_id")
@Expose
private String videoId;

public String getVideoId() {
return videoId;
}

public void setVideoId(String videoId) {
this.videoId = videoId;
}

}

总是失败回应......任何想法可能是什么原因?

这是我的改造界面:

public interface IGDBApi {
    @Headers({
            "Accept: application/json",
            "user-key: (removed for this post)"
    })

    @GET("games/?search=TheWitcher3&fields=id,name,first_release_date,cover.url,screenshots.url,videos.video_id")
    Call<List<Result>> loadFollowers();


    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api-2445582011268.apicast.io/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

}

这是我在主要活动中运行改造的方法:

public void runretrofit(){

    //Starts Retrofit
    final IGDBApi gitHubApi = IGDBApi.retrofit.create(IGDBApi.class);
    //Sets up up the API call
    Call<List<Result>> call = gitHubApi.loadFollowers();
    //Runs the call on a different thread

        Log.v("URL Called", call.request().url() + "");

    call.enqueue(new Callback<List<Result>>() {
        @Override
        public void onResponse(Call<List<Result>> call, Response<List<Result>> response) {
            Log.v("RESULT", "JSON: "+new Gson().toJson(response.body()) );
        }

        @Override
        public void onFailure(Call<List<Result>> call, Throwable t) {
            Log.v("RESULT Fail", "Request failed");
        }
    });
}

1 个答案:

答案 0 :(得分:1)

首先

制作

char *m_strncpy(char *str1, int count) {
  int i;
  char *new_str = malloc(count + 1);  // +1 for the NUL terminator

  if (new_str != NULL)
  {    
    for (i = 0; i < count; i++) {       // copy count first chars
      new_str[i] = str1[i];
    }

    new_str[i] = 0;                     // NUL terminate the string
  }

  return new_str;
}

private Integer firstReleaseDate;

<强>第二

检查所有包含值的列表项。如果某些项具有浮点值,则会抛出错误。