[英]Get a variable from an anonymous class
I have the following situation : 我有以下情况:
final String value = null;
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
value = response;
}
});
System.out.println(value);
I am running this code from my main class. 我正在从我的主班运行这段代码。
Now I need to be able to use the variable (String Response) from the over ridden class in my main class. 现在,我需要能够使用主类中被重写的类中的变量(字符串响应)。
How am i meant to go about getting this variable, as clearly what i have doesnt work. 我要怎么去得到这个变量,因为我所拥有的显然是行不通的。
ps, suggest an edit for the name, I didnt know what to call it. ps,建议对名称进行编辑,我不知道该怎么称呼。
Your problem doesn't have to do with classes; 您的问题与课程无关。 it's that you're using asynchronous code synchronously. 就是您正在同步使用异步代码。 Anything you want to do with the string response must be within the onSuccess
handler (or a function called by it). 您想对字符串响应进行的任何操作都必须在onSuccess
处理程序(或由其调用的函数)内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.