2011-02-05 2 views
-1

Привет, я пытаюсь сделать приложение, которое вытащит список зарегистрированных пользователей друзей из facebook в мое приложение. Я использую SDK для facebook, код компилируется и, кажется, работает, но висит на счетчике анимация, когда я выбираю параметр «получить друзей» в меню! Вот код, я использую нижеAndroid-проект wont pull facebook events

public static final String APP_ID = "IDHERE"; 

private static final String[] PERMISSIONS = 
    new String[]{ "offline_access", "read_stream", 
      "publish_stream","create_event","user_events","friends_events", 
      "publish_checkins", "friends_checkins" }; 

private TextView mText; 
private Handler mHandler = new Handler(); 
private ProgressDialog mSpinner; 
private final ArrayList<Friend> friends = new ArrayList<Friend>(); 
private FriendsArrayAdapter friendsArrayAdapter; 
private ListView listView; 
private Facebook mFacebook; 
private AsyncFacebookRunner mAsyncRunner; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Make sure the app client_app has been set 
    if (APP_ID == null) { 
     Util.showAlert(this, 
     "Warning", "Facebook Applicaton ID must be set..."); 
    } 

    // Initialize the content view 
    setContentView(R.layout.main); 
    // Get the status text line resource 
    mText = (TextView) workdammit.this.findViewById(R.id.txt); 

    // Setup the ListView Adapter that is loaded when selecting "get friends" 
    listView = (ListView) findViewById(R.id.friendsview); 
    friendsArrayAdapter = new FriendsArrayAdapter(this, R.layout.rowlayout, friends); 
    listView.setAdapter(friendsArrayAdapter); 

    // Define a spinner used when loading the friends over the network 
    mSpinner = new ProgressDialog(listView.getContext()); 
    mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    mSpinner.setMessage("Loading..."); 

    // Initialize the Facebook session 
    mFacebook = new Facebook(APP_ID); 
    mAsyncRunner = new AsyncFacebookRunner(mFacebook); 

} 

////////////////////////////////////////////////////////////////////// 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    Log.d("FB Sample App", "onActivityResult(): " + requestCode); 
    mFacebook.authorizeCallback(requestCode, resultCode, data); 
} 

////////////////////////////////////////////////////////////////////// 
// Get Friends request listener 
////////////////////////////////////////////////////////////////////// 

/** 
* FriendsRequestListener implements a request lister/callback 
* for "get friends" requests 
*/ 
public class FriendsRequestListener implements 
     com.facebook.android.AsyncFacebookRunner.RequestListener { 

    /** 
    * Called when the request to get friends has been completed. 
    * Retrieve and parse and display the JSON stream. 
    */ 
    public void onComplete(final String response) { 
     mSpinner.dismiss(); 
     try { 
      // process the response here: executed in background thread 
      Log.d("Facebook-Example-Friends Request", "response.length(): " + response.length()); 
      Log.d("Facebook-Example-Friends Request", "Response: " + response); 

      final JSONObject json = new JSONObject(response); 
      JSONArray d = json.getJSONArray("data"); 
      int l = (d != null ? d.length() : 0); 
      Log.d("Facebook-Example-Friends Request", "d.length(): " + l); 

      for (int i=0; i<l; i++) { 
       JSONObject o = d.getJSONObject(i); 
       String n = o.getString("name"); 
       String id = o.getString("id"); 
       Friend f = new Friend(); 
       f.id = id; 
       f.name = n; 
       friends.add(f); 
      } 

      // Only the original owner thread can touch its views 
      workdammit.this.runOnUiThread(new Runnable() { 
       public void run() { 
        friendsArrayAdapter = new FriendsArrayAdapter(
          workdammit.this, R.layout.rowlayout, friends); 
        listView.setAdapter(friendsArrayAdapter); 
        friendsArrayAdapter.notifyDataSetChanged(); 
       } 
      }); 
     } catch (JSONException e) { 
      Log.w("Facebook-Example", "JSON Error in response"); 
     } 
    } 



    @Override 
    public void onComplete(String response, Object state) { 
     mSpinner.dismiss(); 

    } 

    @Override 
    public void onIOException(IOException e, Object state) { 
     mSpinner.dismiss(); 

    } 

    @Override 
    public void onFileNotFoundException(FileNotFoundException e, 
      Object state) { 
     mSpinner.dismiss(); 

    } 

    @Override 
    public void onMalformedURLException(MalformedURLException e, 
      Object state) { 
     mSpinner.dismiss(); 

    } 

    @Override 
    public void onFacebookError(FacebookError e, Object state) { 
     mSpinner.dismiss(); 

    } 
} 

