我正在使用Facebook连接SSO登录/注册用户到我的应用程序,并且想要在每次登录后让用户在墙上发帖。
但是,在requestListener的onComplete(显示墙上对话框,用户触及Share)上,我收到了错误的回复。没有找到FB SDK文档中的含义,但是我的墙上发布了任何内容。响应应该返回一个JSON格式化的字符串,所以当试图执行“message = json.getString(”message“);”时,我得到一个FacebookError女巫只是打印“请求失败”......
logcat的:
07-27 12:58:09.170: W/FacebookConnect(26086): Activity Started
07-27 12:58:12.300: W/FacebookConnect(26086): User authorized
07-27 12:58:12.300: E/FacebookConnect(26086): onComplete called at facebook.authorize
07-27 12:58:12.300: E/FacebookConnect(26086): Access Token: AAAC1sJLmgzkBAD0M40SQLgluPU...
07-27 12:58:12.860: D/webviewglue(26086): nativeDestroy view: 0x223888
07-27 12:58:13.420: I/SqliteDatabaseCpp(26086): sqlite returned: error code = 14, msg = cannot open file at line 27701 of [8609a15dfa], db=/data/data/androidhive.dashboard/databases/webview.db
07-27 12:58:13.420: I/SqliteDatabaseCpp(26086): sqlite returned: error code = 14, msg = os_unix.c: open() at line 27701 - "" errno=2 path=/CachedGeoposition.db, db=/data/data/androidhive.dashboard/databases/webview.db
07-27 12:58:14.670: D/SampleRequestListener(26086): Response: {"id":"1123123","name"...
07-27 12:58:14.680: E/FacebookConnect(26086): JSON {"work":[{"position"...
07-27 12:58:17.150: E/SampleDialogListener(26086): Dialog Success! post_id=1003157...
07-27 12:58:17.720: E/WallPostRequestListener(26086): Got response: false
07-27 12:58:17.720: E/WallPostRequestListener(26086): Facebook Error: request failed
我的代码(WallPostRequestListener类中出错):
public class FacebookConnectActivity extends Activity {
private static final String TAG_JSON = "json";
static Facebook facebook = new Facebook("11111111111111");
AsyncFacebookRunner mAsyncRunner;
private SharedPreferences mPrefs;
JSONObject json;
Context ctx = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resgatar_produto_layout);
Log.w("FacebookConnect", "Activity Started");
mPrefs = getPreferences(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);
}
if(expires != 0) {
facebook.setAccessExpires(expires);
}
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.w("FacebookConnect", "User authorized");
Log.e("FacebookConnect", "onComplete called at facebook.authorize");
Log.e("FacebookConnect", "Access Token: "+facebook.getAccessToken().toString());
mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me", new SampleRequestListener());
Bundle params = new Bundle();
params.putString("link", "www.qranio.com");
facebook.dialog(ctx, "feed", params, new SampleDialogListener());
}
@Override
public void onFacebookError(FacebookError error) {
Log.w("FacebookConnect", "Error authorizing: "+error.toString());
}
@Override
public void onError(DialogError e) {}
@Override
public void onCancel() {}
});
}
}
public static void logoutFB(){
if(facebook.isSessionValid()){
facebook.setAccessToken(null);
}
}
public void startDataProcess(){
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) {
final String postId = values.getString("post_id");
if (postId != null) {
Log.e("SampleDialogListener", "Dialog Success! post_id=" + postId);
mAsyncRunner.request(postId, new WallPostRequestListener());
} else {
Log.e("SampleDialogListener", "No wall post made"); //Usuario clicou em Cancelar
startDataProcess();
}
}
public void onCancel(){ //Usuario clicou no X do dialog
Log.w("FacebookConnect", "Post to Wall Canceled");
startDataProcess();
}
}
public class WallPostRequestListener extends BaseRequestListener {
public void onComplete(final String response, final Object state) {
Log.e("WallPostRequestListener", "Got response: " + response);
String message = "<empty>";
try {
JSONObject json = Util.parseJson(response);
message = json.getString("message");
} catch (JSONException e) {
Log.e("WallPostRequestListener", "JSON Error in response");
} catch (FacebookError e) {
Log.e("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.d("SampleRequestListener", "Response: " + response.toString());
json = Util.parseJson(response);
Log.e("FacebookConnect", "JSON " + json.toString());
//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.w("Facebook-Example", "JSON Error in response");
} catch (FacebookError e) {
Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
try{
mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me", new SampleRequestListener());
Bundle params = new Bundle();
params.putString("link", "www.qranio.com");
//params.putString("display", "touch");
facebook.dialog(ctx, "feed", params, new SampleDialogListener());
Log.e("FacebookConnect", "Authentication authorizeCallback called (onActivityResult)");
Log.e("FacebookConnect", "Access Token: "+facebook.getAccessToken().toString());
//facebook.
//if (facebook.isSessionValid()){
//}else{
//Log.e("FacebookConnect", "Invalid facebook session while trying to fetch user data");
//}*/
}catch(FacebookError fbe){
Log.e("FacebookConnect", "onActivityResult FacebookError " + fbe.toString());
}catch (Exception e){
Log.e("FacebookConnect", "onActivityResult Exception " + e.toString());
}
}
}