How to send JSon to another activity?

1

How to send JSon to another activity?

        public class SolicitaDados extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls){
        return Conexao.postDados(urls[0], parametros);
    }

    @Override
    protected void onPostExecute(String resultado){

        try {
            JSONObject Dados_Geral = new JSONObject(resultado);

            JSONArray arrayEmpr = Dados_Geral.getJSONArray("empresa");
            JSONArray arrayEsta = Dados_Geral.getJSONArray("estado");

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

I have in MainActivity 2 JSONArray- > company and state, I now need to use company in the activity company, and state in the activity state. How can I send you these arrays?

Thanks for the help! I am a beginner and I am not understanding very well all the Java programming content Android.

    
asked by anonymous 28.01.2017 / 21:08

1 answer

4

You can simply put the JSON in String and send it using the putExtras() method like this:

Intent intent = new Intent(this, DestinoActivity.class);
intent.putExtra("json", jsonobj.toString());
startActivity(intent);

To redownload String in activity , you simply enter JSONObject using the getStringExtra() method. Here's how it would look on your onCreate() :

public class DestinoActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_destino);

        Bundle args = new Bundle();
        JSONObject obj = new JSONObject(args.getStringExtra("json"));

    }
}

For more details on Intent , see in the documentation .

    
29.01.2017 / 02:16