[英]Why post on friends wall in facebook via android application return error?
[英]Can not access Facebook friends via Android application
我通過我的Android應用程序訪問圖形API時遇到問題,該應用程序將用戶的朋友檢索為JSONObject,提取其名稱並在屏幕上顯示它們。 它應該是一個簡單而直接的任務,但顯然它不是。 當我在Android Nexus I上運行我的應用程序時,我登錄到Facebook,然后我被要求單擊“允許”按鈕授予權限,我被重定向到空白頁面。 我期待看到我的朋友的名字,但它不會發生。 有人可以幫助我。
public class Login extends Activity
{
Facebook mFacebook = new Facebook("201509899926116");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
View linearLayout;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout = findViewById(R.id.main_layout);
/* Get existing access_token if any */
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if(access_token != null)
{
mFacebook.setAccessToken(access_token);
}
if(expires != 0)
{
mFacebook.setAccessExpires(expires);
}
/* Only call authorize if the access_token has expired. */
if(!mFacebook.isSessionValid())
{
mFacebook.authorize(this, new String[] {"user_birthday","email",
"user_relationships","user_religion_politics","user_hometown",
"user_location","user_relationship_details","user_education_history",
"user_likes","user_interests", "user_activities"},
new DialogListener()
{
@Override
public void onComplete(Bundle values)
{
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", mFacebook.getAccessToken());
editor.putLong("access_expires", mFacebook.getAccessExpires());
editor.commit();
/* access the graph API */
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 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)
{
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");
TextView tv = new TextView(Login.this);
tv.setText(n);
((LinearLayout) linearLayout).addView(tv);
}
}
catch (JSONException e)
{
Log.w("Facebook-Example", "JSON Error in response");
}
}
}
}
我想我可能已經找到了這個問題..根據關於onComplet的文檔;
/**
* Called when a request completes with the given response.
*
* Executed by a background thread: do not update the UI in this method.
*/
並且您正在嘗試更新您的UI,因此addViews實際上並沒有反映在UI線程上。您應該在活動中創建一個方法,您可以使用runOnUIThread從onComplete調用..這樣的事情;
runOnUiThread(new Runnable() {
public void run() {
inserFacebookNames(facebookContactNames);
}
});
讓我知道這是否真的解決了它..我很好奇,如果這是原因..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.