publish on wall with Facebook sdk 3.0 and Android -
i've read lots of tutorial , answers here, i'm not capable implement button clicked , publish text on wall (with login done programmatically). tried fragment , 2 buttons, code launches a
05-14 09:26:32.548: e/androidruntime(5080): java.lang.unsupportedoperationexception: session: attempt made request new permissions session has pending request.
the code is:
private static final list<string> permissions = arrays.aslist("publish_actions"); private static final string pending_publish_key = "pendingpublishreauthorization"; private boolean pendingpublishreauthorization = false; private static final string tag = "mainfragment"; private static final string publish_actions_permission = null; private uilifecyclehelper uihelper; private loginbutton login; private button sharebutton; private session.statuscallback callback = new session.statuscallback() { @override public void call(session session, sessionstate state, exception exception) { onsessionstatechange(session, state, exception); } }; @override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { view view = inflater.inflate(r.layout.facebook_fragment, container, false); sharebutton = (button) view.findviewbyid(r.id.sharebutton); login=(loginbutton)view.findviewbyid(r.id.loginbutton); login.setpublishpermissions(permissions); if (savedinstancestate != null) { pendingpublishreauthorization = savedinstancestate.getboolean(pending_publish_key, false); } sharebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { publishstory(); } }); return view; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); uihelper = new uilifecyclehelper(getactivity(), callback); uihelper.oncreate(savedinstancestate); } @override public void onresume() { super.onresume(); session session = session.getactivesession(); if (session != null && (session.isopened() || session.isclosed()) ) { onsessionstatechange(session, session.getstate(), null); } uihelper.onresume(); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); uihelper.onactivityresult(requestcode, resultcode, data); } @override public void onpause() { super.onpause(); uihelper.onpause(); } @override public void ondestroy() { super.ondestroy(); uihelper.ondestroy(); } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putboolean(pending_publish_key, pendingpublishreauthorization); uihelper.onsaveinstancestate(outstate); } private void onsessionstatechange(session session, sessionstate state, exception exception) { if (state.isopened()) { log.i(tag, "logged in..."); } else if (state.isclosed()) { log.i(tag, "logged out..."); } if (pendingpublishreauthorization && state.equals(sessionstate.opened_token_updated)) { pendingpublishreauthorization = false; publishstory(); } } private void publishstory() { session session = session.getactivesession(); if (session != null){ // check publish permissions list<string> permissions = session.getpermissions(); if (!issubsetof(permissions, permissions)) { pendingpublishreauthorization = true; session.newpermissionsrequest newpermissionsrequest = new session .newpermissionsrequest(this, permissions); session.requestnewpublishpermissions(newpermissionsrequest); return; } bundle postparams = new bundle(); postparams.putstring("name", "facebook sdk android"); postparams.putstring("caption", "build great social apps , more installs."); postparams.putstring("description", "the facebook sdk android makes easier , faster develop facebook integrated android apps."); postparams.putstring("link", "https://developers.facebook.com/android"); request.callback callback= new request.callback() { public void oncompleted(response response) { jsonobject graphresponse = response.getgraphobject().getinnerjsonobject(); string postid = null; try { postid = graphresponse.getstring("id"); } catch (jsonexception e) { log.i(tag,"json error "+ e.getmessage()); } facebookrequesterror error = response.geterror(); if (error != null) { toast.maketext(getactivity().getapplicationcontext(),error.geterrormessage(),toast.length_short).show(); } else { toast.maketext(getactivity().getapplicationcontext(), postid,toast.length_long).show(); } } }; request request = new request(session, "me/feed", postparams, httpmethod.post, callback); requestasynctask task = new requestasynctask(request); task.execute(); } } private boolean issubsetof(collection<string> subset, collection<string> superset) { (string string : subset) { if (!superset.contains(string)) { return false; } } return true; } }
where's problem? i'm going crazy! much!
i think problem you're explicitly calling onsessionstatechange in onresume method, callback have called anyways (via onactivityresult, when permission request comes back). results in 2 publish permission requests simultaneously, why you're seeing error. try removing onsessionstatechange method in onresume().
Comments
Post a Comment