////////////////////////////////////////////////////////////////////// 
// Wall Post request listener 
////////////////////////////////////////////////////////////////////// 

/** 
* WallPostRequestListener implements a request lister/callback 
* for "wall post requests" 
*/ 
public class WallPostRequestListener implements 
     com.facebook.android.AsyncFacebookRunner.RequestListener { 

    /** 
    * Called when the wall post request has completed 
    */ 
    public void onComplete(final String response) { 
     Log.d("Facebook-Example", "Got response: " + response); 
    } 


    @Override 
    public void onComplete(String response, Object state) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onIOException(IOException e, Object state) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onFileNotFoundException(FileNotFoundException e, 
      Object state) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onMalformedURLException(MalformedURLException e, 
      Object state) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onFacebookError(FacebookError e, Object state) { 
     // TODO Auto-generated method stub 

    } 

} 

////////////////////////////////////////////////////////////////////// 
// Wall post dialog completion listener 
////////////////////////////////////////////////////////////////////// 

/** 
* WallPostDialogListener implements a dialog lister/callback 
*/ 
public class WallPostDialogListener implements 
     com.facebook.android.Facebook.DialogListener { 

    /** 
    * Called when the dialog has completed successfully 
    */ 
    public void onComplete(Bundle values) { 
     final String postId = values.getString("post_id"); 
     if (postId != null) { 
      Log.d("FB Sample App", "Dialog Success! post_id=" + postId); 
      mAsyncRunner.request(postId, new WallPostRequestListener()); 
     } else { 
      Log.d("FB Sample App", "No wall post made"); 
     } 
    } 

    @Override 
    public void onCancel() { 
     // No special processing if dialog has been canceled 
    } 

    @Override 
    public void onError(DialogError e) { 
     // No special processing if dialog has been canceled 
    } 

    @Override 
    public void onFacebookError(FacebookError e) { 
     // No special processing if dialog has been canceled 
    } 
} 

///////////////////////////////////////////////////////// 
// Login/Logout Listeners 
///////////////////////////////////////////////////////// 

/** 
* Listener for login dialog completion status 
*/ 
private final class LoginDialogListener implements 
     com.facebook.android.Facebook.DialogListener { 

    /** 
    * Called when the dialog has completed successfully 
    */ 
    public void onComplete(Bundle values) { 
     // Process onComplete 
     Log.d("FB Sample App", "LoginDialogListener.onComplete()"); 
     // Dispatch on its own thread 
     mHandler.post(new Runnable() { 
      public void run() { 
       mText.setText("Facebook login successful. Press Menu..."); 
      } 
     }); 
    } 

    /** 
    * 
    */ 
    public void onFacebookError(FacebookError error) { 
     // Process error 
     Log.d("FB Sample App", "LoginDialogListener.onFacebookError()"); 
    } 

    /** 
    * 
    */ 
    public void onError(DialogError error) { 
     // Process error message 
     Log.d("FB Sample App", "LoginDialogListener.onError()"); 
    } 

    /** 
    * 
    */ 
    public void onCancel() { 
     // Process cancel message 
     Log.d("FB Sample App", "LoginDialogListener.onCancel()"); 

} }

/** 
* Listener for logout status message 
*/ 
private class LogoutRequestListener implements RequestListener { 

    /** Called when the request completes w/o error */ 
    public void onComplete(String response) { 

     // Only the original owner thread can touch its views 
     workdammit.this.runOnUiThread(new Runnable() { 
      public void run() { 
       mText.setText("Thanks for using FB Sample App. Bye bye..."); 
       friends.clear(); 
       friendsArrayAdapter.notifyDataSetChanged(); 
      } 
     }); 

     // Dispatch on its own thread 
     mHandler.post(new Runnable() { 
      public void run() { 
      } 
     }); 
    } 


    @Override 
    public void onComplete(String response, Object state) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onIOException(IOException e, Object state) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onFileNotFoundException(FileNotFoundException e, 
      Object state) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onMalformedURLException(MalformedURLException e, 
      Object state) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onFacebookError(FacebookError e, Object state) { 
     // TODO Auto-generated method stub 

    } 

} 

