图形API不从Facebook SDK在Android中获取位置 [英] Graph Api Not Getting Location from Facebook SDK in Android

查看:113
本文介绍了图形API不从Facebook SDK在Android中获取位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Andr​​oid应用程序使用Facebook的SDK。我需要得到用户的所有信息。现在我能够得到名ID电子邮件生日和用户的配置文件图片。最后一个,我需要得到的是位置或用户的当前位置。我使用的是低于此code。

I'm using Facebook SDK in my Android App. And I need to get all the information of the user. By now I'm able to get the Name ID Email Birthday and the profile pic of the user. The last one that I need to get is the location or current location of the user. I'm using this code below.

if(fb.isSessionValid()){
            button.setImageResource(R.drawable.logout_button);

            JSONObject obj = null;
            URL img = null;


             try {
                    String jsonUser = fb.request("me");
                    obj = Util.parseJson(jsonUser);
                    String id = obj.optString("id");
                    String name = obj.optString("name");
                    String bday = obj.optString("birthday");
                    String address = obj.optString("location");
                    String email = obj.optString("email");
                    ((TextView) findViewById(R.id.txt)).setText("Welcome! "+name);
                    ((TextView) findViewById(R.id.txtbday)).setText("Birthday: "+bday);
                    ((TextView) findViewById(R.id.txtaddress)).setText("Address: "+address);
                    ((TextView) findViewById(R.id.txtemail)).setText("Email: "+email);
                    img = new URL("http://graph.facebook.com/"+id+"/picture?type=normal");
                    Bitmap bmp = BitmapFactory.decodeStream(img.openConnection().getInputStream());
                    pic.setImageBitmap(bmp);

                } catch (FacebookError e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



        }else{
            button.setImageResource(R.drawable.login_button);
        }
    }

下面是权限

fb.authorize(MainActivity.this,new String[] {"email", "user_location", "user_birthday","publish_stream"}, new DialogListener() {

                public void onFacebookError(FacebookError e) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), ""+e, Toast.LENGTH_SHORT).show();   
                    e.printStackTrace();
                }

                public void onError(DialogError e) {
                    // TODO Auto-generated method stub
                    //Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();      
                }

                public void onComplete(Bundle values) {
                    // TODO Auto-generated method stub
                    Editor editor = sp.edit();
                    editor.putString("access_token", fb.getAccessToken());
                    editor.putLong("access_expires", fb.getAccessExpires());
                    editor.commit();
                    updateButtonImage();
                }

                public void onCancel() {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "Cancel", Toast.LENGTH_SHORT).show();   
                }
            });
        }

感谢谁将会在先进的帮助。

Thanks for who will help in advanced.

推荐答案

在图形API资源管理器工具是看图形API的端点的响应是一个伟大的方式。只需点击获取访问令牌要求正确的权限(如 USER_LOCATION ),并查看/我的输出。当我与 USER_LOCATION 权限的访问令牌,我能看到我的电流

The Graph API Explorer tool is a great way to see what the response of a graph API endpoint is. Just request the right permissions (such as user_location) by clicking "Get Access Token" and see the output of '/me'. When I get an access token with the user_location permission I am able to see my current

https://developers.facebook.com/tool​​s/explorer ?/方法= GET和放大器; PATH =我

要获得目前的位置,这是如何做到这一点:

To get the current location, this is how to do it:

String currentCity = obj.getJSONObject("location").getString("name");

使用新的SDK可以通过如下传递与图形API对象获取位置详细

With newer SDKs you can get location detail by passing as below with graph API object

graphObject.getInnerJSONObject().getJSONObject("location").getString("id"))

这篇关于图形API不从Facebook SDK在Android中获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