2

我的Pojo课堂

package com.webnoo.questin.controllers.networks.pojo;

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

public class Profile {

    @SerializedName("uid")
    @Expose
    private String uid;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("theme")
    @Expose
    private String theme;
    @SerializedName("signature")
    @Expose
    private String signature;
    @SerializedName("signature_format")
    @Expose
    private String signature_format;
    @SerializedName("created")
    @Expose
    private String created;
    @SerializedName("access")
    @Expose
    private String access;
    @SerializedName("login")
    @Expose
    private String login;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("timezone")
    @Expose
    private String timezone;
    @SerializedName("language")
    @Expose
    private String language;
    @SerializedName("picture")
    @Expose
    private Picture picture;
    @SerializedName("data")
    @Expose
    private Data data;
    @SerializedName("uuid")
    @Expose
    private String uuid;
    @SerializedName("roles")
    @Expose
    private Roles roles;
    @SerializedName("og_user_node")
    @Expose
    private Og_user_node og_user_node;
    @SerializedName("field_first_name")
    @Expose
    private Field_first_name field_first_name;
    @SerializedName("field_last_name")
    @Expose
    private Field_last_name field_last_name;
    @SerializedName("field_education")
    @Expose
    private Field_education field_education;
    @SerializedName("field_user_experience")
    @Expose
    private Field_user_experience field_user_experience;
    @SerializedName("field_projects")
    @Expose
    private Field_projects field_projects;
    @SerializedName("field_merits_awards")
    @Expose
    private Field_merits_awards field_merits_awards;
    @SerializedName("field_publications")
    @Expose
    private Field_publications field_publications;
    @SerializedName("field_volunteering")
    @Expose
    private Field_volunteering field_volunteering;
    @SerializedName("field_personal_information")
    @Expose
    private Field_personal_information field_personal_information;
    @SerializedName("field_additional_skills")
    @Expose
    private Field_additional_skills field_additional_skills;
    @SerializedName("field_company_description")
    @Expose
    private List<Object> field_company_description = null;
    @SerializedName("field_i_am_a")
    @Expose
    private Field_i_am_a field_i_am_a;
    @SerializedName("field_groups_logo")
    @Expose
    private List<Object> field_groups_logo = null;
    @SerializedName("field_user_avatar")
    @Expose
    private Field_user_avatar field_user_avatar;
    @SerializedName("metatags")
    @Expose
    private Metatags metatags;
    @SerializedName("rdf_mapping")
    @Expose
    private Rdf_mapping rdf_mapping;
    @SerializedName("comment_notify_settings")
    @Expose
    private Comment_notify_settings comment_notify_settings;
    @SerializedName("realname")
    @Expose
    private String realname;
    @SerializedName("foo")
    @Expose
    private String foo;

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

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

    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public String getSignature_format() {
        return signature_format;
    }

    public void setSignature_format(String signature_format) {
        this.signature_format = signature_format;
    }

    public String getCreated() {
        return created;
    }

    public void setCreated(String created) {
        this.created = created;
    }

    public String getAccess() {
        return access;
    }

