我正在使用 facebook api 来做一个 android 应用程序。它要求用户选择朋友来查看标记的照片。(也就是说,如果用户选择两个名字 A 和 B -> 他应该能够在一张照片中看到 A 和 B)。为了务实地做到这一点,我使用了图形 API 资源管理器,但发现很难提出请求调用。请参考图片:
对于我的查询,应使用上述信息。也就是说,当我提供“id(userID)”时,我需要获取“图片”..
请告诉我这是怎么做的!
这涉及到五个步骤:
第1步:
登录脸书:
private void loginToFacebook()
{
mPrefs = activity.getPreferences(Context.MODE_PRIVATE);
facebook = new Facebook(APP_ID);
accessToken = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (accessToken != null)
{
facebook.setAccessToken(accessToken);
uploadPhoto(facebook, imageBitmap, null);
}
if (expires != 0)
{
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid())
{
facebook.authorize(activity, new String[] {"publish_stream"}, Facebook.FORCE_DIALOG_AUTH, new DialogListener() {
public void onFacebookError(FacebookError arg0) {
// TODO Auto-generated method stub
}
public void onError(DialogError arg0) {
// TODO Auto-generated method stub
}
public void onComplete(Bundle arg0) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
uploadPhoto(facebook, imageBitmap, null);
}
public void onCancel() {
// TODO Auto-generated method stub
}
});
}
}
第2步:
上传图片到Facebook(上传位图),注意这个方法是在login方法里面调用的。Following 方法返回上传图片的 ID,因此在标记朋友时需要这样做:
public String uploadPhoto(Facebook facebook, Bitmap image, String albumId)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
if ( albumId == null ) {
albumId = "me";
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
image.compress(CompressFormat.JPEG, 75, bos);
byte[] photoBytes = bos.toByteArray();
Bundle params = new Bundle();
params.putString(facebook.TOKEN, accessToken);
params.putByteArray("picture", photoBytes);
try {
@SuppressWarnings("deprecation")
String resp = facebook.request(albumId + "/photos", params, "POST");
JSONObject json = Util.parseJson(resp);
if(json.getString("id") != null){
Toast.makeText(activity, "Image Successfully uploaded to facebook!", Toast.LENGTH_SHORT).show();
tagFriends();
}else{
Toast.makeText(activity, "Uploading failed, please try again!", Toast.LENGTH_SHORT).show();
}
uploadedImageID = json.getString("id");
return json.getString("id");
} catch ( IOException e ) {
Toast.makeText(activity, "Uploading failed, please try again!", Toast.LENGTH_SHORT).show();
} catch ( FacebookError e ) {
Toast.makeText(activity, "Uploading failed, please try again!", Toast.LENGTH_SHORT).show();
} catch ( JSONException e ) {
Toast.makeText(activity, "Uploading failed, please try again!", Toast.LENGTH_SHORT).show();
}
return null;
}
第 3 步:
获取您 Facebook 帐户的所有朋友并存储在哈希图中:
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, accessToken);
mAsyncRunner.request("me/friends" , params , "GET", new RequestListener()
{
@Override
public void onMalformedURLException(MalformedURLException e, Object state)
{
}
@Override
public void onIOException(IOException e, Object state)
{
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state)
{
}
@Override
public void onFacebookError(FacebookError e, Object state)
{
}
@Override
public void onComplete(String response, Object state)
{
try
{
JSONObject responseJsonObject = new JSONObject(response);
Log.d("InviteFriends.inviteFriends().new RequestListener() {...}:onComplete", "FB Response =>"+ responseJsonObject);
//event_id = event.getString("id");
//JSONObject responseJsonObject = new JSONObject(eventResponse);
JSONArray jsonArray = responseJsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = jsonArray.getJSONObject(i);
map.put("id", e.getString("id"));
map.put("name", e.getString("name"));
mylist.add(map);
userIds = e.getString("id");
userName = e.getString("name");
Log.d("MainActivity:getAllEvents", "Friend ID, Name:" + userIds + "," + userName);
}
}
catch (Exception e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
第4步:
在列表视图中显示名称列表,并从列表中捕获用户触摸的用户名的用户 ID,并将它们存储在数组中:
final ListView friends = (ListView) findViewById(R.id.lst_frnds);
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), mylist , R.layout.friends_main, new String[] {"name" }, new int[] {R.id.item_title});
Log.d( "size","Friends->" + mylist.size());
friends.setAdapter(adapter);
friends.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) friends.getItemAtPosition(position);
String selectedName = o.get("name");
String selectedId = o.get("id");
Toast.makeText(MemeList.this, "Your friend " + selectedName + "' was selected.", Toast.LENGTH_SHORT).show();
selectedFriendsIDs.add(selectedId);
selectedFriendsNames.add(selectedName);
}
第 5 步:
标记从列表中选择的朋友:
btnSendInvites.setText(selectedFriendsIDs.size() + " selected, tag them now?");
btnSendInvites.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Tagging friends
for(int i = 0; i < selectedFriendsIDs.size(); i++){
String inputParm = uploadedImageID + "/tags?to=" + selectedFriendsIDs.get(i);
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, accessToken);
mAsyncRunner.request(inputParm, params, "POST", new RequestListener()
{
@Override
public void onMalformedURLException(MalformedURLException e, Object state)
{
}
public void onIOException1(IOException e, Object state)
{
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state)
{
}
@Override
public void onFacebookError(FacebookError e, Object state)
{
}
@Override
public void onComplete(String response, Object state)
{
Log.d("Selected IDs ", "" + selectedFriendsIDs);
}
@Override
public void onIOException(IOException e, Object state)
{
// TODO Auto-generated method stub
}
}, null);
}
}
});
如果成功标记,将返回 true。
希望这对某人有帮助!