![](/img/trans.png)
[英]Setting value of class variable from String Request Volley in Android
[英]Setting data from Android Volley to variable
我有一个Volley请求,我试图将结果设置在一个变量中,但是出现错误“从内部类访问变量'stuff',需要声明为final”。 问题是,一旦我将其声明为final,就不允许将变量设置为等于响应(就像我在try块中尝试的那样)。 我四处寻找讨论该问题的其他问题,但没有一个问题对我的情况有所帮助。 这是代码:
public ArrayList getStuff() {
ArrayList<JSONObject> stuff;
new Thread(new Runnable() {
@Override
public void run() {
// Instantiate the RequestQueue.
RequestQueue queue = MySingleton.getInstance(getApplicationContext()).getRequestQueue();
String url ="http://localhost:6000/stuff";
RequestFuture<String> future = RequestFuture.newFuture();
// Testing out blocking
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, future, future);
// Add the request to the RequestQueue.
queue.add(stringRequest);
try {
String response = future.get(30, TimeUnit.SECONDS);
stuff = response;
Log.i("tutors after blocked: ", tutors);
} catch(InterruptedException e) {
e.printStackTrace();
} catch(ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}).start();
// I want to be able to return 'stuff' here
return stuff;
}
只需添加final一词,如下所示。
public ArrayList getStuff() {
final ArrayList<JSONObject> stuff; <--Add final here
new Thread(new Runnable() {
@Override
public void run() {
// Instantiate the RequestQueue.
RequestQueue queue = MySingleton.getInstance(getApplicationContext()).getRequestQueue();
String url ="http://localhost:6000/stuff";
RequestFuture<String> future = RequestFuture.newFuture();
// Testing out blocking
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, future, future);
// Add the request to the RequestQueue.
queue.add(stringRequest);
try {
String response = future.get(30, TimeUnit.SECONDS);
stuff = response;
Log.i("tutors after blocked: ", tutors);
} catch(InterruptedException e) {
e.printStackTrace();
} catch(ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}).start();
// I want to be able to return 'stuff' here
return stuff;
}
只需将变量声明移到方法外即可。 这意味着它已经是该类的全局变量
ArrayList<JSONObject> stuff;
public ArrayList getStuff() {
new Thread(new Runnable() {
@Override
public void run() {
// Instantiate the RequestQueue.
RequestQueue queue = MySingleton.getInstance(getApplicationContext()).getRequestQueue();
String url ="http://localhost:6000/stuff";
RequestFuture<String> future = RequestFuture.newFuture();
// Testing out blocking
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, future, future);
// Add the request to the RequestQueue.
queue.add(stringRequest);
try {
String response = future.get(30, TimeUnit.SECONDS);
stuff = response;
Log.i("tutors after blocked: ", tutors);
} catch(InterruptedException e) {
e.printStackTrace();
} catch(ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}).start();
// I want to be able to return 'stuff' here
return stuff;
}
深入研究之后,我想到了SharedPreferences ,
如果您确实需要外部响应数据,则可以使用SharedPreferences将响应值存储在某个变量中,并在需要时在外部读取相同的值。
为变量设置值
String MyPREFERENCES = "MyPrefs";
SharedPreferences.Editor editor = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE).edit();
editor.putString("returnResponse", "success");
editor.apply();
获取变量的值
String return_value = sharedpreferences.getString("returnResponse", "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.