这是我的代码: - [我得到的输出显示在下面的图片中。] [1] 主文件代码
但我希望输出如同在一个屏幕上显示两个用户的github回购和左侧的vikashumain(用户名)回购以及右侧的naman14(用户名)回购。但我只获得一个用户回购显示像vikashumain这样的方面 用户仅在双方显示
BASE_URL = https://api.github.com/
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
@BindView(R.id.re1)
RecyclerView recyclerView;
@BindView(R.id.re2)
RecyclerView recyclerView2;
View v;
List<MainCategoryData> list,list2;
private RepoAdapter adapter;
Retrofit retrofit;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v= inflater.inflate(R.layout.fragment_blank, container, false);
ButterKnife.bind(this,v);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView2.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager2 = new LinearLayoutManager(getContext());
recyclerView2.setLayoutManager(layoutManager2);
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
retrofit.create(ApiServiceMain.class).maincategorya()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleRespons, this:: handleError);
Observable.just(retrofit.create(ApiServiceMain.class)).subscribeOn(Schedulers.computation())
.flatMap(s -> {
Observable<List<MainCategoryData>> couponsObservable
= s.maincategorya().subscribeOn(Schedulers.io());
Observable<List<MainCategoryData>> storeInfoObservable
= s.maincategoryap().subscribeOn(Schedulers.io());
return Observable.merge(couponsObservable,storeInfoObservable);
}).observeOn(AndroidSchedulers.mainThread()).subscribe(this::handleRespons, this::handleError );
return v;
}
private void handleRespons(List<MainCategoryData> storeCoupons) {
list = new ArrayList<>(storeCoupons);
adapter = new RepoAdapter(list);
recyclerView.setAdapter(adapter);
list2 = new ArrayList<>(storeCoupons);//error
adapter = new RepoAdapter(list2);
recyclerView2.setAdapter(adapter);
}
public void handleError(Throwable error) {
Toast.makeText(getContext(), "Error "+error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
这是我的服务文件:-ApiService.java
public interface ApiServiceMain
{
//we used post here becz here we have parameters .Get and Post works in the same way but post has parameters and get doesn't have.
@GET("users/vikashumain/starred")
Observable<List<MainCategoryData>> maincategoryapi();
@GET("users/naman14/starred")
Observable<List<MainCategoryData>> maincategoryap();
@GET("users/vikashumain/starred")
Observable<List<MainCategoryData>> maincategorya();
}
模型(POJO)类: -
public class MainCategoryData {
public int id;
public String name;
private String html_url;
public String description;
public String language;
public int stargazers_count;
public MainCategoryData() {
}
public int getId() {
return id;
}
List<Vikashumain> data2;
public String getName() {
return name;
}
public List<Vikashumain> getData2() {
return data2;
}
public void setData2(List<Vikashumain> data2) {
this.data2 = data2;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setHtmlUrl(String html_url) {
this.html_url = html_url;
}
public void setDescription(String description) {
this.description = description;
}
public void setLanguage(String language) {
this.language = language;
}
public void setStargazersCount(int stargazers_count) {
this.stargazers_count = stargazers_count;
}
public String getHtmlUrl() {
return html_url;
}
public String getDescription() {
return description;
}
public String getLanguage() {
return language;
}
public int getStargazersCount() {
return stargazers_count;
}
public MainCategoryData(int id, String name, String html_url, String description, String language, int stargazers_count) {
this.id = id;
this.name = name;
this.html_url = html_url;
this.description = description;
this.language = language;
this.stargazers_count = stargazers_count;
}
}
https://i.stack.imgur.com/sga9A.jpg
我正在解析的json文件: - https://api.github.com/users/vikashumain/starred 我正在解析的第二个json文件: - https://api.github.com/users/naman14/starred
答案 0 :(得分:1)
目前尚不清楚您在此尝试实现的目标。你合并了两个用户&#39;数据放入一个列表并在两个视图上设置相同的列表。
由于handleRespons
:
retrofit.create(ApiServiceMain.class).maincategorya()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleRespons, this::handleError);
在此之后的某个时间,以下调用将生成2个列表,每个用户一个,并且handleRespons
被调用两次。较慢的用户数据将再次覆盖两个面板数据。
Observable.just(retrofit.create(ApiServiceMain.class))
.subscribeOn(Schedulers.computation())
.flatMap(s -> {
Observable<List<MainCategoryData>> couponsObservable
= s.maincategorya().subscribeOn(Schedulers.io());
Observable<List<MainCategoryData>> storeInfoObservable
= s.maincategoryap().subscribeOn(Schedulers.io());
return Observable.merge(couponsObservable,storeInfoObservable);
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleRespons, this::handleError);
您应该为每个面板实现两个独立的响应处理程序:
private void handleResponseLeft(List<MainCategoryData> storeCoupons) {
List<MainCategoryData> list = new ArrayList<>(storeCoupons);
RepoAdapter adapter = new RepoAdapter(list);
recyclerView.setAdapter(adapter);
}
private void handleResponseRight(List<MainCategoryData> storeCoupons) {
List<MainCategoryData> list2 = new ArrayList<>(storeCoupons);
RepoAdapter adapter2 = new RepoAdapter(list2);
recyclerView2.setAdapter(adapter2);
}
ApiServiceMain s = retrofit.create(ApiServiceMain.class);
s.maincategorya()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleResponseLeft, this::handleError);
s.maincategoryap()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleResponseRight, this::handleError);