gpt4 book ai didi

android - 无法通过android上的访问 token 获取Facebook Graph API信息

转载 作者:行者123 更新时间:2023-11-30 03:56:53 26 4
gpt4 key购买 nike

我正在 android 上编写一个简单的应用程序来获取通过 Facebook 登录该应用程序的用户的信息。这是我的代码:

public class example extends Activity {

Facebook facebook = new Facebook("id");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);

private static final String TAG ="Debug";
private SharedPreferences mPrefs;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
/*
* 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) {
facebook.setAccessToken(access_token);
}
if(expires != 0) {
facebook.setAccessExpires(expires);
}

/*
* Only call authorize if the access_token has expired.
*/
if(!facebook.isSessionValid()) {

facebook.authorize(this, new String[] {"publish_stream","email","user_groups","read_stream","user_about_me","offline_access"}, new DialogListener() {
@Override
public void onComplete(Bundle values) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
}

@Override
public void onFacebookError(FacebookError error) {}

@Override
public void onError(DialogError e) {}

@Override
public void onCancel() {}
});
}
Log.d(TAG, access_token);
getInformation();
}

private void getInformation(){

JSONObject json = null;

try
{
Log.d("testing", "point1");
String response = facebook.request("me");
Log.d("testing", "point2");
json = Util.parseJson(response);
String id = json.getString("id");
Log.d("testing", id);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (JSONException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (FacebookError e)
{
e.printStackTrace();
}

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

facebook.authorizeCallback(requestCode, resultCode, data);
}

@Override
public void onResume() {
super.onResume();
facebook.extendAccessTokenIfNeeded(this, null);
}
}

我关注这里https://developers.facebook.com/docs/mobile/android/build/#sdk并使用相同的 facebook SDK。因此,如代码所示,在 logcat 中只有 point1 而没有 point2,这意味着 facebook.request("me") 不成功。模拟器显示应用已关闭并说“不幸的是,示例已停止。”

所以有人可以帮我指出问题吗?我想知道访问 token 是否有问题,但我不知道。非常感谢。

日志:

11-03 10:13:55.986: D/Debug(893): AAAFi20EvlJEBACwBLIR4buSOaZB1emxACFPZCZAKums6ZCNwZB0wZCvQnI3grshT0YNl8ZC427BSkrNye6akdCZBjvp4KL24Oyx......
11-03 10:13:55.986: D/testing(893): point1
11-03 10:13:56.106: D/AndroidRuntime(893): Shutting down VM
11-03 10:13:56.106: W/dalvikvm(893): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-03 10:13:56.136: E/AndroidRuntime(893): FATAL EXCEPTION: main
11-03 10:13:56.136: E/AndroidRuntime(893): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crush/com.example.crush.Crush}: android.os.NetworkOnMainThreadException
11-03 10:13:56.136: E/AndroidRuntime(893): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)

最佳答案

您正在主线程上运行网络操作。使用异步任务在后台线程中运行网络操作(在后台线程中执行您的 http 请求)。这就是您收到 android.os.NetworkOnMainThreadException 的原因。

像这样在异步任务中建立网络:

class FacebookRequestTask extends AsyncTask{


protected void onPreExecute() {
//show a progress dialog to the user or something
}

protected void doInBackground() {
//Do your networking here
}

protected void onPostExecute() {
//do something with your parsed
//response (JSON data in case of facebook API's) here
// and dismiss the progress dialog
}
}

new facebookRequestTask().execute();

如果您不知道如何使用异步任务,这里有一些教程供您使用:

http://mobileorchard.com/android-app-developmentthreading-part-2-async-tasks/

http://www.vogella.com/articles/AndroidPerformance/article.html

Google 的官方文档如下:

https://developer.android.com/reference/android/os/AsyncTask.html

关于android - 无法通过android上的访问 token 获取Facebook Graph API信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13208161/

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