java - Cannot refer to the non-final local variable jSONString defined in an enclosing scope -
i trying pass json string server after 60 seconds. @ moment facing problem when try execute myasynctask thread; jsonstring not accessable @ line new myasynctask().execute(jsonstring); , getting error cannot refer non-final local variable jsonstring defined in enclosing scope
this part of code being called onlocationchanged method in inner class of mainactivity:
string jsonstring = converttojson(plong, plat, formatted); postdata sender = new postdata(); sender.timer(jsonstring); postdata class:
public class postdata { string jsonstring; handler handler = new handler(); public postdata() { super(); } public string getjsonstring() { return jsonstring; } public void setjsonstring(string jsonstring) { this.jsonstring = jsonstring; } public void timer(string jsonstring) { this.jsonstring = jsonstring; new thread(new runnable() { @override public void run() { boolean run = true; while (run) { handler.postdelayed(new runnable() { @override public void run() { new myasynctask().execute(jsonstring); //how can access variable'jsonstring' here? } }, 5000); } } }).start(); } class myasynctask extends asynctask<string, integer, void> { @override protected void doinbackground(string... params) { system.out.println("the output of : doinbackground " +params[0]); //the connection code. return null; } } }
use jsonstring declared in postdata trying access timer method parameter not final :
new myasynctask().execute(postdata.this.jsonstring);
Comments
Post a Comment