I've done some Retrofit projects and it always worked but I'm doing one that consumes end-point from api.github.com and is giving an error and I can not find a solution, I've done everything I could, if anyone knows what to do .
- The error displayed when I put Log in onFailure is Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
- Dry the URL of the endpoint I am trying to consume, I will not post the JSON generated by it here because it is too large. link
The Classes:
public class Items implements Serializable{
private int id;
private String name;
private String full_name;
@SerializedName("stargazers_count")
private int stars;
private Owner owner;
private int forks;
private String pulls_url;
public Items() {
}
public Items(String name, String full_name, int stars, Owner owner, int forks, String pulls_url) {
this.name = name;
this.full_name = full_name;
this.stars = stars;
this.owner = owner;
this.forks = forks;
this.pulls_url = pulls_url;
}
public Items(int id, String name, String full_name, int stars, Owner owner, int forks, String pulls_url) {
this.id = id;
this.name = name;
this.full_name = full_name;
this.stars = stars;
this.owner = owner;
this.forks = forks;
this.pulls_url = pulls_url;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFull_name() {
return full_name;
}
public void setFull_name(String full_name) {
this.full_name = full_name;
}
public int getStars() {
return stars;
}
public void setStars(int stars) {
this.stars = stars;
}
public Owner getOwner() {
return owner;
}
public void setOwner(Owner owner) {
this.owner = owner;
}
public int getForks() {
return forks;
}
public void setForks(int forks) {
this.forks = forks;
}
public String getPulls_url() {
return pulls_url;
}
public void setPulls_url(String pulls_url) {
this.pulls_url = pulls_url;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Items that = (Items) o;
return id == that.id;
}
@Override
public int hashCode() {
return id;
}
@Override
public String toString() {
return "Items{" +
"id=" + id +
", name='" + name + '\'' +
", owner=" + owner +
'}';
}
}
public interface RepositoryAPI {
@GET("repositories?q=language:Java&sort=stars&page=1")
public Call<List<Items>> getAllRepositories();
Gson gson = new GsonBuilder().registerTypeAdapter(Items.class, new ItemDeserializer()).create();
public static final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/search/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
public class ControllerAccessWS {
RepositoryAPI repositoryAPI;
//public synchronized List<Items> getAllRepositores() {
public void getAllRepositores() {
Log.i("app", "getAllRepositores");
Gson gson = new GsonBuilder().registerTypeAdapter(Items.class, new ItemDeserializer()).create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/search/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
Log.i("app", "retrofit");
repositoryAPI = retrofit.create(RepositoryAPI.class);
Call<List<Items>> callListRepositore = repositoryAPI.getAllRepositories();
Log.i("app", "repositoryAPI = retrofit.create");
callListRepositore.enqueue(new Callback<List<Items>>() {
@Override
public void onResponse(Call<List<Items>> call, Response<List<Items>> response) {
List<Items> listItems = new ArrayList<Items>();
listItems.addAll(response.body());
for (int i = 0; i < listItems.size(); i++) {
Log.i("app", "Repositori ID: " + listItems.get(i).getId());
}
/**
List<Items> listRepositore;
Log.i("app", "callListRepositore.enqueue - onResponse");
if (response.isSuccessful()) {
Log.i("app", "callListRepositore.enqueue - response.isSuccessful()");
listRepositore = response.body();
Log.i("app", "callListRepositore.enqueue - onResponse - response.code() == 200");
for (int i = 0; i < listRepositore.size(); i++) {
Log.i("app", "Repositori ID: " + listRepositore.get(i).getId());
}
}else{
Log.e("app", "Erro no Response: " + response.code());
}
**/
}
@Override
public void onFailure(Call<List<Items>> call, Throwable t) {
Log.e("app", "onFailure - Erro no Response: " + t.getMessage());
}
});
//return null;
}
}
public class ItemDeserializer implements JsonDeserializer<Items>{
@Override
public Items deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonElement items = json.getAsJsonObject();
if(json.getAsJsonObject().get("items") != null){
items = json.getAsJsonObject().get("items");
}
return (new Gson().fromJson(items, Items.class));
}
}
public class MainActivity extends AppCompatActivity {
private Toolbar mTbMain;
private Drawer mNavDrawer;
private AccountHeader mAccountNavDrawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ControllerAccessWS controllerAccessWS = new ControllerAccessWS();
controllerAccessWS.getAllRepositores();
}
}