检索仅在我的设备上工作的Facebook好友

时间:2014-03-10 15:59:23

标签: android facebook-fql facebook-android-sdk restfb

我正在开发一个应用程序,用于检索当前用户的Facebook好友。我的代码正在我的设备上运行但是当我在任何其他设备上运行时,它没有显示任何结果。

你可以告诉我出错的地方吗?

我的代码如下:

public class FaceBook extends Activity implements SearchView.OnQueryTextListener, SearchView.OnCloseListener{

        ArrayList<Param> facebook_names = new ArrayList<Param>();
        Param name = null;
        Names_Adapter adapter;
        ListView Names_List;
        SearchView searchView;
        int check_search =1;
        ArrayList<Param>new_names;
        private UiLifecycleHelper uiHelper;
        private static final int REAUTH_ACTIVITY_CODE = 100;



        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_face_book);

            ActionBar bar = getActionBar();
            bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            bar.setCustomView(R.layout.custom_titlebar);
            bar.setBackgroundDrawable(getResources().getDrawable(R.drawable.header));
            //bar.setTitle("Main Menu");
            bar.setDisplayShowHomeEnabled(true);  
            bar.setHomeButtonEnabled(true);

            uiHelper = new UiLifecycleHelper(this, callback);
            uiHelper.onCreate(savedInstanceState);

            TextView myTitleText = (TextView) findViewById(R.id.mytext);
            if ( myTitleText != null ) {
                myTitleText.setText("Facebook");
            }

            Log.d("COMP", "TEST");


             Names_List = (ListView) findViewById(R.id.listView1);


             ImageButton reload = (ImageButton) findViewById(R.id.reload);
             reload.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent restart  = new Intent(FaceBook.this, FaceBook.class);
                    startActivity(restart);
                    finish();
                }
            });



        }

              @Override
              public void onActivityResult(int requestCode, int resultCode, Intent data) {
                  super.onActivityResult(requestCode, resultCode, data);
                  if (requestCode == REAUTH_ACTIVITY_CODE) {
                      uiHelper.onActivityResult(requestCode, resultCode, data);
                  }
              }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.face_book, menu);
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            searchView = (SearchView) findViewById(R.id.searchView1);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            // TODO Auto-generated method stub
            if(!query.isEmpty())
            {

                check_search = 0;
                new_names = new ArrayList<Param>();
                for(int i=0;i<facebook_names.size();i++)
                {
                    if(facebook_names.get(i).getName().toLowerCase().startsWith(query))
                    {
                        Param new_param = new Param(facebook_names.get(i).getUrl(),facebook_names.get(i).getName(),facebook_names.get(i).getPic());
                        new_names.add(new_param);
                        adapter.notifyDataSetChanged();


                    }
                }

                 Names_Adapter adapter = new Names_Adapter(FaceBook.this,R.layout.facebook_names_list,new_names);
                        Names_List.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
            }
            else
            {
                check_search=1;
                 adapter = new Names_Adapter(FaceBook.this,R.layout.facebook_names_list,facebook_names);
                    Names_List.setAdapter(adapter);
            }

            return false;
        }

        @Override
        public boolean onClose() {
            // TODO Auto-generated method stub
            Names_List.setAdapter(adapter);
            return false;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // TODO Auto-generated method stub

            int itemId = item.getItemId();
            switch (itemId) {
            case android.R.id.home:

                FaceBook.this.finish();
                break;
            }

            return super.onOptionsItemSelected(item);
        }

        private void makeRequest(Session session)
        {
            String fqlQuery = "SELECT uid,name,pic,profile_url FROM user WHERE uid IN " +
                        "(SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY first_name";
                Bundle params = new Bundle();
                params.putString("q", fqlQuery);
                session = Session.getActiveSession();

                Request request = new Request(session,
                        "/fql",                         
                        params,                         
                        HttpMethod.GET,                 
                        new Request.Callback(){       
                    public void onCompleted(Response response) {
                        Log.i("TAG", "Result: " + response.toString());

                        try{
                            GraphObject graphObject = response.getGraphObject();
                            JSONObject jsonObject = graphObject.getInnerJSONObject();
                            Log.d("data", jsonObject.toString(0));

                            JSONArray array = jsonObject.getJSONArray("data");
                            for(int i=0;i<array.length();i++){


                                JSONObject friend = array.getJSONObject(i);
                              name = new Param(friend.optString("profile_url"),friend.optString("name"),friend.optString("pic"));
                                facebook_names.add(name);
                             //   Log.d("uid",friend.getString("uid"));
                              //  Log.d("name", friend.getString("name"));
                                //Log.d("pic_square",friend.getString("pic_square")); 


                                  adapter = new Names_Adapter(FaceBook.this,R.layout.facebook_names_list,facebook_names);
                                    Names_List.setAdapter(adapter);

                                 searchView.setOnQueryTextListener(FaceBook.this);
                                searchView.setOnCloseListener(FaceBook.this);

                             Names_List.setOnItemClickListener(new OnItemClickListener() {

                                @Override
                                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                        long arg3) {
                                    // TODO Auto-generated method stub

                                    Intent intent = new Intent(FaceBook.this,Web_Activity.class);
                                    if(check_search==0)
                                    {
                                    intent.putExtra("WEB",new_names.get(arg2).getUrl());
                                    intent.putExtra("NAME",new_names.get(arg2).getName());
                                    }
                                    else
                                    {
                                        intent.putExtra("WEB",facebook_names.get(arg2).getUrl());   
                                        intent.putExtra("NAME",facebook_names.get(arg2).getName());
                                    }
                                    startActivity(intent);
                                }
                            });

                            }
                        }catch(JSONException e){
                            e.printStackTrace();
                        }


                    }                  
                }); 
                Request.executeBatchAsync(request); 
        }

        private void onSessionStateChange(final Session session, SessionState state, Exception exception) {
            if (session != null && session.isOpened()) {
                // Get the user's data.
                makeRequest(session);
            }
        }



        public void onResume() {
            super.onResume();
            Session session = Session.getActiveSession();
            if (session != null &&
                   (session.isOpened() || session.isClosed()) ) {
                onSessionStateChange(session, session.getState(), null);
            }

            uiHelper.onResume();
        }


         private Session.StatusCallback callback = new Session.StatusCallback() {
                @Override
                public void call(final Session session, final SessionState state, final Exception exception) {
                    onSessionStateChange(session, state, exception);
                }
            };


        @Override
        public void onSaveInstanceState(Bundle bundle) {
            super.onSaveInstanceState(bundle);
            uiHelper.onSaveInstanceState(bundle);
        }

        @Override
        public void onPause() {
            super.onPause();
            uiHelper.onPause();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            uiHelper.onDestroy();
        }
    }

我还尝试使用restFB来访问Facebook好友。

AccessToken accessToken =  new DefaultFacebookClient().obtainAppAccessToken(appid,app secret; 
    FacebookClient facebookClient = new DefaultFacebookClient(accessToken.getAccessToken());

User user = facebookClient.fetchObject("me", User.class);

当我这样做时,它给出了一个错误,指出必须使用有效的访问令牌来检索用户值。

1 个答案:

答案 0 :(得分:1)

转到Facebook开发人员网站,在您的应用的“状态和评论”下,将其设为 PUBLIC 。在“临时”测试完成后,您可以随时从公开中删除。

另一种方法是打印出您正在测试应用的每个设备的关键哈希值,然后将这些哈希值作为关键哈希值添加到您的应用的FB开发者的设置&gt; Android中。< / p>