我首先将响应作为 JSON 响应获取,然后将结果存储在哈希数组中,并将选定的好友 ID 输入到如下所示的数组中:
private void inviteFriends()
{
try
{
Bundle params = new Bundle();
mAsyncRunner2.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());
}
runOnUiThread(new Runnable() {
public void run(){
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), mylist , R.layout.friends_main,
new String[] {"name" },
new int[] {R.id.item_title});
Log.d(
"InviteFriends.inviteFriends().new RequestListener() {...}.onComplete(...).new Runnable() {...}:run",
"Friends->" + mylist.size());
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(final AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
String selectedName = o.get("name");
String selectedId = o.get("id");
Toast.makeText(InviteFriends.this, "Your friend " + selectedName + " was selected.", Toast.LENGTH_SHORT).show();
selectedFriendsIDs.add(selectedId);
selectedFriendsNames.add(selectedName);
selectedFriendsCount = selectedFriendsIDs.size();
btnSendInvites.setText(selectedFriendsCount + " friends selected");
Log.d("Selected IDs ", "" + selectedFriendsIDs);
Log.d("Selected Names ", "" + selectedFriendsNames);
//make list null
}
});
}
});
}
}, null);
}
catch (Exception e)
{
}
然后我邀请朋友,在下面的 for 循环中一一邀请:
public void inviteSelectedFriends()
{
try
{
String createdEventID = CreateFbEvent.eventID.toString();
Log.d("InviteSelectedFriends:inviteSelectedFriends", "size -> " + InviteFriends.selectedFriendsIDs.size());
for(int i = 0; i < selectedFriendsIDs.size(); i++){
String inputParm = createdEventID + "/invited/" + selectedFriendsIDs.get(i);
Log.d("InviteSelectedFriends:inviteSelectedFriends", "inputParm"+inputParm);
Bundle params = new Bundle();
mAsyncRunner2.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)
{
try
{
JSONObject eventResponse = new JSONObject(response);
//event_id = event.getString("id");
Log.i(tag, "Event Response => " + eventResponse);
//Log.w("myapp", inputParm);
//Log.d("InviteSelectedFriends:inviteSelectedFriends", "INPUT PARAM->"+ inputParm.toString());
}
catch (Exception e)
{
}
}
@Override
public void onIOException(IOException e, Object state)
{
// TODO Auto-generated method stub
}
}, null);
}
}
catch (Exception e)
{
}
}
希望这可以帮助!