简体   繁体   English

如何从一个活动向另一个活动发送和接收 json 对象?

[英]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.我在从活动到另一个活动发送和接收 google json 对象时遇到问题。

 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.请帮助我如何从一个活动向另一个活动发送和接收 json 对象。

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 1.通过 Intent 发送字符串

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

2. Receive string from intent in other activity 2.从其他活动中的意图接收字符串

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

3. Parse json using JsonParser 3.使用JsonParser解析json

new JsonParser().parse(scamDataStr);

You can simply put an entire JSONObject as a string.您可以简单地将整个 JSONObject 作为字符串。 Something like this:像这样的东西:

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

And then in the MovieProductActivity you could然后在 MovieProductActivity 你可以

JSONObject jsonObj = new JSONObject(getIntent().getStringExtra("scamData"));
  • Send String via Intent通过意图发送字符串

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

  • Receive string from intent in other activity从其他活动中的意图接收字符串

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

  • Parse json using JsonParser使用 JsonParser 解析 json

new JsonParser().parse(scamDataStr);新的 JsonParser().parse(scamDataStr);

For kotlin enthusiasts,对于 kotlin 爱好者,

Use data classes and use @Parcelize annotation, extend with Parcelable interface.使用数据类并使用@Parcelize 注解,使用 Parcelable 接口扩展。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Intent 将对象从一个 Android Activity 发送到另一个? - How to send an object from one Android Activity to another using Intents? 如何在android中将Calendar对象从一个活动发送到另一个活动? - How to send a Calendar object from one activity to another in android? 如何使用可打包将对象从一个Android活动发送到另一个? - How to send an object from one Android Activity to another using parcelable? 将对象的数组列表从一个活动发送到另一个活动 - Send an Arraylist of an object from one activity to another 将Json数据从一个活动中的列表视图发送到另一活动中 - Send Json data from listview in one activity to another activity 如何将字符串从一个活动发送到另一个活动? - How to send string from one activity to another? 如何将SharedPreferences从一个活动发送到另一个活动 - How to send SharedPreferences from one Activity to another 如何将图像从一个活动发送到另一个活动 - How to send image from one activity to another 如何将一个活动的类对象类型的ArrayList发送到另一个活动? - How to send ArrayList of class object type from one activity to another activity? 如何从recyclerView接收JSON数据到另一个活动 - How to receive JSON data from recyclerView to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM