我正在尝试使用从JSON网址读取的数据填充对象。
public JSONHandler(final ImageButton canli, final ImageButton indie, final ImageButton easy, final ImageButton karisik, final ImageButton rock) {
request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
channelObject = response.getJSONObject("channel0");
song = channelObject.getString("song");
artist = channelObject.getString("artist");
duration = channelObject.getInt("duration");
playedAt = channelObject.getInt("playedat");
channelImg = channelObject.getString("image_extralarge");
channel0 = new Channel(song, artist, duration, playedAt, channelImg);
Picasso.get().load(channel1.getImg()).fit().into(indie);
我正在使用此结构填充ImageButton和其他东西。如您所见,我只能在方法内部填充按钮等。是否可以在我的方法中使用channel1对象并保存以供以后使用?
答案 0 :(得分:0)
您可以将每个字段保存到sharedPreference,也可以:
将此添加到gradle文件中,
实现'com.google.code.gson:gson:2.8.5'
放置数据:
SharedPreferences prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
Gson gson = new Gson();
String res = gson.toJson(channel0);
editor.putString("data", res);
editor.apply();
获取数据:
SharedPreferences prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
Gson gson = new Gson();
String data = prefs.getString("data", "");
Channel obj = gson.fromJson(data, Channel.class);