如何从Android中的Facebook检索我的位置和电话号码?

时间:2014-04-14 11:03:55

标签: android facebook facebook-graph-api facebook-android-sdk

我是Android的新手。我已将Facebook与Android集成。我检索了id,名称和个人资料图片。我想从Fb检索我的电话号码和位置。我无法理解权限设置和所有权限。我做了以下编码。我希望有人一步一步向我解释该怎么做。我的代码如下:

MainActivity.class

session.openForRead(new Session.OpenRequest(MainActivity.this).setCallback(statusCallback).setPermissions(permissions));
    new Request(
       session,
       "/me?fields=location",
           null,
           HttpMethod.GET,                         
           new Request.Callback() {
                 public void onCompleted(Response response) {
                    // handle the result 
                    // f
                     Map<String, Object> responseMap = new HashMap<String, Object>();
                     GraphObject graphObject = response.getGraphObject();
                     responseMap = graphObject.asMap();
                     String location = responseMap.get("location").toString();
                     Log.i("location",""+location);
                 }
           }
        ).executeAsync();

   Session session = Session.getActiveSession();
    if (session.isOpened()) {
      Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {
        @Override
        public void onCompleted(GraphUser user, Response response) {
           // TODO Auto-generated method stub
           if(user!=null){
                final String t1=user.getId();
                final String t2=user.getName();
           }
        }
    });

错误日志:

   04-15 10:19:13.024: E/AndroidRuntime(17207): FATAL EXCEPTION: main
   04-15 10:19:13.024: E/AndroidRuntime(17207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taskmanager/com.example.taskmanager.MainActivity}: java.lang.UnsupportedOperationException: Session: an attempt was made to open an already opened session.
   04-15 10:19:13.024: E/AndroidRuntime(17207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
   04-15 10:19:13.024: E/AndroidRuntime(17207):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
  04-15 10:19:13.024: E/AndroidRuntime(17207):  at android.app.ActivityThread.access$700(ActivityThread.java:143)
  04-15 10:19:13.024: E/AndroidRuntime(17207):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
  04-15 10:19:13.024: E/AndroidRuntime(17207):  at android.os.Handler.dispatchMessage(Handler.java:99)
  04-15 10:19:13.024: E/AndroidRuntime(17207):  at android.os.Looper.loop(Looper.java:137)
  04-15 10:19:13.024: E/AndroidRuntime(17207):  at android.app.ActivityThread.main(ActivityThread.java:4960)
  04-15 10:19:13.024: E/AndroidRuntime(17207):  at java.lang.reflect.Method.invokeNative(Native Method)
  04-15 10:19:13.024: E/AndroidRuntime(17207):  at java.lang.reflect.Method.invoke(Method.java:511)
 04-15 10:19:13.024: E/AndroidRuntime(17207):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
  04-15 10:19:13.024: E/AndroidRuntime(17207):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
  04-15 10:19:13.024: E/AndroidRuntime(17207):  at dalvik.system.NativeStart.main(Native Method)
  04-15 10:19:13.024: E/AndroidRuntime(17207): Caused by: java.lang.UnsupportedOperationException: Session: an attempt was made to open an already opened session.
 04-15 10:19:13.024: E/AndroidRuntime(17207):   at com.facebook.Session.open(Unknown Source)
 04-15 10:19:13.024: E/AndroidRuntime(17207):   at com.facebook.Session.openForRead(Unknown Source)
 04-15 10:19:13.024: E/AndroidRuntime(17207):   at com.example.taskmanager.MainActivity.updateView(MainActivity.java:85)
  04-15 10:19:13.024: E/AndroidRuntime(17207):  at com.example.taskmanager.MainActivity.onCreate(MainActivity.java:59)
  04-15 10:19:13.024: E/AndroidRuntime(17207):  at android.app.Activity.performCreate(Activity.java:5203)
    04-15 10:19:13.024: E/AndroidRuntime(17207):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
   04-15 10:19:13.024: E/AndroidRuntime(17207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
04-15 10:19:13.024: E/AndroidRuntime(17207):    ... 11 more

1 个答案:

答案 0 :(得分:0)

您无法获取用户的电话号码。

要获取位置,您需要:

  • API致电:/me?fields=location - Demo

  • 权限:user_location 使用android sdk检查here如何申请其他权限。

获取位置的代码:

new Request(
   session,
   "/me?fields=location",
   null,
   HttpMethod.GET,
   new Request.Callback() {
     public void onCompleted(Response response) {
         /* handle the result */
     }
   }
).executeAsync();

查看用户可以使用的所有字段:/{user-id}