gpt4 book ai didi

android - 如何用他们的个人资料图片、姓名和 ID 查找或获取 facebook 的所有好友列表

转载 作者:搜寻专家 更新时间:2023-11-01 07:37:42 27 4
gpt4 key购买 nike

我在一个应用程序中工作,我必须在其中从 Facebook 获取所有好友列表。

我已经实现了这样的代码:

Facebook mFacebook = new Facebook("141118172662401");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
Bundle params = new Bundle();
params.putString("fields", "name,id");
mAsyncRunner.request("me/friends", params, "GET", new FriendListRequestListener(), null);
}
});
}

public class FriendListRequestListener {
public void onComplete(final String response) {
_error = null;
try {
JSONObject json = Util.parseJson(response);
final JSONArray friends = json.getJSONArray("data");
FacebookFriendsActivity.this.runOnUiThread(new Runnable() {
JSONObject data;
public void run() {
try{

try {
data = Util.parseJson(response);
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONArray friendsData = data.getJSONArray("data");
// mDbAdapter.deleteAllFriends();

for (int i = 0; i < friendsData.length(); i++) {
JSONObject friend = friendsData.getJSONObject(i);
/* mDbAdapter.addFriend(friend.getString("name"),
friend.getString("id"));*/
}
}catch (Exception e) {
// TODO: handle exception
}
}
});

} catch (JSONException e) {
_error = "JSON Error in response";
} catch (FacebookError e) {
_error = "Facebook Error: " + e.getMessage();
}

if (_error != null)
{
FacebookFriendsActivity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Error occurred: " +
_error, Toast.LENGTH_LONG).show();
}
});
}
}
}

在这一行抛出错误:

mAsyncRunner.request("me/friends", params, "GET", new FriendListRequestListener(), null);

这有什么问题吗?还添加了异步类..

如果有任何其他方法或来源可以找到 Facebook 好友,请告诉我。

最佳答案

用这个来获取好友列表。

    public class FriendsList extends Activity implements OnItemClickListener 
{

private Handler mHandler;
protected ListView friendsList;
protected static JSONArray jsonArray;
protected String graph_or_fql;

/*
* Layout the friends' list
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mHandler = new Handler();
setContentView(R.layout.friends_list);

Bundle extras = getIntent().getExtras();
String apiResponse = extras.getString("API_RESPONSE");
graph_or_fql = extras.getString("METHOD");
try {
if(graph_or_fql.equals("graph")) {
jsonArray = new JSONObject(apiResponse).getJSONArray("data");
} else {
jsonArray = new JSONArray(apiResponse);
}
} catch (JSONException e) {
showToast("Error: " + e.getMessage());
return;
}
friendsList = (ListView)findViewById(R.id.friends_list);
friendsList.setOnItemClickListener(this);
friendsList.setAdapter(new FriendListAdapter(this));

showToast(getString(R.string.can_post_on_wall));
}

/*
* Clicking on a friend should popup a dialog for user to post on friend's wall.
*/

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
try {
final long friendId;
if(graph_or_fql.equals("graph")) {
friendId = jsonArray.getJSONObject(position).getLong("id");
} else {
friendId = jsonArray.getJSONObject(position).getLong("uid");
}
String name = jsonArray.getJSONObject(position).getString("name");


new AlertDialog.Builder(this)
.setTitle(R.string.post_on_wall_title)
.setMessage(String.format(getString(R.string.post_on_wall), name))
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Bundle params = new Bundle();
/*
* Source Tag: friend_wall_tag
* To write on a friend's wall, provide friend's UID in the 'to' parameter.
* More info on feed dialog - https://developers.facebook.com/docs/reference/dialogs/feed/
*/
params.putString("to", String.valueOf(friendId));
params.putString("caption", getString(R.string.app_name));
params.putString("description", getString(R.string.app_desc));
params.putString("picture", Utility.HACK_ICON_URL);
params.putString("name", getString(R.string.app_action));
Utility.mFacebook.dialog(FriendsList.this, "feed", params, new PostDialogListener());
}

})
.setNegativeButton(R.string.no, null)
.show();
} catch (JSONException e) {
showToast("Error: " + e.getMessage());
}
}

/*
* Callback after the message has been posted on friend's wall.
*/
public class PostDialogListener extends BaseDialogListener {
public void onComplete(Bundle values) {
final String postId = values.getString("post_id");
if (postId != null) {
showToast("Message posted on the wall.");
} else {
showToast("No message posted on the wall.");
}
}
}

