我正在尝试在我的应用程序上使用 FB Connect,并且在模拟器中一切正常并在 Galaxy Nexus 中进行调试,但在将其上传到谷歌播放后它停止工作。
如果我安装从 Play 下载的应用程序,我的 facebook.authorize 函数会返回一个 DialogError(我只是不知道是什么错误,因为我只能在使用 eclipse 进行安装时查看 logcat)。只有当我安装了 facebook 应用程序时才会发生这种情况。因此,当我卸载 fb 应用程序时,它运行良好...
也许是我在计算机上准备 facebook SDK 时所做的一些错误配置?我在下面粘贴了所有代码,但没有任何日志...
public class FacebookConnectActivity extends Activity {
private static final String TAG_JSON = "json";
static Facebook facebook = new Facebook("11111111111111");
AsyncFacebookRunner mAsyncRunner;
private static SharedPreferences mPrefs;
JSONObject json = null;
Context ctx = this;
boolean callback = false;
private static Context staticContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resgatar_produto_layout);
FacebookConnectActivity.staticContext = getApplicationContext();
Log.e("FacebookConnect", "Activity Started");
mPrefs = getSharedPreferences("PREFS_LOGIN", MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
Log.e("FacebookConnect",
"Access Token retrieved from SharedPreferences");
}
if (expires != 0) {
facebook.setAccessExpires(expires);
Log.e("FacebookConnect",
"Access Expires retrieved from SharedPreferences");
}
if (!facebook.isSessionValid()) {
facebook.authorize(this, new String[] { "email", "user_photos",
"user_birthday", "user_hometown", "user_relationships",
"user_location", "user_work_history", "publish_actions" },
new DialogListener() {
@Override
public void onComplete(Bundle values) {
Log.e("FacebookConnect", "User authorized");
Log.e("FacebookConnect",
"onComplete called at facebook.authorize");
Log.e("FacebookConnect", "Access Token: "
+ facebook.getAccessToken().toString());
try {
mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me",
new SampleRequestListener());
Log.e("FacebookConnect",
"AsyncFacebookRunner request started (facebook.authorize.onComplete)");
Bundle params = new Bundle();
params.putString("link",
"https://apps.facebook.com/qranio_game/");
params.putString("picture", "http://www.qranio.com/site/images/logo_qranio_facebook.png");
int count = 0;
while (json == null && count<20){
Log.e("FacebookConnect",
"Waiting for json (facebook.authorize.onComplete)");
waiting(1000);
count++;
}
if (!(count<20)){
noConnectionAlert();
}
String usuario = json.getString("first_name");
params.putString("name", usuario+" está jogando Qranio para Android");
params.putString("caption", "Qranio - Making Learning Fun");
params.putString("description", "O Qranio é uma plataforma online que propicia o Saber. Você joga, " +
"aprende, acumula Qi$ e troca por prêmios incríveis! Venha jogar também!");
//JSONArray fbArr = new JSONArray("{\"name\":\"Jogar\",\"link\":\"https://apps.facebook.com/qranio_game/\"}");
params.putString("actions", "{\"name\":\"Jogar\",\"link\":\"https://apps.facebook.com/qranio_game/\"}");
params.putString("display", "touch");
facebook.dialog(ctx, "feed", params,
new SampleDialogListener());
Log.e("FacebookConnect",
"Post on Wall Dialog called (facebook.authorize.onComplete)");
// facebook.
// if (facebook.isSessionValid()){
// }else{
// Log.e("FacebookConnect",
// "Invalid facebook session while trying to fetch user data");
// }
} catch (FacebookError fbe) {
Log.e("FacebookConnect",
"facebook.authorize FacebookError "
+ fbe.toString());
} catch (Exception e) {
Log.e("FacebookConnect",
"facebook.authorize Exception "
+ e.toString());
}
}
@Override
public void onFacebookError(FacebookError error) {
Log.e("FacebookConnect",
"FacebookError authorizing: "
+ error.toString());
final AlertDialog alertDialog = new AlertDialog.Builder(
FacebookConnectActivity.this).create();
alertDialog.setTitle("Erro");
alertDialog
.setMessage("Ocorreu um erro com o Facebook, por favor tente novamente.");
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
alertDialog.dismiss();
finish();
}
});
alertDialog.show();
}
@Override
public void onError(DialogError e) {
//Here is where I'm getting the error when downloaded from Play
Log.e("FacebookConnect",
"Error authorizing: " + e.toString());
final AlertDialog alertDialog = new AlertDialog.Builder(
FacebookConnectActivity.this).create();
alertDialog.setTitle("Erro");
alertDialog
.setMessage("Erro ao tentar conectar com o facebook, tente novamente.");
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
alertDialog.dismiss();
finish();
}
});
alertDialog.show();
}
@Override
public void onCancel() {
Log.e("FacebookConnect", "Autorizing canceled");
finish();
}
});
} else {
mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me", new SampleRequestListener());
Log.e("FacebookConnect",
"Valid session found, AsyncFacebookRunner request started");
startDataProcess();
}
}
public void noConnectionAlert(){
final AlertDialog alertDialog = new AlertDialog.Builder(
FacebookConnectActivity.this).create();
alertDialog.setTitle("Erro");
alertDialog
.setMessage("Verifique sua conexão com a internet e tente novamente.");
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
alertDialog.dismiss();
finish();
}
});
alertDialog.show();
}
public static void waiting (int n){
long t0, t1;
t0 = System.currentTimeMillis();
do{
t1 = System.currentTimeMillis();
}
while (t1 - t0 < n);
}
public static void logoutFB() {
if (facebook.isSessionValid()) {
Log.e("AndroidDashboarDesign",
"Valid FB session found, logging out");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.logout(staticContext, new BaseRequestListener() {
@Override
public void onComplete(String response, Object state) {
Log.e("AndroidDashboarDesign", "Complete FB logout");
}
@Override
public void onIOException(IOException e, Object state) {
Log.e("AndroidDashboarDesign",
"(logout) IOException: " + e.getMessage());
}
@Override
public void onFileNotFoundException(FileNotFoundException e,
Object state) {
Log.e("AndroidDashboarDesign",
"(logout) FileNotFoundException: " + e.getMessage());
}
@Override
public void onMalformedURLException(MalformedURLException e,
Object state) {
Log.e("AndroidDashboarDesign",
"(logout) MalFormedURLException: " + e.getMessage());
}
@Override
public void onFacebookError(FacebookError e, Object state) {
Log.e("AndroidDashboarDesign", "(logout) FacebookError: "
+ e.getMessage());
}
});
}
}
public void startDataProcess() {
int count = 0;
while (json == null && count<20){
Log.e("FacebookConnect",
"Waiting for json (Valid session found)");
waiting(1000);
count++;
}
if (!(count<20)){
noConnectionAlert();
}else{
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
Log.e("FacebookConnect", "startDataProcess() executed");
Intent a = new Intent(FacebookConnectActivity.this,
FacebookDataProcess.class);
a.putExtra(TAG_JSON, json.toString());
startActivity(a);
finish();
}
}
public class SampleDialogListener extends BaseDialogListener {
public void onComplete(Bundle values) {
Log.e("FacebookConnect", "SampleDialogListener complete");
final String postId = values.getString("post_id");
if (postId != null) {
Log.e("FacebookConnect",
"(SampleDialogListener) Dialog Success! post_id="
+ postId);
//mAsyncRunner.request(postId, new WallPostRequestListener());
//Log.e("FacebookConnect", "WallPostRequestListener started");
Toast.makeText(FacebookConnectActivity.this, "Mensagem postada com sucesso!", Toast.LENGTH_LONG).show();
startDataProcess();
} else {
Log.e("FacebookConnect",
"(SampleDialogListener) No wall post made, maybe canceled by user."); // Usuario
// clicou
// em
// Cancelar
startDataProcess();
}
}
public void onError(DialogError e) {
Log.e("FacebookConnect",
"Error authorizing: " + e.toString());
final AlertDialog alertDialog = new AlertDialog.Builder(
FacebookConnectActivity.this).create();
alertDialog.setTitle("Erro");
alertDialog
.setMessage("Erro ao tentar conectar com o facebook, não foi possível publicar em seu mural.");
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
startDataProcess();
alertDialog.dismiss();
}
});
alertDialog.show();
}
public void onCancel() { // Usuario clicou no X do dialog
Log.e("FacebookConnect", "Post to Wall Canceled with \"X\" button");
startDataProcess();
}
}
public class WallPostRequestListener extends BaseRequestListener {
public void onComplete(final String response, final Object state) {
Log.e("FacebookConnect", "WallPostRequestListener complete");
Log.e("FacebookConnect", "(WallPostRequestListener) Got response: "
+ response);
String message = "<empty>";
try {
JSONObject json = Util.parseJson(response);
message = json.getString("message");
} catch (JSONException e) {
Log.e("FacebookConnect",
"(WallPostRequestListener) JSON Error in response: "
+ e.toString());
} catch (FacebookError e) {
Log.e("FacebookConnect",
"(WallPostRequestListener) Facebook Error: "
+ e.getMessage());
// Toast.makeText(ctx, "Erro ao postar no Facebook.",
// Toast.LENGTH_LONG).show();
}
startDataProcess();
}
}
public class SampleRequestListener extends BaseRequestListener {
public void onComplete(final String response, final Object state) {
try {
// process the response here: executed in background thread
Log.e("FacebookConnect", "SampleRequestListener complete");
Log.e("SampleRequestListener",
"Response: " + response.toString());
json = Util.parseJson(response);
Log.e("FacebookConnect", "JSON: " + json.toString());
// postOnWall();
// startDataProcess();
// then post the processed result back to the UI thread
// if we do not do this, an runtime exception will be generated
// e.g. "CalledFromWrongThreadException: Only the original
// thread that created a view hierarchy can touch its views."
} catch (JSONException e) {
Log.e("FacebookConnect",
"JSON Error in response (SampleRequestListener): "
+ e.getMessage());
} catch (FacebookError e) {
Log.e("FacebookConnect",
"Facebook Error at SampleRequestListener: "
+ e.getMessage());
}
}
}
public void postOnWall() {
Looper.prepare();
Bundle params = new Bundle();
params.putString("link", "www.qranio.com");
facebook.dialog(ctx, "feed", params, new SampleDialogListener());
Log.e("FacebookConnect", "SampleDialogListener started");
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
callback = true;
Log.e("FacebookConnect",
"Authentication authorizeCallback called (onActivityResult)");
}
}