J'essaie d'utiliser Retrofit2 pour récupérer des données en JSON, puis de les analyser dans mon adaptateur RecyclerView, mais quoi que je fasse, je n'arrive pas à comprendre comment faire pour que ça marche.Retrofit2 HTTP Obtenir Json à RecyclerView
Pour l'instant, je peux charger un RecyclerView avec des données DBFlow locales, mais je n'arrive pas à comprendre comment le faire pour le JSON que j'ai HTTP GET avec Retrofit2 pour mon second RecyclerView. J'ai le code ci-dessous jusqu'à présent, qui charge le JSON dans le journal. Mais ne peut pas l'analyser sur le RecyclerView.
MainActivity.java:
onCreate() {
...
mGithubApi = ApiUtils.getApi();
mGithubRecyclerView = (RecyclerView)
findViewById(R.id.github_repository_recyclerview);
RecyclerView.LayoutManager mGithubLayoutManager = new
LinearLayoutManager(getApplicationContext());
mGithubRecyclerView.setLayoutManager(mGithubLayoutManager);
mGithubRecyclerView.setHasFixedSize(true);
...
loadGithubUserRepositoryJson();
}
// Load Github repo JSON
public void loadGithubUserRepositoryJson() {
Call<ResponseBody> result = api.getRepos("motivecodex");
result.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
Log.e(LOG_TAG, response.body().string());
mGithubAdapter = new GithubRepositoriesAdapter((List<GithubRepository>) mGithubRepository);
mGithubRecyclerView.setAdapter(mGithubAdapter);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("MainActivity", "error loading from API");
}
});
}
GithubRepositoryAdapter.java
public class GithubRepositoriesAdapter extends RecyclerView.Adapter<GithubRepositoriesAdapter.RepositoryViewHolder> {
private List<GithubRepository> githubRepositoryList;
public class RepositoryViewHolder extends RecyclerView.ViewHolder {
public TextView repository, commits, stars, forks;
public RepositoryViewHolder(View view) {
super(view);
repository = (TextView) view.findViewById(R.id.listitem_repository);
commits = (TextView) view.findViewById(R.id.listitem_commits);
stars = (TextView) view.findViewById(R.id.listitem_stars);
forks = (TextView) view.findViewById(R.id.listitem_forked);
}
}
public GithubRepositoriesAdapter(List<GithubRepository> githubRepositoryList) {
this.githubRepositoryList = githubRepositoryList;
}
@Override
public RepositoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listitem_repository, parent, false);
return new RepositoryViewHolder(itemView);
}
@Override
public void onBindViewHolder(RepositoryViewHolder holder, int position) {
GithubRepository githubRepository = githubRepositoryList.get(position);
holder.repository.setText(githubRepository.getName());
holder.commits.setText(githubRepository.getStargazersCount());
holder.stars.setText(githubRepository.getStargazersCount());
holder.forks.setText(githubRepository.getForks());
}
@Override
public int getItemCount() {
return githubRepositoryList.size();
}
}
Interface Api.java:
@GET("users/{user}/repos")
Call<List<GithubRepository>> listRepos(@Path("user") String user);
modèle
GithubRepository.java
public class GithubRepository {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("full_name")
@Expose
private String fullName;
@SerializedName("stargazers_count")
@Expose
private Integer stargazersCount;
@SerializedName("forks")
@Expose
private Integer forks;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public Integer getStargazersCount() {
return stargazersCount;
}
public void setStargazersCount(Integer stargazersCount) {
this.stargazersCount = stargazersCount;
}
public Integer getForks() {
return forks;
}
public void setForks(Integer forks) {
this.forks = forks;
}
}
ApiUtil.java
public static final String BASE_URL = "https://api.github.com/users/";
public static Api getApi() {
return RetrofitClient.getClient(BASE_URL).create(Api.class);
}
RetrofitClient.java
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Salut, désolé, a eu un certain décalage de code dans ma question. J'ai mis à jour quelques champs. Pouvez-vous s'il vous plaît regarder à nouveau? Avec '' une autre option '' j'obtiens l'erreur: 'java.lang.NullPointerException: Tentative d'appeler la méthode d'interface 'int java.util.List.size()' sur une référence d'objet nul à com.motivecodex. githubusers.adapter.GithubRepositoriesAdapter.getItemCount (GithubRepositoriesAdapter.java:62) 'qui se trouve à' return githubRepositoryList.size(); ' – MOTIVECODEX
mGithubAdapter = new GithubRepositoriesAdapter ((Liste) response.body()); Assurez-vous également d'obtenir la liste des données dans response.body() ... et non null –
Shmuel
Tous ResponseBody dans ENQUEUE doit être List. Changez-le en 4 endroits. –
Shmuel