我创建了一个登录 Facebook 的小型应用程序,然后我可以将文本发布到墙上。起初它运作良好。但第二天,应用程序收到 Facebook 错误。
public class fblogin extends Activity implements OnClickListener {
String API_ID;
Facebook fb;
TextView fronttext;
ImageView pic;
Button postbutton,loginfb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginscreen);
API_ID = getString(R.string.API_ID);
fb= new Facebook(API_ID);
pic=(ImageView) findViewById(R.id.picture_pic);
fronttext=(TextView)findViewById(R.id.fronttext);
postbutton= (Button)findViewById(R.id.post);
loginfb=(Button)findViewById(R.id.login);
loginfb.setOnClickListener(this);
updateButtonImage();
}
public void updateButtonImage()
{
if(fb.isSessionValid())
{
postbutton.setVisibility(Button.VISIBLE);
loginfb.setText("Logout");
fronttext.setText("WelCome ");
pic.setVisibility(ImageView.VISIBLE);
Toast.makeText(getApplicationContext(), "You are log In ",Toast.LENGTH_LONG);
JSONObject obj=null;
URL img_url=null;
try {
String jsonUser = fb.request("me");
obj=Util.parseJson(jsonUser);
String id = obj.optString("id");
String name=obj.optString("name");
fronttext.setText("WelCome "+ name);
img_url =new URL("http://graph.facebook.com/"+id+"/picture?type=small");
Bitmap bmp=BitmapFactory.decodeStream(img_url.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
{
postbutton.setVisibility(Button.INVISIBLE);
loginfb.setText("Login");
fronttext.setText("To get Started , Login Using Facebook");
pic.setVisibility(ImageView.INVISIBLE);
Toast.makeText(getApplicationContext(), "You are log Out ",Toast.LENGTH_LONG);
}
}
public void postbuttonClicks(View v)
{
switch(v.getId()){
case R.id.post:
//post
fb.dialog(fblogin.this,"feed",new DialogListener() {
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
Toast.makeText(fblogin.this, "facebook Error....", Toast.LENGTH_LONG).show();
}
@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
Toast.makeText(fblogin.this, "Error On Page.....", Toast.LENGTH_LONG).show();
}
@Override
public void onComplete(Bundle values) {
Toast.makeText(fblogin.this, "Sucessfully Post on wall...", Toast.LENGTH_LONG).show();
}
@Override
public void onCancel() {
Toast.makeText(fblogin.this, "Cancel The Activity....", Toast.LENGTH_LONG).show();
}
});
break;
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(fb.isSessionValid()){
//log out
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
fb.authorize(fblogin.this,new DialogListener() {
@Override
public void onComplete(Bundle values) {
updateButtonImage();
}
@Override
public void onFacebookError(FacebookError e) {
updateButtonImage();
Toast.makeText(fblogin.this, "facebookError", Toast.LENGTH_LONG).show();
}
@Override
public void onError(DialogError e) {
Toast.makeText(fblogin.this, "facebookon...Error", Toast.LENGTH_LONG).show();
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(fblogin.this, "facebookCancel", Toast.LENGTH_LONG).show();
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
fb.authorizeCallback(requestCode, resultCode, data);
}
}
然后我创建了onActivityResult
方法,错误得到解决,我可以登录了。但是今天我又遇到了这个错误。我该怎么做才能解决它?我的程序有问题吗?还是会话问题?