/////////////////////////////////////////////////////////////////// 
// Menu handlers 
/////////////////////////////////////////////////////////////////// 

/** 
* Invoked at the time to create the menu 
* @param menu is the menu to create 
*/ 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.main_menu, menu); 
    return true; 
} 

/** 
* Invoked when preparing to display the menu 
* @param menu is the menu to prepare 
*/ 
@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 

    MenuItem loginItem = menu.findItem(R.id.login); 
    MenuItem postItem = menu.findItem(R.id.wallpost); 
    MenuItem getfriendsItem = menu.findItem(R.id.getfriends); 
    if (mFacebook.isSessionValid()) { 
     loginItem.setTitle("Logout"); 
     postItem.setEnabled(true); 
     getfriendsItem.setEnabled(true); 
    } else { 
     loginItem.setTitle("Login"); 
     postItem.setEnabled(false); 
     getfriendsItem.setEnabled(false); 
    } 
    loginItem.setEnabled(true); 
    return super.onPrepareOptionsMenu(menu); 
} 

/** 
* Invoked when a menu item has been selected 
* @param item is the selected menu items 
*/ 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 

     // Login/logout toggle 
     case R.id.login: 
      // Toggle the button state. 
      // If coming from login transition to logout. 
      if (mFacebook.isSessionValid()) { 
       AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFacebook); 
       asyncRunner.logout(this.getBaseContext(), new LogoutRequestListener()); 
      } else { 
       // Toggle the button state. 
       // If coming from logout transition to login (authorize). 
       mFacebook.authorize(this, PERMISSIONS, new LoginDialogListener()); 
      } 
      break; 

     // Wall Post 
     case R.id.wallpost: // Wall Post 
      mFacebook.dialog(workdammit.this, "stream.publish", new WallPostDialogListener()); 
      break; 

     // Get Friend's List 
     case R.id.getfriends: // Wall Post 
      // Get the authenticated user's friends 
      mSpinner.show(); 

      mAsyncRunner.request("me/friends", new FriendsRequestListener()); 
      break; 

     default: 
      return false; 

    } 
    return true; 
} 

}

есть также два других классов, которые работают с применением кода для них ниже, также

/** * ListView Друзья ArrayAdapter */ Публичный класс Друг sArrayAdapter расширяет ArrayAdapter { частный конечный контекст активности; частные конечные друзья ArrayList; private int resourceId;

/** 
* Constructor 
* @param context the application content 
* @param resourceId the ID of the resource/view 
* @param friends the bound ArrayList 
*/ 
public FriendsArrayAdapter(
     Activity context, 
     int resourceId, 
     ArrayList<Friend> friends) { 
    super(context, resourceId, friends); 
    this.context = context; 
    this.friends = friends; 
    this.resourceId = resourceId; 
} 

/** 
* Updates the view 
* @param position the ArrayList position to update 
* @param convertView the view to update/inflate if needed 
* @param parent the groups parent view 
*/ 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View rowView = convertView; 
    if (rowView == null) { 
     LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     rowView = vi.inflate(resourceId, null); 
    } 
    Friend f = friends.get(position); 
    TextView rowTxt = (TextView) rowView.findViewById(R.id.rowtext_top); 
    rowTxt.setText(f.name); 
    return rowView; 
} 

}

public class Friend { 
public String id; 
public String name; 
public byte[] picture; 
public Bitmap pictureBitmap;; 

}

любая помощь будет большим: D

+0

Пожалуйста, разместите действительно необходимые фрагменты кода. – CSchulz

ответ

1

Код выглядит хорошо!

Вот одна вещь, которую нужно попробовать ... проверьте, вызван ли ваш метод «@Override» onComplete() и что-то там. Например.

public class FriendsRequestListener implements 
     com.facebook.android.AsyncFacebookRunner.RequestListener { 

... 
... 
    @Override 
    public void onComplete(String response, Object state) { 
     mSpinner.dismiss(); 
     // Got a response... now process it 
     onComplete(response); 
    } 
+1

Я столкнулся с такой же проблемой. yes Вызывается метод Overridden onComplete(). –

+0

Мои переопределенные методы не называются .. но интерфейс facebook работает отлично – user591124

Смежные вопросы