我使用方法loadUserName()在loadFriends()用户名中发出请求 但没有任何反应,第二个请求(loadUserName())仅针对第一个通道触发。我有这个片段的日志和代码 另外,AS告诉我,列表“名称”is never assigned,但是我不填写方法loadUserNames()吗?这与我的问题有关(不是调用onResponse和onFailure吗?) 其他方法和请求工作正常
public class FriendsFragment extends Fragment {
private String sid;
private PenyokService friendsService;
private RecyclerView friendsRecyclerView;
private FriendsAdapter friendsAdapter;
private List<Integer> ids;
private List<String> names;
public FriendsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
friendsRecyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_stats, container, false);
// view = getView();
sid = ((MainActivity) getActivity()).getSid();
friendsService = APIUtils.getPService();
friendsRecyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_stats, container, false);
friendsAdapter = new FriendsAdapter(new ArrayList<Integer>(0), new ArrayList<String>(0), getActivity().getApplicationContext(), sid);
friendsRecyclerView.setAdapter(friendsAdapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
friendsRecyclerView.setLayoutManager(layoutManager);
loadFriends();
return friendsRecyclerView;
}
private void loadFriends(){
friendsService.getFriends(sid).enqueue(new Callback<Model>() {
@Override
public void onResponse(Call<Model> call, Response<Model> response) {
if(response.isSuccessful()) {
ids = response.body().getMyFriends();
Log.d("testtest", "start load names");
for (int i : ids)
Log.d("test", "loaded names " + i);
loadUserName(ids);
Log.d("testtest", "loaded names");
friendsAdapter.updateFriends(response.body().getMyFriends(), names);
// Toast.makeText(getActivity(), "Gav gav", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Model> call, Throwable t) {
//showErrorMessage();
Log.d("testtest", t.toString());
Toast.makeText(getActivity(), t.toString(), Toast.LENGTH_SHORT).show();
}
});
}
private void loadUserName(List<Integer> id) {
Log.d("test", "loading names");
for (int x : id) {
Log.d("testtest", "after for " + x);
friendsService.getUserName(x, sid).enqueue(new Callback<Model>() {
@Override
public void onResponse(Call<Model> call, Response<Model> response) {
Log.d("testtest", "before response is successful");
if (response.isSuccessful()) {
Log.d("testtest", "response is successful");
if (response.body().getNameFor() != null) {
names.add(response.body().getNameFor());
Log.d("testtest", "in onResponse");
}
else {
names.add("Set your name");
Log.d("testtest", "we set name");
}
}
}
@Override
public void onFailure(Call<Model> call, Throwable t) {
//showErrorMessage();
Log.d("testtest", t.toString() + "loadUserName");
// Toast.makeText(getActivity(), t.toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
}
09-07 15:35:30.993 6238-6238/com.journaldev.navigationdrawer D/testtest: start load names
09-07 15:35:30.993 6238-6238/com.journaldev.navigationdrawer D/test: loaded names 1
09-07 15:35:30.993 6238-6238/com.journaldev.navigationdrawer D/test: loaded names 2
09-07 15:35:30.993 6238-6238/com.journaldev.navigationdrawer D/test: loaded names 4
09-07 15:35:30.993 6238-6238/com.journaldev.navigationdrawer D/test: loading names
09-07 15:35:30.993 6238-6238/com.journaldev.navigationdrawer D/testtest: after for 1
09-07 15:35:31.053 6238-6238/com.journaldev.navigationdrawer D/testtest: after for 2
09-07 15:35:31.063 6238-6238/com.journaldev.navigationdrawer D/testtest: after for 4
09-07 15:35:31.103 6238-6238/com.journaldev.navigationdrawer D/testtest: loaded names
答案 0 :(得分:0)
改装请求后的代码很快就会启动。它的启动速度快于服务器的响应速度 解决方案 - 考虑调用方法的结构(获得完整响应 - >开始下一个方法)
答案 1 :(得分:0)
永远不会分配names
变量我们还没有创建列表的实例。如果您尝试向列表添加任何当前为null的内容将导致NullPointerException
。我会建议将列表初始化为
private List<String> names=new ArrayList<>();