mohsinmemon
mohsinmemon

Reputation: 1

pass jsonarray from Activity A to Activity B?

I have two Activity A and B.I call API in Activity A and get response in JsonArray. Now I need to get Whole JsonArray in Activity B from Activity A.how can I do this?

Upvotes: 0

Views: 1807

Answers (4)

Komal12
Komal12

Reputation: 3348

try this,

    In Activity A:

    Intent intent = new Intent(ActivityA.this, ActivityB.class);
    intent.putExtra("jsonArray", jsonArray.toString());
    startActivity(intent);

    In Activity B:

    Intent intent = getIntent();
    String jsonArray = intent.getStringExtra("jsonArray");

    try {
        JSONArray array = new JSONArray(jsonArray);

    } catch (JSONException e) {
        e.printStackTrace();
    }

Upvotes: 1

Morteza
Morteza

Reputation: 51

first you should convert your data as an ArrayList in activity A like below :

    ArrayList<YourCustomDataModel> dataArrayList= null;
    String jsonInternets = //"Your Json Response"

    if (jsonInternets != null && !jsonInternets.isEmpty()) {
        Gson gson = new Gson();
        YourCustomDataModel[] dataModels= gson.fromJson(jsonInternets,
                YourCustomDataModel[].class);

        dataArrayList= new ArrayList<YourCustomDataModel>(Arrays.asList(dataModels));
    }

after this step you can put this Arraylist in your intent like below :

    Intent intent=new Intent(ActivityA.this,ActiviyB.class);
    intent.putExtra("data",dataArrayList);
    startActivity(intent);

now after this steps in Activity B you can get your data as an ArrayList like below:

 if (getIntent().getExtras()!=null&&getIntent().getExtras().getSerializable("data")!=null)
    {
        ArrayList<YourCustomDataModel> dataArrayList=getIntent().getExtras().getSerializable("data");
    }

Upvotes: 0

eLemEnt
eLemEnt

Reputation: 1801

@mohsinmemon

Convert JsonArray to String then attach it to Intent ans send it.

JSONObject jObject = new JSONObject("Json Response");
Intent obj_intent = new Intent(ActivityA.this, ActivityB.class);
Bundle b = new Bundle();                
b.putString("Array",jObject.toString());
obj_intent.putExtras(b);

In ActivityB

Bundle b = getIntent().getExtras();
String Array=b.getString("Array");

Upvotes: 1

nullvoid
nullvoid

Reputation: 131

If you are not starting the second activity from first; declare that Json as public static else just pass it as bundle in intent.

Upvotes: 0

Related Questions