我有以下代码:
JSONArray arr = new JSONArray();
JSONObject obj = new JSONObject();
obj.put("message", message);
obj.put("flag", flag);
obj.put("intent", intent);
arr.put(obj);
Utility.saveDataLocally(getApplicationContext(), Constants.SOME_CONST, arr.toString());
其中intent
是Intent
类的对象。现在,在Utility中保存这个JSONArray后,我想从另一个活动中检索这个保存的JSONArray,所以我正在做同样的事情:
String s = Utility.getSaveDataLocally(this, Constants.SOME_CONST);
JSONArray arr = new JSONArray(s);
//Below works
String m = arr.getJSONObject(0).getString("message");
//But below doesn't (obviously).
Intent i = arr.getJSONObject(0).getIntent("intent");
请告诉我如何实现上述目标?也就是说,通过Android中的JSONObject传递一个intent,并从另一端正确接收它?
答案 0 :(得分:1)
我建议您制作一个 arrayList of hash-map
,然后将页面名称作为键值对。这样您也可以导航到该页面以及数据,请尝试这对它有帮助。
答案 1 :(得分:0)
第1步:为通知创建POJO类(比如说) 哪个是可分区的...因此您可以将此类对象从一个活动解析为其他
public class Notification implements Parcelable {
/**
* message : you have successfully registered
* flag : 0
* intent : incoming msg
*/
private String message;
private int flag;
private String intent;
public void setMessage(String message) {
this.message = message;
}
public void setFlag(int flag) {
this.flag = flag;
}
public void setIntent(String intent) {
this.intent = intent;
}
public String getMessage() {
return message;
}
public int getFlag() {
return flag;
}
public String getIntent() {
return intent;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.message);
dest.writeInt(this.flag);
dest.writeString(this.intent);
}
public Notification() {
}
protected Notification(Parcel in) {
this.message = in.readString();
this.flag = in.readInt();
this.intent = in.readString();
}
public static final Parcelable.Creator<Notification> CREATOR = new Parcelable.Creator<Notification>() {
public Notification createFromParcel(Parcel source) {
return new Notification(source);
}
public Notification[] newArray(int size) {
return new Notification[size];
}
};
}
第2步:第一个活动代码..这里我们使用google的GSON库(将json对象序列化或反序列化为java对象)
private static final String NOTIFICATIONS="NOTIFICATIONS";
try {
JSONObject obj = new JSONObject();
obj.put("message", "you have successfully registered");
obj.put("flag", 0);
obj.put("intent", "incoming msg");
Gson gson=new Gson();
ArrayList<Notification> notificationList=new ArrayList<>();
Notification notification=gson.fromJson(obj.toString(), Notification.class);
notificationList.add(notification);
Intent intent=new Intent(MainActivity.this, Main2Activity.class);
intent.putParcelableArrayListExtra(NOTIFICATIONS, notificationList);
startActivity(intent);
}
catch (JSONException je)
{
je.printStackTrace();
}
第2步:获取通知的可分配数据的第二个活动代码
private static final String NOTIFICATIONS="NOTIFICATIONS";
ArrayList<Notification> notifications=getIntent().getExtras().getParcelableArrayList(NOTIFICATIONS);
for(Notification notification: notifications)
{
Log.d("NOTIFICATIONS", "Message = "+notification.getMessage());
Log.d("NOTIFICATIONS", "Flag = "+notification.getFlag());
Log.d("NOTIFICATIONS", "Intent = "+notification.getIntent());
Log.d("NOTIFICATIONS", "");
}