2016-07-07 126 views
1

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

請發佈您的asynctask代碼 – Hardy

+1

它完成。 @Hardy –

+0

檢查我更新的最後一行,現在如果它不工作,那麼我們可以再次嘗試:) @Tom Finet – Hardy

回答

1

你好,你不通知你的適配器檢查,如果下面的代碼工作:----

​​
  • getContext()*在這裏檢查你的cont分機是否正在實現你的界面!

使用以下行: -

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

**下面的代碼新行FR你需要的情況下**

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(); 
+0

這似乎沒有工作,由於mSuggestedFriends == null,片段仍然是空白的; –

+0

打印登錄sendSuggestedFriends方法你在做什麼? @TomFinet – Hardy

+0

你想讓我Log.i()建議的朋友字段? @Hardy –