java - Getter Setter method being called at wrong order -
i need help. im making app parses bible , displays in webview. implemented next/passage feature app. calling correct json address iv´e got issues order in getters , setters being called.
this code block calls static method in order string response:
string previouspassage = backnextgo.getpassage (currentchapter, chapterselected); l.m("previouspassage");
and methods calls:
public class backnextgo { private backnextgo() { } private static string requesturl; private static requestqueue queue; private static string s; public static string getpassage(string data, int chapterselected) { setrequesturl(data, chapterselected); queue = volleysingleton.getinstance().getrequestqueue(); sendjsonrequest(); return getdata(); } private static void setrequesturl(string s, int chapterselected) { requesturl = constants.display_data_url_part_1 + s.replaceall("\\s+", "").replaceall("(\\d+)(?!.*\\d)", string.valueof(chapterselected)) + constants.display_data_url_part_2 + constants.bible_api_key; } private static string getrequesturl() { return requesturl; } private static void sendjsonrequest() { jsonobjectrequest request = new jsonobjectrequest(request.method.get, getrequesturl(), new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { if (response == null || response.length() == 0) { } else { try { setdata(response .getstring(constants.keys.books.key_selected_chapter)); } catch (jsonexception e) { e.printstacktrace(); } } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }); queue.add(request); } public static string getdata() { counter++; l.m("getter method"); return s; } private static void setdata(string data) { s = data; l.m("setter method"); } }
i have placed toasts on getter , setter method on method calling backnextpassage static class.
and order is:
1) getter method
2) calling code block (string)
3) setter method
to understanding, order should be: first setter method. becouse called within "sendjsonrequest" method , getdata or getter method, , calling method.
this sucks becouse everytime call backnextpassage class im getting data set last call. appreciated =)
ps:
l("some random text"); these normal toasts like: toast.maketext(context, "........show();
update!:
so tried synchronizing methods hangs until app crashes... heres code: (maybe can point out mistake? =) )
public class backnextgo { private static backnextgo instance = null; private string requesturl; private requestqueue queue; private string s; private backnextgo() { } public static backnextgo getinstance() { if (instance == null) { instance = new backnextgo(); } return instance; } public string getpassage(string data, int chapterselected) { setrequesturl(data, chapterselected); queue = volleysingleton.getinstance().getrequestqueue(); synchronized (this) { sendjsonrequest(); try { wait(); } catch (interruptedexception e) { e.printstacktrace(); } } return getdata(); } private void setrequesturl(string s, int chapterselected) { requesturl = constants.display_data_url_part_1 + s.replaceall("\\s+", "").replaceall("(\\d+)(?!.*\\d)", string.valueof(chapterselected)) + constants.display_data_url_part_2 + constants.bible_api_key; system.out.println(requesturl); } private string getrequesturl() { return requesturl; } private void sendjsonrequest() { jsonobjectrequest request = new jsonobjectrequest(request.method.get, getrequesturl() , new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { if (response == null || response.length() == 0) { } else { try { setdata(response.getstring(constants.keys.books.key_selected_chapter)); } catch (jsonexception e) { e.printstacktrace(); } } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }); queue.add(request); } private void setdata(string data) { synchronized (this) { s = data; notify(); } } public string getdata() { return s; } }
and again... heres code calling class:
string previouspassage = backnextgo.getinstance().getpassage(currentchapter, chapterselected);
so create interface in backnextgo
public class backnextgo { public interface ondata { public void updatedata(string data); } private backnextgo() { } private static string requesturl; private static requestqueue queue; private static string s; private ondata sondata = null; public static string getpassage(string data, int chapterselected, ondata ondatacallback) { sondata = ondatacallback; setrequesturl(data, chapterselected); queue = volleysingleton.getinstance().getrequestqueue(); sendjsonrequest(); return getdata(); } private static void setrequesturl(string s, int chapterselected) { requesturl = constants.display_data_url_part_1 + s.replaceall("\\s+", "").replaceall("(\\d+)(?!.*\\d)", string.valueof(chapterselected)) + constants.display_data_url_part_2 + constants.bible_api_key; } private static string getrequesturl() { return requesturl; } private static void sendjsonrequest() { jsonobjectrequest request = new jsonobjectrequest(request.method.get, getrequesturl(), new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { if (response == null || response.length() == 0) { } else { try { setdata(response .getstring(constants.keys.books.key_selected_chapter)); } catch (jsonexception e) { e.printstacktrace(); } } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }); queue.add(request); } public static string getdata() { counter++; l.m("getter method"); return s; } private static void setdata(string data) { s = data; if(sondata != null) { sondata.updatedata(data); } l.m("setter method"); } }
then in calling class like...
string previouspassage; ondata ondatacallback = new ondata { public void updatedata(string data) { previouspassage = data; } }; backnextgo.getinstance().getpassage(currentchapter, chapterselected, ondatacallback);
good luck, happy coding.
Comments
Post a Comment