Tosin Onikute
Tosin Onikute

Reputation: 4002

Retrofit: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

I'm using Retrofit for the first time, leaving behind the traditional httpclient with AsyncTask. And I am having issues understanding how Retrofit really works.

Am getting this Error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

The full url is : https://api.github.com/users/basil2style

Json return

{
  "login": "basil2style",
  "id": 1285344,
  "avatar_url": "https://avatars.githubusercontent.com/u/1285344?v=3",
  "gravatar_id": "",
  "url": "https://api.github.com/users/basil2style",
  "html_url": "https://github.com/basil2style",
  "followers_url": "https://api.github.com/users/basil2style/followers",
  "following_url": "https://api.github.com/users/basil2style/following{/other_user}",
  "gists_url": "https://api.github.com/users/basil2style/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/basil2style/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/basil2style/subscriptions",
  "organizations_url": "https://api.github.com/users/basil2style/orgs",
  "repos_url": "https://api.github.com/users/basil2style/repos",
  "events_url": "https://api.github.com/users/basil2style/events{/privacy}",
  "received_events_url": "https://api.github.com/users/basil2style/received_events",
  "type": "User",
  "site_admin": false,
  "name": "Basil",
  "company": "MakeInfo",
  "blog": "http://www.themakeinfo.com",
  "location": "India",
  "email": "[email protected]",
  "hireable": true,
  "bio": null,
  "public_repos": 39,
  "public_gists": 3,
  "followers": 28,
  "following": 129,
  "created_at": "2011-12-26T00:17:22Z",
  "updated_at": "2015-09-23T18:36:51Z"
}

Here is my code below.

MainActivity

    public class MainActivity extends ActionBarActivity  {

    List<GitHub> examplelist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RestAdapter restAdapter = new RestAdapter.Builder().
                setEndpoint("https://api.github.com").build();
        GitHubAPI gitapi = restAdapter.create(GitHubAPI.class);
        gitapi.getFeed(new Callback<List<GitHub>>() {
            @Override
            public void success(List<GitHub> github, Response response) {
                 examplelist = github;
                Log.d("Size: ", Integer.toString(examplelist.size()));
            }

            @Override
            public void failure(RetrofitError error) {
                Log.d("Error: ", error.getLocalizedMessage());
            }
        });
    }

}

Model

Am only interested in getting login, id,avatar_url and gravater_id Data from Github api

public class GitHub {
    String login;
    String id;
    String avatar_url;
    String gravatar_id;

    public String getLogin() {
        return login;
    }
    public String getId() {
        return id;
    }
    public String getAvatar_url() {
        return avatar_url;
    }
    public String getGravatar_id() {
        return gravatar_id;
    }
}

Interface

public interface GitHubAPI {
    @GET("/users/basil2style")
    public void getFeed( Callback<List<GitHub>> response);
}

Please i really need to make this work, Am frustrated here.

Upvotes: 1

Views: 4950

Answers (1)

Mateus Brandao
Mateus Brandao

Reputation: 900

Your Interface should look like this:

public interface GitHubAPI { 
    @GET("/users/basil2style") 
    public void getFeed( Callback<GitHub> response);
} 

and your callback:

gitapi.getFeed(new Callback<GitHub>() {
            @Override 
            public void success(GitHub github, Response response) { 
                Github example = github; 
                //Log.d("Size: ", Integer.toString(examplelist.size())); 
            } 

            @Override 
            public void failure(RetrofitError error) { 
                Log.d("Error: ", error.getLocalizedMessage()); 
            } 
        }); 

Upvotes: 1

Related Questions