我正在开发一个使用谷歌云端点作为后端的android应用程序。后端工作,可以由android客户端调用。为什么mSuggestedFriends为null?
在FindFriendFragment中,我称之为端点异步任务类;
new EndpointTask.GetSuggestedFriends(mUserId, getContext());
从经由暴露的端点API数据存储区获取所建议的朋友,并将它们发送回客户端;
return mApi.getSuggestedFriends().execute().getItems();
在on post执行我使用一个接口;
public interface OnFetchedSuggestedFriends {
void sendSuggestedFriends(List<Profile> suggestedFriends);
}
@Override
protected void onPostExecute(List<Profile> profiles) {
super.onPostExecute(profiles);
OnFetchedSuggestedFriends callback = (OnFetchedSuggestedFriends) mContext;
callback.sendSuggestedFriends(profiles);
}
其中FindFriendFragment实现发送建议的朋友回到要使用的片段;
public class FindFriendFragment extends Fragment
implements EndpointTask.GetSuggestedFriends.OnFetchedSuggestedFriends
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_find_friend, container, false);
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
if (user != null)
mUserId = user.getToken(true).toString();
new EndpointTask.GetSuggestedFriends(mUserId, getContext());
RecyclerView suggestedFriendRecyclerView = (RecyclerView) view.findViewById(R.id.suggested_friends_recycler_view);
suggestedFriendRecyclerView.setHasFixedSize(true);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
suggestedFriendRecyclerView.setLayoutManager(mLayoutManager);
SuggestedFriendAdapter adapter = new SuggestedFriendAdapter(mSuggestedFriends, mUserId);
suggestedFriendRecyclerView.setAdapter(adapter);
return view;
}
@Override
public void sendSuggestedFriends(List<Profile> suggestedFriends) {
mSuggestedFriends = suggestedFriends;
}
但是建议朋友字段为空,即使当我测试用API Explorer作为预期getSuggestedFriends方法返回suggestedFriends后端。这是由于API调用需要很多时间吗?
编辑:
这里是异步任务代码:
public static class GetSuggestedFriends extends AsyncTask<Void, Void, List<Profile>>{
private BirthpayApi mApi;
private String mUserId;
private OnFetchedSuggestedFriends mListener;
public GetSuggestedFriends(String userId, OnFetchedSuggestedFriends listener) {
mUserId = userId;
mListener = listener;
}
public interface OnFetchedSuggestedFriends {
void sendSuggestedFriends(List<Profile> suggestedFriends);
}
@Override
protected List<Profile> doInBackground(Void... params) {
if (mApi == null) {
BirthpayApi.Builder builder = new BirthpayApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("http://10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
mApi = builder.build();
}
try{
return mApi.getSuggestedFriends().execute().getItems();
} catch(IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(List<Profile> profiles) {
super.onPostExecute(profiles);
mListener.sendSuggestedFriends(profiles);
}
}
请发布您的asynctask代码 – Hardy
它完成。 @Hardy –
检查我更新的最后一行,现在如果它不工作,那么我们可以再次尝试:) @Tom Finet – Hardy