3

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.

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Prabha Karan
  • 1,309
  • 3
  • 17
  • 35
  • 2
    parse your `Json` into one custom Object, then make your class as `Parcelable` or `Serializable` to pass between activities – Shayan Pourvatan May 09 '17 at 04:16
  • I don't know how to parse ...please will you post your code – Prabha Karan May 09 '17 at 04:19
  • We cannot help you generate/write a piece of code. We can just help you make a good logic. Your question shows that haven't done proper research! – Zain I. May 09 '17 at 04:22
  • Possible duplicate of [How to pass gson serialised object to Intent in android?](http://stackoverflow.com/questions/21761438/how-to-pass-gson-serialised-object-to-intent-in-android) – Zain I. May 09 '17 at 04:23
  • @ Zain how to receive and pass the received data to json object, in above link i am little confused in receiving side – Prabha Karan May 09 '17 at 04:46

5 Answers5

3

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();
                }
            }
Aashish Aadarsh
  • 491
  • 5
  • 7
3

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);
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
0

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"));
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31
0
  • 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);

0

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")
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459