    public void setAccess(String access) {
        this.access = access;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getTimezone() {
        return timezone;
    }

    public void setTimezone(String timezone) {
        this.timezone = timezone;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public Picture getPicture() {
        return picture;
    }

    public void setPicture(Picture picture) {
        this.picture = picture;
    }

    public Data getData() {
        return data;
    }

    public void setData(Data data) {
        this.data = data;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public Roles getRoles() {
        return roles;
    }

    public void setRoles(Roles roles) {
        this.roles = roles;
    }

    public Og_user_node getOg_user_node() {
        return og_user_node;
    }

    public void setOg_user_node(Og_user_node og_user_node) {
        this.og_user_node = og_user_node;
    }

    public Field_first_name getField_first_name() {
        return field_first_name;
    }

    public void setField_first_name(Field_first_name field_first_name) {
        this.field_first_name = field_first_name;
    }

    public Field_last_name getField_last_name() {
        return field_last_name;
    }

    public void setField_last_name(Field_last_name field_last_name) {
        this.field_last_name = field_last_name;
    }

    public Field_education getField_education() {
        return field_education;
    }

    public void setField_education(Field_education field_education) {
        this.field_education = field_education;
    }

    public Field_user_experience getField_user_experience() {
        return field_user_experience;
    }

    public void setField_user_experience(Field_user_experience field_user_experience) {
        this.field_user_experience = field_user_experience;
    }

    public Field_projects getField_projects() {
        return field_projects;
    }

    public void setField_projects(Field_projects field_projects) {
        this.field_projects = field_projects;
    }

    public Field_merits_awards getField_merits_awards() {
        return field_merits_awards;
    }

    public void setField_merits_awards(Field_merits_awards field_merits_awards) {
        this.field_merits_awards = field_merits_awards;
    }

    public Field_publications getField_publications() {
        return field_publications;
    }

    public void setField_publications(Field_publications field_publications) {
        this.field_publications = field_publications;
    }

    public Field_volunteering getField_volunteering() {
        return field_volunteering;
    }

    public void setField_volunteering(Field_volunteering field_volunteering) {
        this.field_volunteering = field_volunteering;
    }

    public Field_personal_information getField_personal_information() {
        return field_personal_information;
    }

    public void setField_personal_information(Field_personal_information field_personal_information) {
        this.field_personal_information = field_personal_information;
    }

    public Field_additional_skills getField_additional_skills() {
        return field_additional_skills;
    }

    public void setField_additional_skills(Field_additional_skills field_additional_skills) {
        this.field_additional_skills = field_additional_skills;
    }

    public List<Object> getField_company_description() {
        return field_company_description;
    }

    public void setField_company_description(List<Object> field_company_description) {
        this.field_company_description = field_company_description;
    }

    public Field_i_am_a getField_i_am_a() {
        return field_i_am_a;
    }

    public void setField_i_am_a(Field_i_am_a field_i_am_a) {
        this.field_i_am_a = field_i_am_a;
    }

    public List<Object> getField_groups_logo() {
        return field_groups_logo;
    }

    public void setField_groups_logo(List<Object> field_groups_logo) {
        this.field_groups_logo = field_groups_logo;
    }

    public Field_user_avatar getField_user_avatar() {
        return field_user_avatar;
    }

    public void setField_user_avatar(Field_user_avatar field_user_avatar) {
        this.field_user_avatar = field_user_avatar;
    }

    public Metatags getMetatags() {
        return metatags;
    }

    public void setMetatags(Metatags metatags) {
        this.metatags = metatags;
    }

    public Rdf_mapping getRdf_mapping() {
        return rdf_mapping;
    }

    public void setRdf_mapping(Rdf_mapping rdf_mapping) {
        this.rdf_mapping = rdf_mapping;
    }

    public Comment_notify_settings getComment_notify_settings() {
        return comment_notify_settings;
    }

    public void setComment_notify_settings(Comment_notify_settings comment_notify_settings) {
        this.comment_notify_settings = comment_notify_settings;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

}

给出以下错误的用户节点类:预期 BEGIN_OBJECT 但在第 1 行第 1114 列路径 $.og_user_node 处为 BEGIN_ARRAY

我的 API 调用:

 public void getProfile (){

      Log.v("checkResponseProfile","check please");
     OkHttpClient httpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
          @Override
          public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
              Request original = chain.request();

              // Request customization: add request headers
              Request.Builder requestBuilder = original.newBuilder() .addHeader("Accept-Language","application/json")
                      .addHeader("content-type", "application/x-www-form-urlencoded").addHeader("API_KEY", "a5XSE8XCdsY6hAoCNojYBQ")
                      .addHeader("X-CSRF-Token", Constants.token)
                      ;

              Request request = requestBuilder.build();
              return chain.proceed(request);
          }
      }).build();



       Retrofit retrofit = new Retrofit.Builder()
              .baseUrl(Constants.API_BASE_URL)
              .client(httpClient)

              .addConverterFactory(GsonConverterFactory.create())
              .build();
      ApiInterface restAPI = retrofit.create(ApiInterface.class);

      Call<Profile> callProfile = restAPI.getProfile(1015);
      callProfile.enqueue(new Callback<Profile>() {
          @Override
          public void onResponse(Call<Profile> call, Response<Profile> response) {
              Log.v("checkResponseProfile",response.body().toString()+"code"+response.code());
          }

          @Override
          public void onFailure(Call<Profile> call, Throwable t) {
              Log.v("checkResponseProfile","check here");
              t.printStackTrace();
          }
      });

  }

我的接口端点方法:@GET("{uid}") 调用 getProfile(@Path("uid") int uid);

这是我在服务文档中提到的 API 结构用户检索目的:-通过 id 检索用户 Rest URL:-/api/v1/people/{uid} 方法:GET 标头:接受-语言:应用程序/json API_KEY:a5XSE8XCdsY6hAoCNojYBQ 内容-类型:application/x-www-form-urlencoded X-CSRF-Token:

4

3 回答 3

1

您的 api 调用返回一个 JSON 数组,但您的改造方法装饰需要一个 Profile 对象,这意味着您正在尝试将 JSON 数组解析为 json 对象。

您可以通过两种方式解决此问题。

  • 如果您对 api 具有权限,请使其返回 json 对象而不是 json 数组
  • 更好的是,更改您的方法装饰以返回一个 List 并在回调中检索第一项。 见下文

 Call<List<Profile>> callProfile = restAPI.getProfile(1015);
      callProfile.enqueue(new Callback<List<Profile>>() {
          @Override
          public void onResponse(Call<List<Profile>> call, Response<List<Profile>> response) {
 
     //TODO use first entry of response
        Log.v("checkResponseProfile",response.body().toString()+"code"+response.code());
          }

          @Override
          public void onFailure(Call<List<Profile>> call, Throwable t) {
              Log.v("checkResponseProfile","check here");
              t.printStackTrace();
          }
      });

希望有帮助

于 2017-06-10T14:13:09.160 回答
0

我终于解决了这个问题,问题在于我生成的用于解析的 pojo 类,我将 og_user_node 作为单个对象,但它是类型对象的列表类型(Og_user_node)从 Og_user_node 更改了 og_user_node 的类型到 List ,然后根据响应类似地更正整个 pojo 。最初我使用工具来生成我的 pojo 类,但相信我这个工具在响应很大的情况下不好,它没有给出正确的类

不管怎样,谢谢大家的回复和帮助

于 2017-06-12T09:10:04.330 回答
0

更改所有呼叫、回调和响应

Call<Profile>

Call<List<Profile>>

Callback<Profile>

Callback<List<Profile>>

Response<Flowers>

Response<List<Flowers>>
于 2017-06-10T14:12:12.093 回答