Reputation: 744
I have the following code:
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());
where intent
is an object of Intent
class. Now, after saving this JSONArray in Utility, I want to retrieve this saved JSONArray from another activity, so I am doing the following for the same:
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");
Can you please tell me how do I achieve the above? That is, passing an intent via a JSONObject, in Android, and receiving it correctly from the other end?
Upvotes: 0
Views: 373
Reputation: 1768
step 1 : create POJO class for Notification (lets say) Which is parcelable.. hence you can parse this class object from one activity to other
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];
}
};
}
step 2 : first activity code .. here we are using GSON library from google (to serialize or deserialize the json object to java object)
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();
}
step 2 : second activity code to get parcelable data for notification
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", "");
}
Upvotes: 0
Reputation: 302
I suggest you to make an arrayList of hash-map
and in that put the page name as a key value pair.So that way you also can navigate to that page along with the data,Try this and it will help you.
Upvotes: 1