设置Facebook图表请求始终返回英文结果

时间:2016-04-02 10:21:47

标签: android facebook-graph-api

返回结果的语言取决于用户手机上的设置和/或其位置。我想强制请求只返回英文结果。 有问题的请求只是对用户的简单“我”请求。 到目前为止,我试图在参数中添加“local”,如下所示:

GraphRequest graphRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
        }
    });
    Bundle param = new Bundle();
    param.putString("fields", "albums, id, birthday, photos&locale=en_us");    
    graphRequest.setParameters(param);
    graphRequest.executeAsync();

我也试过这样做:

String graphPath = "/me?fields=id,name , first_name,albums, last_name&locale=en_us";
    GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), graphPath, null, HttpMethod.GET, new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse response) {
            Log.i(TAG, "response: " + response);
            Log.i(TAG, "object: " + response.getJSONObject());
        }
    });
    GraphResponse response = request.executeAsync();

两个都没有运气。最后一种方法实际上适用于Facebooks Graph Api Explorer:https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me%3Ffields%3Dalbums%26locale%3Dnb_no&version=v2.5

如果不添加“& locale = en_us”,则顶部图形请求将按预期工作。

1 个答案:

答案 0 :(得分:3)

这实际上是我所拥有的Facebook sdk版本中的一个错误,所以它不会只返回英文结果。它可以将“locale”设置为额外参数。正确的方法是:

GraphRequest graphRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
        }
    });
    Bundle param = new Bundle();
    param.putString("fields", "gender, email, name, albums");
    param.putString("locale", "en_US");
    graphRequest.setParameters(param);
    graphRequest.executeAsync();

如果这对您不起作用,请尝试升级您的Facebook sdk。我现在正在版本'com.facebook.android:facebook-android-sdk:4.11.0',这非常有效。