2017-10-08 5 views
0

Retrofit2を使用してJSON形式でデータを取得し、RecyclerViewアダプタに解析しようとしていますが、何をするにしてもこの機能を実現する方法がわかりません。Retrofit2 HTTP RecyclerViewにJsonを取得

現時点では、ローカルのDBFlowデータで1つのRecyclerViewを読み込むことはできますが、2番目のRecyclerViewでRetrofit2を使用してHTTPで取得したJSONの処理方法を把握することはできません。 JSONをログに読み込む以下のコードがあります。しかし、それを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(); 
    } 
} 

インタフェースApi.java:

@GET("users/{user}/repos") 
Call<List<GithubRepository>> listRepos(@Path("user") String user); 

モデル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; 
    } 
} 

答えて

1

(これは約私はあなたのタイプを見ることができないとして):

localRepositoryList.addAll(response.body()); 
adapter.notifyDataSetChanged(); 

PS:私は仮定コードには次のようなコードが含まれています:

GithubRepositoriesAdapter adapter=new GithubRepositoriesAdapter(localRepositoryList); 
mGithubRecyclerView.setAdapter(adapter); 
ログの場所で617​​

別のオプション

mGithubRecyclerView.setAdapter(new GithubRepositoriesAdapter(response.body())); 

UPDATE:

public void loadGithubUserRepositoryJson() { 
    api.listRepos("motivecodex").enqueue(new Callback<List<GithubRepository>>() { 
    @Override 
    public void onResponse(Call<List<GithubRepository>> call, Response<List<GithubRepository>> response) { 
     try { 
      Log.e(LOG_TAG, response.body().string()); 

      mGithubAdapter = new GithubRepositoriesAdapter(response.body()); 
      mGithubRecyclerView.setAdapter(mGithubAdapter); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onFailure(Call<List<GithubRepository>> call, Throwable t) { 
     Log.e("MainActivity", "error loading from API"); 
    } 
}); 
} 
+0

こんにちは、申し訳ありませんが、私の質問のコードのいくつかの不一致がありました。いくつかのフィールドを更新しました。もう一度見ていただけますか? 「別のオプション」でエラーが発生しました。 java.lang.NullPointerException:com.motivecodexのヌルオブジェクト参照で のインタフェースメソッド 'int java.util.List.size()'を呼び出そうとしました。 – MOTIVECODEX

+1

mGithubAdapter = new GithubRepositoriesAdapter((List )response.body());返信githubRepositoryList.size();にあるgithubusers.adapter.GithubRepositoriesAdapter.getItemCount(GithubRepositoriesAdapter.java:62) また、response.body()...のデータを含むリストを取得していて、ヌルでないことを確認してください。 – Shmuel

+1

ENQUEUEのすべてのResponseBodyがList です。 4か所で変更してください。 – Shmuel

関連する問題