Android 向全局变量添加截击响应
我使用截取库获取数据。我使用HashMap来存储响应数据。当我从截击请求中提取数据时,它显示为空。我知道我应该在response方法中使用这个Hashmap。但我需要把它放在一个全局变量中。有什么建议吗Android 向全局变量添加截击响应,android,android-volley,Android,Android Volley,我使用截取库获取数据。我使用HashMap来存储响应数据。当我从截击请求中提取数据时,它显示为空。我知道我应该在response方法中使用这个Hashmap。但我需要把它放在一个全局变量中。有什么建议吗 String liveURL = BASE_URL + "livescores?api_token=" + API_KEY; HashMap<String, String> hashMap = new HashMap<>(); Json
String liveURL = BASE_URL + "livescores?api_token=" + API_KEY;
HashMap<String, String> hashMap = new HashMap<>();
JsonObjectRequest livescoresRequest = new JsonObjectRequest(Request.Method.GET,
liveURL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
final JSONArray liveArray = response.getJSONArray("data");
for (int i = 0; i < liveArray.length(); i++) {
JSONObject data = liveArray.getJSONObject(i);
String liveID = data.getString("id");
final String league_id = data.getString("league_id");
hashMap.put(league_id, liveID);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error", error.toString());
}
});
Volley.newRequestQueue(getContext()).add(livescoresRequest);
Log.i("HASHMAP", hashMap.toString());
String liveURL=BASE_URL+“livescores?api_token=“+api_KEY;
HashMap HashMap=新的HashMap();
JsonObjectRequest livescoresRequest=新的JsonObjectRequest(Request.Method.GET,
liveURL,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
final JSONArray liveArray=response.getJSONArray(“数据”);
对于(int i=0;i
create interface callback向我推荐一个完美的链接遵循本教程:它与我的编码相同。如何将响应保存在建议教程中的全局变量中您可以通过多种方式保存在类应用程序、共享首选项、sqlite或room上。