为什么mSuggestedFriends为空?

时间:2016-07-07 08:47:36

标签: android google-app-engine google-cloud-endpoints

我正在开发一个使用谷歌云端点作为后端的Android应用程序。后端工作,可以由Android客户端调用。

在FindFriendFragment中,我调用一个端点异步任务类;

new EndpointTask.GetSuggestedFriends(mUserId, getContext());

通过公开的端点api从数据存储区中提取建议的朋友,并将它们发送回客户端;

return mApi.getSuggestedFriends().execute().getItems();

在on post 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;
    }

然而建议的friends字段为null,即使我使用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);
        }
    }

1 个答案:

答案 0 :(得分:1)

您好,您没有通知您的适配器检查以下代码是否有效:----

 public class FindFriendFragment extends Fragment
    implements EndpointTask.GetSuggestedFriends.OnFetchedSuggestedFriends
 SuggestedFriendAdapter adapter;
@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);

  adapter = new SuggestedFriendAdapter(mSuggestedFriends, mUserId);
    suggestedFriendRecyclerView.setAdapter(adapter);
    return view;
}

@Override
public void sendSuggestedFriends(List<Profile> suggestedFriends) {
    mSuggestedFriends = suggestedFriends;
adapter.notifyDatasetChanged();
}
  • getContext()*请在此处查看您的上下文是否正在实现您的界面!!!

请改用下面的行: -

new EndpointTask.GetSuggestedFriends(mUserId, FindFriendFragment.this);

**新的代码行下面是您需要的背景**

new EndpointTask.GetSuggestedFriends(mUserId, getActivity(),FindFriendFragment.this);

你的asynctask参数应该是!!!

EndpointTask.GetSuggestedFriends.OnFetchedSuggestedFriends listener;

并在构造函数中定义它,如....

GetSuggestedFriends(mUserId, getActivity(),

    EndpointTask.GetSuggestedFriends.OnFetchedSuggestedFriends listener_)
{
  listener=listener_}

你的帖子看起来像这样......

@Override
protected void onPostExecute(List<Profile> profiles) {
    super.onPostExecute(profiles);
    listener.sendSuggestedFriends(profiles);
}

:)最终试用版:

 new EndpointTask.GetSuggestedFriends(mUserId, getActivity(),FindFriendFragment.this).execute();