我正在开发一个Android应用程序,其中有两个活动。
第一项活动:使用按钮进入Facebook已登录页面。我想知道如何从Facebook获取数据(如姓名,DOB,地点,电子邮件ID)到我的应用程序。
第二个活动(使用EditText):如何匹配API或需要什么请告诉我。我到处搜索但没找到任何答案。
我已经这样做了,但它显示错误。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facebook);
facebook = (Button) findViewById(R.id.facebook);
facebook.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onClickFacebookLogin();
// Intent in = new Intent(FacebookActivity.this,GetDataFromFacebook.class);
// in.putExtra("email", );
// startActivity(in);
}
});
}
public void onClickFacebookLogin() {
// Session.openActiveSession(this, true, new Session.StatusCallback() {
openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
// ((GlobalFilename)Login.this.getApplication()).setfbSession(session);
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user,
Response response) {
// TODO Auto-generated method stub
if (user != null) {
final GraphUser graphuser=user;
try{
new AsyncTask<Void, Void, Void>() {
String fbemail,fname,lname,mediaid,image_url,gender,dob;
ProgressDialog progressDialog;
ImageLoader imageLoader;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
if (progressDialog == null) {
progressDialog = createProgressDialog(MyAccount.this);
progressDialog.show();
} else {
progressDialog.show();
}
}
@Override
protected Void doInBackground(Void... params) {
Object g = graphuser.asMap().get("email");
if(g==null)
{
fbemail="";
fname=graphuser.getFirstName();
lname=graphuser.getLastName();
mediaid=graphuser.getId();
System.out.println("===>accountname inside null"+fbemail);
return null;
}
else{
fbemail= graphuser.asMap().get("email").toString();
gender =graphuser.asMap().get("gender").toString();
image_url = "https://graph.facebook.com/"+graphuser.getId()+"/picture?type=square";
// image_url=String.format(image_url);
//accountName =graphuser.getProperty("email").toString();
System.out.println("fb email====>"+fbemail);
fname=graphuser.getFirstName();
lname=graphuser.getLastName();
mediaid=graphuser.getId();
dob=graphuser.getBirthday();
System.out.println("Gender"+gender);
System.out.println("Image==>"+image_url);
try {
InputStream in = new java.net.URL(image_url).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
if(mIcon11==null)
{
System.out
.println("null in doinbackground");
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
//Toast.makeText(getApplicationContext(), userid, Toast.LENGTH_LONG).show();
System.out.println("fb email====>"+fbemail);
//Toast.makeText(getApplicationContext(), accountName, Toast.LENGTH_LONG).show();
return null;
}
}
@Override
protected void onPostExecute(Void v) {
super.onPreExecute();
progressDialog.dismiss();
tvname.setText(fname+" "+lname);
tvemail.setText(fbemail);
imageLoader = new ImageLoader(MyAccount.this);
System.out
.println("imageurlonpost==>"+image_url);
// imageLoader.DisplayImage(image_url,fbprofileimage);
if (mIcon11 != null
){
// do what you need to do with the bitmap :)
fbprofileimage.setImageBitmap(mIcon11);
}
else{
Toast.makeText(MyAccount.this,"Null",Toast.LENGTH_LONG).show();
}
}
}.execute();
}catch(Exception e) {
//findViewById(R.id.progressbar).setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Facebook configuration error!", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
},Arrays.asList("email"));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}
在new Session.StatusCallback()(under onClickFacebookLogin()) & getActiveSession() (at onActivityResult)
答案 0 :(得分:1)
这是完整的工作代码。设置您的开发者帐户并调用此方法。一旦控件从facebook返回,也不要忘记调用onActivityResult()。
//Method invoked when facebook login button is clicked
public void onClickFacebookLogin() {
// Session.openActiveSession(this, true, new Session.StatusCallback() {
openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
// ((GlobalFilename)Login.this.getApplication()).setfbSession(session);
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user,
Response response) {
// TODO Auto-generated method stub
if (user != null) {
final GraphUser graphuser=user;
try{
new AsyncTask<Void, Void, Void>() {
String fbemail,fname,lname,mediaid,image_url,gender,dob;
ProgressDialog progressDialog;
ImageLoader imageLoader;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
if (progressDialog == null) {
progressDialog = createProgressDialog(MyAccount.this);
progressDialog.show();
} else {
progressDialog.show();
}
}
@Override
protected Void doInBackground(Void... params) {
Object g = graphuser.asMap().get("email");
if(g==null)
{
fbemail="";
fname=graphuser.getFirstName();
lname=graphuser.getLastName();
mediaid=graphuser.getId();
System.out.println("===>accountname inside null"+fbemail);
return null;
}
else{
fbemail= graphuser.asMap().get("email").toString();
gender =graphuser.asMap().get("gender").toString();
image_url = "https://graph.facebook.com/"+graphuser.getId()+"/picture?type=square";
// image_url=String.format(image_url);
//accountName =graphuser.getProperty("email").toString();
System.out.println("fb email====>"+fbemail);
fname=graphuser.getFirstName();
lname=graphuser.getLastName();
mediaid=graphuser.getId();
dob=graphuser.getBirthday();
System.out.println("Gender"+gender);
System.out.println("Image==>"+image_url);
try {
InputStream in = new java.net.URL(image_url).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
if(mIcon11==null)
{
System.out
.println("null in doinbackground");
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
//Toast.makeText(getApplicationContext(), userid, Toast.LENGTH_LONG).show();
System.out.println("fb email====>"+fbemail);
//Toast.makeText(getApplicationContext(), accountName, Toast.LENGTH_LONG).show();
return null;
}
}
@Override
protected void onPostExecute(Void v) {
super.onPreExecute();
progressDialog.dismiss();
tvname.setText(fname+" "+lname);
tvemail.setText(fbemail);
imageLoader = new ImageLoader(MyAccount.this);
System.out
.println("imageurlonpost==>"+image_url);
// imageLoader.DisplayImage(image_url,fbprofileimage);
if (mIcon11 != null
){
// do what you need to do with the bitmap :)
fbprofileimage.setImageBitmap(mIcon11);
}
else{
Toast.makeText(MyAccount.this,"Null",Toast.LENGTH_LONG).show();
}
}
}.execute();
}catch(Exception e) {
//findViewById(R.id.progressbar).setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Facebook configuration error!", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
},Arrays.asList("email"));
}