Passing JSONObject into another activity
You can just encapsulate all of the information about a movie into a Movie
object, which implements Parcelable
.
The code will look similar to above, but instead of passing 6 different extras you can just pass one extra that is the movie.
Movie movie = new Movie();
movie.setTitle(jsonObj.getJSONObject("Title").opt("val").toString());
movie.setRelDat(jsonObj.getJSONObject("RelDate").opt("val").toString());
.
.
.
i.putExtra("movie", movie);
For information on implementing a Parcelable object, see Parcelable docs. You basically just write out each string in 'writeToParcel', and read in each string in 'readFromParcel' in the correct order.
How to pass json data in another activity
You can not pass directly JSONObject
to another activity. But you can convert json to string and pass it. Then in SecondActivity
you can convert it to json again.
Start SecondActivity
codes in FirstActivity
:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("data", json.toString());
startActivity(intent);
Then get this data from SecondActivity
:
String data = getIntent().getStringExtra("data");
try {
JSONObject json = new JSONObject(data);
} catch (JSONException e) {
e.printStackTrace();
}
Good luck.
How to send and receive the json object from one activity to another?
You are doing the correct way. To get it in another activity, you can proceed as
if (getIntent().getExtras() != null) {
String scamDatas = getIntent().getStringExtra("scamDatas");
String scamSubCategoryText = getIntent().getStringExtra("scamSubCategoryText");
try {
JsonParser parser = new JsonParser();
JsonObject scamDataJsonObject = parser.parse(scamDatas).getAsJsonObject();
} catch (Exception e) {
e.printStackTrace();
}
}
Android/Java: How to get the ArrayListJSONObject from an Activity to another?
JSONObject is not Parcelable, or even Serializable. So the only option you're left with is to utilize JSONObject's toString()
method and then rely on using putStringArrayListExtra()
of Intent to pass it to the other.
On the other activity, use getExtras()
of Intent to receive and convert that to JSONObject by passing the String into its constructor & loop to form the ArrayList if required.
Say, the ArrayList of JSONObject is sendingArr
:
List<String> sendingListOfStrings = new ArrayList<String>();
for(int i = 0; i < sendingArr.length(); i++) {
sendingListOfStrings.add( sendingArr.getJSONObject(i).toString() );
}
Sending from ActivityOne to ActivityTwo:
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putStringArrayListExtra("sending_list", sendingListOfStrings);
startActivity(intent);
Then capture it = in the other:
ArrayList<JSONObject> receivingArr;
Bundle receivingListOfStrings = getIntent().getExtras();
ArrayList<String> list = receivingListOfStrings.getStringArrayList("sending_list");
for (int i = 0; i < receivingListOfStrings.length(); i++) {
receivingArr.add( new JSONObject( receivingListOfStrings.get(i) ) );
}
How to pass json result from one activity to another
i recommended to send JSON result as string see the following code
Intent intent=new Intent(getApplicationContext(),DistClass.class);
intent.putExtra("json",object.toString());
startActivity(intent);
in the second class do the following
String jsonResult=getIntent().getExtras().getString("json");
JSONObject json=new JSONObject(jsonResult);
that's all
Pass JSON Object from one activity to another Android
In your second activity, you can access the intent's JSON string with
getIntent().getStringExtra("deal");
Then you can convert that back into a JSON object if needed.
Update:
The reason why you're receiving an error in the Intent constructor in your Async task is because you're not passing in a valid context as a parameter. Your context property is of type Intent when it should be Context so fix that like this:
private Context mContext;
Add a constructor to your GetNearbyPlacesList like so
public GetNearbyPlacesList(Context context) {
mContext = context
}
Pass it a context on instantiation, your first activity most likely.
So then when you construct that intent in your async task class you can do so like this:
Intent intent = new Intent(mContext, ListRestActivity.class)
Related Topics
Why Is Calling Process.Killprocess(Process.Mypid()) a Bad Idea
Android Audiotrack Playing .Wav File, Getting Only White Noise
Android: How to Create Fade-In/Fade-Out Sound Effects for Any Music File That My App Plays
Retrofit2: Modifying Request Body in Okhttp Interceptor
How to Rotate Textview 90 Degrees and Display
Dependency Ignored Because of Conflict Android Studio
Add Dynamic Text Over Android Seekbar Thumb
What Actually Happens When Persistence Is Enabled in Firebase
How to Customize the Contextual Action Bar Using Appcompat in Material Design
Pop the Fragment Backstack Without Playing the Pop-Animation
Map Markers with Text in Google Maps Android API V2
Webview Rendering Issue in Android Kitkat