2016-03-07 151 views
1

這就是我想要做的目前獲取用戶位置

permissions.add("user_friends"); 
    permissions.add("email"); 
    permissions.add("public_profile"); 
    permissions.add("user_location"); 
在我捆綁

而且我傳遞參數

Bundle bundle = new Bundle(); 
    bundle.putString("fields", "id,name,picture,cover,email,location,accounts"); 

不幸的是,響應不包含位置對象。

偶試過這種方法,但崩潰,並將響應爲空或一些錯誤

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

回答

0

許多個月後,我知道,但任何人都可能需要在這裏是爲我工作:

@Override 
       public void onCompleted(JSONObject object, GraphResponse response) { 

        URL profile_pic=null; 
        try 
        { 

         String id = object.getString("id"); // get_id 
         try { 
          profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=150&height=150"); 
         } catch (MalformedURLException e) { 
          e.printStackTrace(); 
         } 
         String pic = profile_pic.toString(); //get profile pic to string to decode later to bitmap if needed 
         String name = object.getString("name"); // get username 
         String email = object.getString("email"); //get user email 

         String location = object.getJSONObject("location").getString("name"); //get location 


         System.out.println(id + ", " + name + ", " + email + " " + location); 
        } 
        catch (JSONException e) 
        { 
         e.printStackTrace(); 
        } 
        Intent intent = new Intent(StartingActivity.this, SignUpFinalise.class); 
        startActivity(intent); 

       } 

這裏的邏輯是響應對象中的位置是對象本身。因此需要調用getJSONObject,以便我們可以深入其樹,並獲取名稱字段,這是實際位置 您可以在此處看到響應對象的示例: https://developers.facebook.com/docs/android/graph