Graph API从Android SDK中的Facebook SDK获取位置

编程入门 行业动态 更新时间:2024-10-10 13:22:21
本文介绍了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(graph.facebook/+ 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()); editormit(); updateButtonImage(); } public void onCancel(){ // TODO自动生成的方法stub Toast.makeText(getApplicationContext(),取消,Toast.LENGTH_SHORT )。显示(); } }); }

感谢谁来帮助高级人员。

解决方案

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

developers.facebook/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("graph.facebook/"+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()); editormit(); 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

developers.facebook/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获取位置

本文发布于:2023-11-28 14:25:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642781.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   API   Graph   Android   Facebook

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!