我如何在Android的onResponse之外使用变量? [英] How do i use a variable outside the onResponse in Android?
问题描述
我创建了一个活动,将一些记录插入到mysql数据库中。我声明了一个名为 lastInsertId
的全局变量。当我尝试 println
onResponse
方法中的变量时,可以正常工作,但是当我尝试 println
在方法外返回 null
。我需要在方法之外使用这个变量。可以做什么?
这里是我的代码:
I have created an activity in which i insert some records into a mysql database. I declared a global variable named lastInsertId
. When i try to println
the variable inside the onResponse
method, works fine but when i try to println
outside the method returns null
. I need to use this variable also outside the method. What can be done?
Here is my code:
String insertUrl = "http://localhost/file.php";
String lastInsertId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
lastInsertId = response.toString();
System.out.println(lastInsertId); // returns the lastInsertId
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
// parameters
return parameters;
}
};
requestQueue.add(request);
System.out.println(lastInsertId); // get's null
}
谢谢!
Thanks!
推荐答案
我弄明白了。大约一年后我回答了这个问题,因为我看到这个帖子有几百个访问者。希望我的回答能够帮助其他功能访问者从onResponse方法获取数据。这里是代码:
I figure it out. I answered this question after about a year, beacuse i saw that this post had a few hundred visitor. Hope my answer will help other feature visitors to get data out from onResponse method. Here is the code:
String insertUrl = "http://localhost/file.php";
String lastInsertId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
lastInsertId = response.toString();
System.out.println(lastInsertId); // returns the lastInsertId
callback.onSuccess(lastInsertId);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
// parameters
return parameters;
}
};
requestQueue.add(request);
}
public interface VolleyCallback{
void onSuccess(ArrayList<Data> dataArrayList);
}
这是我们在Activity中需要的代码。
And this is the code we need inside the Activity.
public void onResume(){
super.onResume();
getString(new VolleyCallback(){
@Override
public void onSuccess(String result){
System.out.println(result); // returns the value of lastInsertId
}
});
}
这篇关于我如何在Android的onResponse之外使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!