0
我一直在努力與Android和Facebook的圖形API,我敢肯定,這是中級程序員的小菜一碟。我想要的是一個非常簡單的Android應用程序,它將用戶登錄到Facebook,獲得用戶同意訪問信息,最後無縫地查詢圖形API以獲取用戶朋友。我提到工作無縫,因爲我看到許多示例應用程序需要用戶按下「獲取朋友」按鈕,但我不想那樣做。我希望提取用戶朋友而不要求他推送任何東西。使用Facebook-Android SDK訪問朋友的麻煩
我粘貼了下面的代碼。我可以登錄並授予我的應用程序許可。接下來,我期望它展示我的朋友,但它只是指引我到一個空白頁面。問題可能是我在錯誤的地方訪問了圖形API,因爲Logcat不包含我在請求前後打印的消息。你可以幫我嗎。
公共類FacebookLogin延伸活動{
Facebook mFacebook = new Facebook("XXXXXXXXXXXX");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
View linearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout = findViewById(R.id.main_layout);
mFacebook.authorize(this, new DialogListener() {
@Override
public void onComplete(Bundle values)
{
Log.d("Facebook-Example-Friends Request", "Started API request");
mAsyncRunner.request("me/friends", new FriendsRequestListener());
Log.d("Facebook-Example-Friends Request", "Finished API request");
}
@Override
public void onFacebookError(FacebookError error) {}
@Override
public void onError(DialogError e) {}
@Override
public void onCancel() {}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
public class FriendsRequestListener implements RequestListener
{
/**
* Called when the request to get friends has been completed.
* Retrieve, parse and display the JSON stream.
*/
public void onComplete(final String response)
{
Log.d("Facebook-Example-Friends Request", "FriendsListenerOnComplete");
try
{
JSONObject json = Util.parseJson(response);
final JSONArray friends = json.getJSONArray("data");
FacebookLogin.this.runOnUiThread(new Runnable()
{
public void run()
{
int l = (friends != null ? friends.length() : 0);
for (int i=0; i<l; i++)
{
try
{
JSONObject o = friends.getJSONObject(i);
TextView tv = new TextView(FacebookLogin.this);
tv.setText(o.getString("name"));
((LinearLayout) linearLayout).addView(tv);
}
catch(JSONException e)
{
Toast.makeText(getApplicationContext(), "JSON parsing error", Toast.LENGTH_LONG);
}
}
}
});
}
catch(JSONException e)
{
Log.d("Facebook-Example-Friends Request", "JSON Error in response");
}
catch(FacebookError e)
{
Log.d("Facebook-Example-Friends Request", "Facebook Error in response");
}
}
}
}
不logcat wr ite「已啓動API請求」?如果不嘗試在onOnError,onError,onCancel中添加Log.d以查看它說的是什麼。 – Daverix