I want to get the location of user in my android application through Facebook login.I implemented login and everything is working fine other than location(Location which user updates in his profile as "Lives in").I retrieved username and id using this.I set permission also.But its not working.Please help me.
Here is my class
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);
}
}