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

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

问题描述

我在Android应用中使用了Facebook SDK。我需要获取用户的所有信息。到目前为止,我可以获得姓名ID电子邮件生日和用户的个人资料图片。我需要得到的最后一个是用户的位置或当前位置。我正在使用以下代码。

  if(fb.isSessionValid()){
button.setImageResource .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(欢迎!+名称);
((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自动生成的catch块
e.printStackTrace();
} catch(JSONException e){
// TODO自动生成的catch块
e.printStackTrace();
} catch(MalformedURLException e){
// TODO自动生成的catch块
e.printStackTrace();
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}



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

这是权限

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

public void onFacebookError(FacebookError e){
// TODO自动生成的方法存根
Toast.makeText(getApplicationContext(),+ e,Toast.LENGTH_SHORT ).show();
e.printStackTrace();
}

public void onError(DialogError e){
// TODO自动生成的方法stub
//Toast.makeText(getApplicationContext(),Error,Toast.LENGTH_SHORT).show();
}

public void onComplete(Bundle values){
// TODO自动生成的方法stub
编辑器编辑器= sp.edit();
editor.putString(access_token,fb.getAccessToken());
editor.putLong(access_expires,fb.getAccessExpires());
editor.commit();
updateButtonImage();
}

public void onCancel(){
// TODO自动生成的方法stub
Toast.makeText(getApplicationContext(),取消,Toast.LENGTH_SHORT )。显示();
}
});
}

感谢谁来帮助高级人员。

解决方案

Graph API Explorer工具是查看图形API端点的响应的好方法。通过点击获取访问令牌,查看/ me的输出,请求正确的权限(例如 user_location )。当我使用 user_location 权限获取访问令牌时,我可以看到我当前的



https://developers.facebook.com/tools/explorer/?method=GET&path=me



要获取当前位置,请执行以下操作:

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

使用较新的SDK,您可以通过传递如下API API对象

$来获取位置详细信息b
$ b

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


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);
        }
    }

Here are the permissions

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.

解决方案

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/tools/explorer/?method=GET&path=me

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

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

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

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

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

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