2

MY Pojo Class

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

}

User Node class which is giving following error : Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 1114 path $.og_user_node

My API Call :

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

  }

MY Interface endpoint method : @GET("{uid}") Call getProfile(@Path("uid") int uid );

this is my API structure as mentioned in service doc User Retrieve Purpose: - User Retrieve by id Rest URL: - /api/v1/people/{uid} Method:GET Headers: Accept-Language: application/json API_KEY: a5XSE8XCdsY6hAoCNojYBQ Content-Type: application/x-www-form-urlencoded X-CSRF-Token:

Naman
  • 39
  • 11

3 Answers3

1

Your api call returns a JSON array but your retrofit method decoration expects a single Profile Object meaning you are trying to parse a JSON array as json object.

You can fix this in two ways.

  • If you have authority over the api, make it return a json object instead of a json array
  • Better still, change your method decoration to return a List and inside the callback just retrieve the first item. See below

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

Hope that helps

  • I tried ur suggestion changed the return type ftom single object to object list but got following error : Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 1114 path $.og_user_node – Naman Jun 10 '17 at 20:57
  • sry the error is ...Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ after chnaging declaration and call to List type – Naman Jun 10 '17 at 21:57
  • Can you please post the raw json response? – The-null-Pointer- Jun 11 '17 at 22:14
0

Change all Call, Callback and Response

Call<Profile>

to

Call<List<Profile>>

and

Callback<Profile>

to

Callback<List<Profile>>

and

Response<Flowers>

to

Response<List<Flowers>>
0

I have solved the issue finally , the issue is with my pojo class that I have generated which I am using for parsing , I am taking og_user_node as single object but it is list type of object of type (Og_user_node) changed type of og_user_node from Og_user_node to List and then similarly corrected the whole pojo as per the response . Initially I am using tool to generate my pojo classes but believe me that tool is not good in case the response is large ,, it does not give the correct classes

Anyways thanks guys for your response and help

Naman
  • 39
  • 11