Android 为什么mSuggestedFriends为空?
我正在开发一个android应用程序,它使用谷歌云端点作为后端。后端工作,可以由android客户端调用 在FindFriendFragment中,我调用endpoints异步任务类Android 为什么mSuggestedFriends为空?,android,google-app-engine,google-cloud-endpoints,Android,Google App Engine,Google Cloud Endpoints,我正在开发一个android应用程序,它使用谷歌云端点作为后端。后端工作,可以由android客户端调用 在FindFriendFragment中,我调用endpoints异步任务类 new EndpointTask.GetSuggestedFriends(mUserId, getContext()); 它通过公开的端点api从数据存储中获取建议的好友,并将其发送回客户端 return mApi.getSuggestedFriends().execute().getItems(); 在on-
new EndpointTask.GetSuggestedFriends(mUserId, getContext());
它通过公开的端点api从数据存储中获取建议的好友,并将其发送回客户端
return mApi.getSuggestedFriends().execute().getItems();
在on-post-execute中,我使用一个接口
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);
}
fetchedSuggestedFriends上的公共接口{
void sendSuggestedFriends(列出suggestedFriends);
}
@凌驾
受保护的void onPostExecute(列表配置文件){
super.onPostExecute(profiles);
OnFetchedSuggestedFriends回调=(OnFetchedSuggestedFriends)mContext;
callback.sendSuggestedFriends(配置文件);
}
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;
}
公共类FindFriendFragment扩展了片段
实现EndpointTask.GetSuggestedFriends.OnFetchedSuggestedFriends
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u find\u friend,container,false);
mAuth=FirebaseAuth.getInstance();
FirebaseUser=mAuth.getCurrentUser();
如果(用户!=null)
mUserId=user.getToken(true.toString();
新建EndpointTask.GetSuggestedFriends(mUserId,getContext());
RecyclerView suggestedFriendRecyclerView=(RecyclerView)视图。findViewById(R.id.suggested\u friends\u recycler\u视图);
suggestedFriendRecyclerView.setHasFixedSize(true);
LinearLayoutManager mllayoutmanager=新的LinearLayoutManager(getContext());
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
suggestedFriendRecyclerView.setLayoutManager(mLayoutManager);
SuggestedFriendAdapter=新建SuggestedFriendAdapter(msSuggestedFriends,mUserId);
suggestedFriendRecyclerView.setAdapter(适配器);
返回视图;
}
@凌驾
public void sendSuggestedFriends(列出suggestedFriends){
msSuggestedFriends=suggestedFriends;
}
但是,建议的朋友字段为空,即使当我使用api资源管理器测试后端时,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);
}
}
公共静态类GetSuggestedFriends扩展异步任务{
私人银行;
私人字符串库;
私密的自荐朋友;
公共GetSuggestedFriends(字符串用户ID,OnFetchedSuggestedFriends侦听器){
mUserId=userId;
mListener=监听器;
}
公共接口OnFetchedSuggestedFriends{
void sendSuggestedFriends(列出suggestedFriends);
}
@凌驾
受保护列表doInBackground(无效…参数){
如果(mApi==null){
BirthpayApi.Builder=new BirthpayApi.Builder(AndroidHttp.newCompatibleTransport(),
新的AndroidJsonFactory(),null)
//针对本地devappserver运行的选项
//-10.0.2.2是Android emulator中本地主机的IP地址
//-在本地devappserver上运行时关闭压缩
.setRootUrl(“http://10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(新的GoogleClientRequestInitializer(){
@凌驾
public void initialize(AbstractGoogleClientRequest AbstractGoogleClientRequest)引发IOException{
abstractGoogleClientRequest.setDisablegzip内容(true);
}
});
mApi=builder.build();
}
试一试{
返回mApi.getSuggestedFriends().execute().getItems();
}捕获(IOE异常){
e、 printStackTrace();
返回null;
}
}
@凌驾
受保护的void onPostExecute(列表配置文件){
super.onPostExecute(profiles);
mListener.sendSuggestedFriends(个人资料);
}
}
您好,您没有通知您的适配器检查以下代码是否有效:-
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();
}
new EndpointTask.GetSuggestedFriends(mUserId, getActivity(),FindFriendFragment.this).execute();
**下面是所需上下文的新代码行**
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);
}
这似乎不起作用,由于mSuggestedFriends==null,片段仍然为空;打印登录sendSuggestedFriends方法您得到了什么@Tomfine你想让我登录suggestedFriends字段吗@Hardyow,它不会被调用。Log语句没有被调用,难怪mSuggestedFriends为null,但为什么呢@Hardy@TomFinet检查我的最后一行请发布您的asynctask代码检查我更新的最后一行,如果它不工作,我们可以再次尝试:)@Tom FinetI刚刚忘记添加.execute()。谢谢@HardyNow mSuggestedFriends不为null且具有正确的值,但是回收器视图未显示数据@这是因为最初mSuggestedFriends为null,之后在使用适配器创建回收器视图时设置它。尽管我调用mAdapter.notifyDataSetChanged();没有任何更改,适配器不会用新数据更新@能吃苦耐劳的
new EndpointTask.GetSuggestedFriends(mUserId, getActivity(),FindFriendFragment.this).execute();