我正在開發一個使用谷歌雲端點作爲後端的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