public void showToast(final String msg) {
mHandler.post(new Runnable() {
public void run() {
Toast toast = Toast.makeText(FriendsList.this, msg, Toast.LENGTH_LONG);
toast.show();
}
});
}

/**
* Definition of the list adapter
*/
public class FriendListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
FriendsList friendsList;

public FriendListAdapter(FriendsList friendsList) {
this.friendsList = friendsList;
if(Utility.model == null) {
Utility.model = new FriendsGetProfilePics();
}
Utility.model.setListener(this);
mInflater = LayoutInflater.from(friendsList.getBaseContext());
}

@Override
public int getCount() {
return jsonArray.length();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(position);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
View hView = convertView;
if(convertView == null) {
hView = mInflater.inflate(R.layout.friend_item, null);
ViewHolder holder = new ViewHolder();
holder.profile_pic = (ImageView)hView.findViewById(R.id.profile_pic);
holder.name = (TextView) hView.findViewById(R.id.name);
holder.info = (TextView) hView.findViewById(R.id.info);
hView.setTag(holder);
}

ViewHolder holder = (ViewHolder) hView.getTag();
try {
if(graph_or_fql.equals("graph")) {
holder.profile_pic.setImageBitmap(Utility.model.getImage(jsonObject.getString("id"), jsonObject.getString("picture")));
} else {
holder.profile_pic.setImageBitmap(Utility.model.getImage(jsonObject.getString("uid"), jsonObject.getString("pic_square")));
}
} catch (JSONException e) {
holder.name.setText("");
}
try {
holder.name.setText(jsonObject.getString("name"));
} catch (JSONException e) {
holder.name.setText("");
}
try {
if(graph_or_fql.equals("graph")) {
holder.info.setText(jsonObject.getJSONObject("location").getString("name"));
} else {
JSONObject location = jsonObject.getJSONObject("current_location");
holder.info.setText(location.getString("city") + ", " + location.getString("state"));
}

} catch (JSONException e) {
holder.info.setText("");
}
return hView;
}

}


class ViewHolder {
ImageView profile_pic;
TextView name;
TextView info;
}
}

还有 FriendsGetProfilePics

   public class FriendsGetProfilePics
{


Hashtable<String, Bitmap> friendsImages;
Hashtable<String, String> positionRequested;
BaseAdapter listener;
int runningCount = 0;
Stack<ItemPair> queue;

/*
* 15 max async tasks at any given time.
*/
final int MAX_ALLOWED_TASKS = 15;

public FriendsGetProfilePics() {
friendsImages = new Hashtable<String, Bitmap>();
positionRequested = new Hashtable<String, String>();
queue = new Stack<ItemPair>();
}

/*
* Inform the listener when the image has been downloaded.
* listener is FriendsList here.
*/
public void setListener(BaseAdapter listener) {
this.listener = listener;
reset();
}

public void reset() {
positionRequested.clear();
runningCount = 0;
queue.clear();
}

/*
* If the profile pictore has already been downloaded and cached, return it
* else excecute a new async task to fetch it -
* if total async tasks >15, queue the request.
*/
public Bitmap getImage(String uid, String url) {
Bitmap image = (Bitmap)friendsImages.get(uid);
if(image != null) {
return image;
}
if(!positionRequested.containsKey(uid)) {
positionRequested.put(uid, "");
if (runningCount >= MAX_ALLOWED_TASKS) {
queue.push(new ItemPair(uid, url));
} else {
runningCount++;
new GetProfilePicAsyncTask().execute(uid, url);
}
}
return null;
}

public void getNextImage() {
if(!queue.isEmpty()) {
ItemPair item = (ItemPair)queue.pop();
new GetProfilePicAsyncTask().execute(item.uid, item.url);
}
}

/*
* Start a AsyncTask to fetch the request
*/
private class GetProfilePicAsyncTask extends AsyncTask<Object, Void, Bitmap> {
String uid;
protected Bitmap doInBackground(Object... params) {
this.uid = (String)params[0];
String url = (String)params[1];
return Utility.getBitmap(url);
}

protected void onPostExecute(Bitmap result) {
runningCount--;
if(result != null) {
friendsImages.put(uid, result);
listener.notifyDataSetChanged();
getNextImage();
}
}
}

class ItemPair {
String uid;
String url;

public ItemPair(String uid, String url) {
this.uid = uid;
this.url = url;
}
}

}

关于android - 如何用他们的个人资料图片、姓名和 ID 查找或获取 facebook 的所有好友列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8454882/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com