存储排球请求数据 [英] Store Volley Request data
问题描述
我正在学习自己编程,并且遇到了一个我认为很容易解决的问题.
I'm learning to program by myself and I am facing a problem which I believe is easy to solve.
我正在使用Google Volley进行请求,并且想知道我如何存储请求中的信息,以便我在onCreate中与他们一起使用.
I'm making requests using Google Volley and like to know how I store the information coming in the request for me to work with them in my onCreate.
public void parseJSON(){
StringRequest getRequest = new StringRequest(Request.Method.GET,activity.getString(R.string.URL),
new Response.Listener() {
@Override
public void onResponse(String response) {
I need to store the data for use in the method onCreate.
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error.Response", error.toString());
}
});
request.add(getRequest);
}
我不知道如何将此单独方法的数据传递给main.
I do not know how to pass the data of this separate method to main.
我一个人开始,已经做了很多研究,但是这个简单的问题却一无所获.
I'm starting alone and has researched a lot, but found nothing with this simple problem.
还是谢谢你!
推荐答案
您可以使用SharedPreferences来存储数据.创建一个方法可以做到这一点.
You can use SharedPreferences to store the data. Create a method to do that.
private void sharedResponse(String response) {
SharedPreferences m = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = m.edit();
editor.putString("Response", response);
editor.commit();
}
然后从onResponse中调用此方法.
And call this method from your onResponse.
@Override
public void onResponse(String response) {
//I need to store the data for use in the method onCreate.
sharedResponse(response);
}
您可以通过以下方式从其他班级访问此数据:
You can access this data from other class by
SharedPreferences m = PreferenceManager.getDefaultSharedPreferences(this);
mResponse = m.getString("Response", "");
这篇关于存储排球请求数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!