Prabha Karan
Prabha Karan

Reputation: 1329

How to send and receive the json object from one activity to another?

I am facing the issues in sending and receiving the google json objects from activity to another activity.

 List<Integer> selectedScamMediumIds = scamMediumHorizontalAdapter.getSelectedScamMediumIds();
 JsonObject scamData = new JsonObject();
 JsonArray scamMediumJsonArray = new JsonArray();

 for (Integer scamMediumId:selectedScamMediumIds) {
     JsonPrimitive jsonPrimitive = new JsonPrimitive(scamMediumId);
     scamMediumJsonArray.add(jsonPrimitive);
 }
 scamData.add("scam_medium_id",scamMediumJsonArray);
 scamData.addProperty("scam_category_id", scamCategoryId);
 scamData.addProperty("scam_sub_category_id", scamSubCategoryId + "");
 scamData.addProperty("scammer_phone", phoneNumber.getText().toString());
 scamData.addProperty("scammer_location", scammerLocation.getText().toString());
 scamData.addProperty("lat", lattitude);
 scamData.addProperty("lng", longitude);

 Intent intent = new Intent(ScamLookUpActivity.this, ScamSearchActivity.class);
 intent.putExtra("scamDatas", scamData.toString());
 intent.putExtra("scamSubCategoryText", subCategoryTitle);
 startActivity(intent);

I have tried the above method, I don't know whether it is correct or not. Please help me how to send and receive the json object from one activity to another activity.

Upvotes: 3

Views: 3776

Answers (5)

Samarth Gupta
Samarth Gupta

Reputation: 66

For kotlin enthusiasts,

Use data classes and use @Parcelize annotation, extend with Parcelable interface.

Can use intents in case of activity, one way to move between fragments - Use bundle.

Bundle().apply{
 putParcelable("some_unique_key",data)
}

val data = arguments.getParcelable<DataType>("some_unique_key")

Upvotes: 0

maher alaqil
maher alaqil

Reputation: 11

  • Send String via Intent

intent.put("scamData", scamData.getAsString();

  • Receive string from intent in other activity

String scamDataStr = getIntent().getStringExtra("scamData");

  • Parse json using JsonParser

new JsonParser().parse(scamDataStr);

Upvotes: 0

Aashish Aadarsh
Aashish Aadarsh

Reputation: 491

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();
                }
            }

Upvotes: 3

Gowtham Subramaniam
Gowtham Subramaniam

Reputation: 3478

You can simply put an entire JSONObject as a string. Something like this:

i.putString("scamData", jsonObj.toString);

And then in the MovieProductActivity you could

JSONObject jsonObj = new JSONObject(getIntent().getStringExtra("scamData"));

Upvotes: 0

PEHLAJ
PEHLAJ

Reputation: 10126

Try

1. Send String via Intent

intent.put("scamData", scamData.getAsString(); //or scamData.toString();

2. Receive string from intent in other activity

String scamDataStr = getIntent().getStringExtra("scamData");

3. Parse json using JsonParser

new JsonParser().parse(scamDataStr);

Upvotes: 3

Related Questions