android - Facebook friendliest is not retrieving -
i using scrumptious documentation show friends using application. have followed tutorials here
https://developers.facebook.com/docs/android/scrumptious/show-friends?locale=en_gb
i have tried every combination not retrieving friends using application
i have used 3 class
scrumptious.java public class scrumptiousapplication extends application { private list<graphuser> selectedusers; public list<graphuser> getselectedusers() { return selectedusers; } public void setselectedusers(list<graphuser> selectedusers) { this.selectedusers = selectedusers; } } selectionfragment.java package com.example.facebooknew; import java.io.bytearrayinputstream; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.util.arraylist; import java.util.arrays; import java.util.list; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import com.facebook.facebookexception; import com.facebook.facebookoperationcanceledexception; import com.facebook.facebookrequesterror; import com.facebook.httpmethod; import com.facebook.request; import com.facebook.requestasynctask; import com.facebook.response; import com.facebook.session; import com.facebook.session.openrequest; import com.facebook.session.statuscallback; import com.facebook.sessiondefaultaudience; import com.facebook.sessionloginbehavior; import com.facebook.sessionstate; import com.facebook.uilifecyclehelper; import com.facebook.internal.sessiontracker; import com.facebook.internal.utility; import com.facebook.model.graphobject; import com.facebook.model.graphobjectlist; import com.facebook.model.graphuser; import com.facebook.widget.loginbutton; import com.facebook.widget.profilepictureview; import com.facebook.widget.webdialog; import com.facebook.widget.webdialog.oncompletelistener; import android.app.activity; import android.content.context; import android.content.intent; import android.net.uri; import android.os.bundle; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.button; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import android.widget.toast; public class selectionfragment extends fragment{ /* in fragment data user profile returned profile , name etc * (non-javadoc) * @see android.support.v4.app.fragment#oncreateview(android.view.layoutinflater, android.view.viewgroup, android.os.bundle) */ private static final string tag = "selectionfragment"; private static final list<string> permissions = arrays.aslist( "email","user_location"); private static final int reauth_activity_code = 100; private profilepictureview profilepictureview; private textview usernameview; private textview firstname; private textview lastname; private textview location; private uilifecyclehelper uihelper; private list<graphuser> selectedusers; private listview listview; private list<baselistelement> listelements; private static final string friends_key = "friends"; private textview userinfotextview; private button sendrequestbutton; private string requestid; private session.statuscallback callback = new session.statuscallback() { @override public void call(final session session, final sessionstate state, final exception exception) { onsessionstatechange(session, state, exception); } }; @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); // check incoming notification. save info uri intenturi = getactivity().getintent().getdata(); if (intenturi != null) { string requestidparam = intenturi.getqueryparameter("request_ids"); if (requestidparam != null) { string array[] = requestidparam.split(","); requestid = array[0]; log.i(tag, "request id: "+requestid); toast.maketext(getactivity().getapplicationcontext(),requestid, toast.length_long).show(); } } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { super.oncreateview(inflater, container, savedinstancestate); view view = inflater.inflate(r.layout.selection, container, false); // find user's profile picture custom view profilepictureview = (profilepictureview) view.findviewbyid(r.id.selection_profile_pic); profilepictureview.setcropped(true); // find user's name view usernameview = (textview) view.findviewbyid(r.id.selection_user_name); firstname= (textview) view.findviewbyid(r.id.selection_first_name); //lastname = (textview) view.findviewbyid(r.id.selection_last_name); //location = (textview) view.findviewbyid(r.id.selection_location); //userinfotextview = (textview) view.findviewbyid(r.id.userinfotextview); sendrequestbutton=(button) view.findviewbyid(r.id.sendrequestbutton); uihelper = new uilifecyclehelper(getactivity(), callback); uihelper.oncreate(savedinstancestate); session session = session.getactivesession(); if (session != null && session.isopened()) { // user's data makemerequest(session); } sendrequestbutton.setonclicklistener(new onclicklistener(){ @override public void onclick(view arg0) { // todo auto-generated method stub sendrequestdialog(); } }); // find list view listview = (listview) view.findviewbyid(r.id.selection_list); // set list view items, based on list of // baselistelement items listelements = new arraylist<baselistelement>(); // add item friend picker listelements.add(new peoplelistelement(0)); // set list view adapter listview.setadapter(new actionlistadapter(getactivity(), r.id.selection_list, listelements)); if (savedinstancestate != null) { // restore state each list element (baselistelement listelement : listelements) { listelement.restorestate(savedinstancestate); } } return view; } private void makemerequest(final session session) { // make api call user data , define // new callback handle response. request request = request.newmerequest(session, new request.graphusercallback() { @override public void oncompleted(graphuser user, response response) { // if response successful if (session == session.getactivesession()) { if (user != null) { // set id profilepictureview // view in turn displays profile picture. profilepictureview.setprofileid(user.getid()); // set textview's text user's name. usernameview.settext(user.getname()); //firstname.settext(user.getfirstname()); //lastname.settext(user.getlastname()); //location.settext(user.getproperty("email").tostring()); } } if (response.geterror() != null) { // handle errors, later. } } }); request.executeasync(); } @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == reauth_activity_code) { uihelper.onactivityresult(requestcode, resultcode, data); } else if (resultcode == activity.result_ok) { // nothing }else if (resultcode == activity.result_ok && requestcode >= 0 && requestcode < listelements.size()) { listelements.get(requestcode).onactivityresult(data); } } private void onsessionstatechange(final session session, sessionstate state, exception exception) { if (session != null && session.isopened()) { // user's data. if (state.isopened() && requestid != null) { getrequestdata(requestid); } if (state.isopened()) { sendrequestbutton.setvisibility(view.visible); } else if (state.isclosed()) { sendrequestbutton.setvisibility(view.invisible); } makemerequest(session); } } @override public void onresume() { super.onresume(); uihelper.onresume(); } @override public void onsaveinstancestate(bundle bundle) { super.onsaveinstancestate(bundle); (baselistelement listelement : listelements) { listelement.onsaveinstancestate(bundle); } uihelper.onsaveinstancestate(bundle); } @override public void onpause() { super.onpause(); uihelper.onpause(); } @override public void ondestroy() { super.ondestroy(); uihelper.ondestroy(); } private interface mygraphlanguage extends graphobject { // getter id field string getid(); // getter name field string getname(); } private class actionlistadapter extends arrayadapter<baselistelement> { private list<baselistelement> listelements; public actionlistadapter(context context, int resourceid, list<baselistelement> listelements) { super(context, resourceid, listelements); this.listelements = listelements; // set observer list item changes // refresh view. (int = 0; < listelements.size(); i++) { listelements.get(i).setadapter(this); } } @override public view getview(int position, view convertview, viewgroup parent) { view view = convertview; if (view == null) { layoutinflater inflater = (layoutinflater) getactivity() .getsystemservice(context.layout_inflater_service); view = inflater.inflate(r.layout.listitem, null); } baselistelement listelement = listelements.get(position); if (listelement != null) { view.setonclicklistener(listelement.getonclicklistener()); imageview icon = (imageview) view.findviewbyid(r.id.icon); textview text1 = (textview) view.findviewbyid(r.id.text1); textview text2 = (textview) view.findviewbyid(r.id.text2); if (icon != null) { icon.setimagedrawable(listelement.geticon()); } if (text1 != null) { text1.settext(listelement.gettext1()); } if (text2 != null) { text2.settext(listelement.gettext2()); } else{ toast.maketext(getactivity().getapplicationcontext(),"no data received " ,toast.length_long).show(); } } return view; } } /* private class used populate friend , bring friends list*/ private class peoplelistelement extends baselistelement { public peoplelistelement(int requestcode) { super(getactivity().getresources().getdrawable(r.drawable.add_friends), getactivity().getresources().getstring(r.string.action_people), getactivity().getresources().getstring(r.string.action_people_default), requestcode); } /* event trigger picker activity*/ @override protected view.onclicklistener getonclicklistener() { return new view.onclicklistener() { @override public void onclick(view view) { // nothing startpickeractivity(pickeractivity.friend_picker, getrequestcode()); } }; } private void setuserstext() { string text = null; if (selectedusers != null) { // if there 1 friend if (selectedusers.size() == 1) { text = string.format(getresources() .getstring(r.string.single_user_selected), selectedusers.get(0).getname()); } else if (selectedusers.size() == 2) { // if there 2 friends text = string.format(getresources() .getstring(r.string.two_users_selected), selectedusers.get(0).getname(), selectedusers.get(1).getname()); } else if (selectedusers.size() > 2) { // if there more 2 friends text = string.format(getresources() .getstring(r.string.multiple_users_selected), selectedusers.get(0).getname(), (selectedusers.size() - 1)); } } if (text == null) { // if no text, use placeholder text text = getresources() .getstring(r.string.action_people_default); } // set text in list element. notify // adapter data has changed // refresh list view. settext2(text); } @override protected void onactivityresult(intent data) { selectedusers = ((scrumptiousapplication) getactivity() .getapplication()) .getselectedusers(); setuserstext(); notifydatachanged(); } @override protected void onsaveinstancestate(bundle bundle) { if (selectedusers != null) { bundle.putbytearray(friends_key, getbytearray(selectedusers)); } } private byte[] getbytearray(list<graphuser> users) { // convert list of graphusers list of string // each element json representation of // graphuser can stored in bundle list<string> usersasstring = new arraylist<string>(users.size()); (graphuser user : users) { usersasstring.add(user.getinnerjsonobject().tostring()); } try { bytearrayoutputstream outputstream = new bytearrayoutputstream(); new objectoutputstream(outputstream).writeobject(usersasstring); return outputstream.tobytearray(); } catch (ioexception e) { log.e(tag, "unable serialize users.", e); } return null; } private list<graphuser> restorebytearray(byte[] bytes) { try { @suppresswarnings("unchecked") list<string> usersasstring = (list<string>) (new objectinputstream (new bytearrayinputstream(bytes))) .readobject(); if (usersasstring != null) { list<graphuser> users = new arraylist<graphuser> (usersasstring.size()); (string user : usersasstring) { graphuser graphuser = graphobject.factory .create(new jsonobject(user), graphuser.class); users.add(graphuser); } return users; } } catch (classnotfoundexception e) { log.e(tag, "unable deserialize users.", e); } catch (ioexception e) { log.e(tag, "unable deserialize users.", e); } catch (jsonexception e) { log.e(tag, "unable deserialize users.", e); } return null; } @override protected boolean restorestate(bundle savedstate) { byte[] bytes = savedstate.getbytearray(friends_key); if (bytes != null) { selectedusers = restorebytearray(bytes); setuserstext(); return true; } return false; } } /* function responsible launching freind picker activity*/ private void startpickeractivity(uri data, int requestcode) { intent intent = new intent(); intent.setdata(data); intent.setclass(getactivity(), pickeractivity.class); startactivityforresult(intent, requestcode); } public void facebooksession(session session){ //session= session.getactivesession(); session.openrequest request = new session.openrequest(this); request.setpermissions(arrays.aslist("basic_info","email","location")); request.setcallback( new session.statuscallback() { // callback when session changes state @override public void call(session session, sessionstate state, exception exception) { if (session.isopened()) { request.newmerequest(session, new request.graphusercallback() { @override public void oncompleted(graphuser user, response response) { if (user != null) { toast.maketext(selectionfragment.this.getactivity(), "user email is:"+user.getproperty("email"), toast.length_short).show(); } else { toast.maketext(selectionfragment.this.getactivity(), "error user null", toast.length_short).show(); } } }).executeasync(); } } }); //end of call; session.openforread(request); //now request above } private void sendrequestdialog() { bundle params = new bundle(); params.putstring("message", "learn how make android apps social"); params.putstring("data", "https://play.google.com/store/apps/details?id=de.j4velin.mapsmeasure&hl=en"); webdialog requestsdialog = ( new webdialog.requestsdialogbuilder(getactivity(), session.getactivesession(), params)) .setoncompletelistener(new oncompletelistener() { @override public void oncomplete(bundle values, facebookexception error) { if (error != null) { if (error instanceof facebookoperationcanceledexception) { toast.maketext(getactivity().getapplicationcontext(), "request cancelled", toast.length_short).show(); } else { toast.maketext(getactivity().getapplicationcontext(), "network error", toast.length_short).show(); } } else { final string requestid = values.getstring("request"); if (requestid != null) { toast.maketext(getactivity().getapplicationcontext(), "request sent", toast.length_short).show(); toast.maketext(getactivity().getapplicationcontext(), requestid, toast.length_short).show(); } else { toast.maketext(getactivity().getapplicationcontext(), "request cancelled", toast.length_short).show(); } } } }) .build(); requestsdialog.show(); } private void getrequestdata(final string inrequestid) { // create new request http // request id graph path. request request = new request(session.getactivesession(), inrequestid, null, httpmethod.get, new request.callback() { @override public void oncompleted(response response) { // process returned response graphobject graphobject = response.getgraphobject(); facebookrequesterror error = response.geterror(); // default message string message = "incoming request"; if (graphobject != null) { // check if there data if (graphobject.getproperty("data") != null) { try { // data, parse info key/value info jsonobject dataobject = new jsonobject((string)graphobject.getproperty("data")); // value key - badge_of_awesomeness string badge = dataobject.getstring("badge_of_awesomeness"); // value key - social_karma string karma = dataobject.getstring("social_karma"); // sender's name jsonobject fromobject = (jsonobject) graphobject.getproperty("from"); string sender = fromobject.getstring("name"); string title = sender+" sent gift"; // create text alert based on sender // , data message = title + "\n\n" + "badge: " + badge + " karma: " + karma; } catch (jsonexception e) { message = "error getting request info"; } } else if (error != null) { message = "error getting request info"; } } toast.maketext(getactivity().getapplicationcontext(), message, toast.length_long).show(); } }); // execute request asynchronously. request.executebatchasync(request); } } , have abstarct baselist element class
Comments
Post a Comment