anand
anand

Reputation: 1751

Not able to populate pojo with RestAPI Data

I am using android-bootsrap from this link for my project from
https://github.com/AndroidBootstrap/android-bootstrap

I retained the project structure as it is and not performing authentication. In my apps i am making a call to a rest API which is returning the data in Json format. Using retrofit we are calling the services and getting the data in Json also.

We have created one POJO class for Json API. But in my app we are unable to get the Pojo object populated with Rest API data.

My code look as follows: User.java

public class User implements Serializable {
 private String shortDescription;
 private String name;

 public String getShortDescription() { return shortDescription;}
 public void setShortDescription(String shortDescription) {
    this.shortDescription = shortDescription;
 }

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

UserService.java

 public interface UserService {
   @GET("/search/items")
   UsersWrapper getUsers();
 }

UserWrapper.java

public class UsersWrapper {
 private User results;
 public User getResults() { return results; }
}

MainActivity.class

public class MainActivity extends BaseActivity {
 String data = "";
 private User user;
 @Inject protected BootstrapServiceProvider serviceProvider;

 @Override
 protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new MyTask().execute();
 }

// making async request 
 class MyTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        try {
            user = serviceProvider.getService().getUsers();   
            user.getShortDescription();   // here i am getting NullPointerException
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }
}

I am getting the data in the retrofit log but when i am trying to get data through User object i am getting NullPointerException.

My Json data is as follows:

{"name":"user1","short_description":"user1 is a postgraduate student"}

Upvotes: 0

Views: 540

Answers (1)

Pedlar
Pedlar

Reputation: 1054

So, the way you have your User/UserWrapper class setup GSON is expecting a JSON result that looks like:

{
  "results": {
    "name": "user1",
    "short_description": "user1 is a postgraduate student"
  }
}

If the JSON Result is just the stuff inside of the "results" block in my above example you would want to ditch the UserWrapper all together, and change your Service Interface to look like:

public interface UserService {
   @GET("/search/items")
   User getUsers();
}

If the JSON Result is an Array of Users such as

[
  {
    "name": "user1",
    "short_description": "user1 is a postgraduate student"
  },
  ...
]

Then you would want your Interface to look like:

public interface UserService {
   @GET("/search/items")
   List<User> getUsers();
}

Upvotes: 1

Related Questions