简体   繁体   中英

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.

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

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

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"));
  • 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);

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM