0
我想通过Facebook登录获取用户在我的Android应用程序中的位置。我实现了登录,并且除了位置以外,一切工作正常(用户在其个人资料中更新为“生活在“)我检索用户名和ID使用this.I设置权限also.But它不工作,请帮助我。无法从Facebook SDK获取用户位置Android
这里是我的类
public class FacebookActivity extends Activity implements OnClickListener
{
Facebook fb;
//ImageView pic;
CircularImageView pic;
ImageView button;
TextView welcome,location;
SharedPreferences sp;
String APP_ID = "xxxxxxxx";
String name;
String id;
String town;
String currentCity;
String imgurl_check;
Drawable d ;
Bitmap bitmap;
String access_token;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fb = new Facebook(APP_ID);
sp = getPreferences(MODE_PRIVATE);
access_token = sp.getString("access_token", null);
long expires = sp.getLong("access_expires", 0);
if(access_token !=null)
{
fb.setAccessToken(access_token);
}
if(expires !=0)
{
fb.setAccessExpires(expires);
}
button = (ImageView) findViewById(R.id.login);
welcome = (TextView) findViewById(R.id.name);
location = (TextView) findViewById(R.id.location);
//pic = (ImageView) findViewById(R.id.picture_pic);
pic = (CircularImageView) findViewById(R.id.picture_pic);
button.setOnClickListener(this);
updateButtonImage();
}
private void updateButtonImage()
{
if(fb.isSessionValid())
{
//button.setImageResource(R.drawable.fb_logout);
button.setVisibility(View.INVISIBLE);
new GetProfileName().execute();
new LoadProfileImage(pic)
.execute("https://graph.facebook.com/me/picture?width=200&height=200&method=GET&access_token="
+ access_token);
//pic.setVisibility(View.VISIBLE);
}
else
{
button.setImageResource(R.drawable.fb_login);
welcome.setVisibility(View.INVISIBLE);
pic.setVisibility(View.INVISIBLE);
}
}
private class GetProfileName extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls)
{
JSONObject obj = null;
JSONObject obj1 = null;
URL img_url = null;
String jsonUser;
String jsonPicture;
try {
jsonUser = fb.request("me");
obj = Util.parseJson(jsonUser);
id = obj.optString("id");
name = obj.optString("name");
currentCity = obj.getJSONObject("location").getString("name");
Log.d("Executing thread",id);
Log.d("Executing thread",name);
Log.d("Executing thread",currentCity);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FacebookError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
location.setText(currentCity);
location.setVisibility(View.VISIBLE);
welcome.setText(name);
welcome.setVisibility(View.VISIBLE);
sp = getPreferences(MODE_PRIVATE);
final SharedPreferences.Editor editor = sp.edit();
editor.putString("UserName", name);
editor.commit();
}
}
/**
* Background Async task to load user profile picture from url
* */
private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
CircularImageView bmImage;
public LoadProfileImage(CircularImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
pic.setVisibility(View.VISIBLE);
String extr = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extr + "/App");
if (!mFolder.exists()) {
mFolder.mkdir();
}
String s = "myfile.png";
File f = new File(mFolder.getAbsolutePath(),s);
String strMyImagePath = f.getAbsolutePath();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
result.compress(Bitmap.CompressFormat.PNG,70, fos);
fos.flush();
fos.close();
// MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onClick(View v)
{
if(fb.isSessionValid())
{
try {
fb.logout(getApplicationContext());
updateButtonImage();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
//login to fb
fb.authorize(FacebookActivity.this,new String[] {"email","user_hometown","user_location"},new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
Toast.makeText(FacebookActivity.this, "fberror", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
Toast.makeText(FacebookActivity.this, "onerror", Toast.LENGTH_SHORT).show();
}
@Override
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();
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(FacebookActivity.this, "Oncancel", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
fb.authorizeCallback(requestCode, resultCode, data);
}
}
的任何错误都你得到些什么? – 2014-10-10 10:35:21
没有error.just,它没有从json – Nevaeh 2014-10-10 10:37:56
检索但它存在于JSON? – Tobi 2014-10-10 10:38:58