How to send Json to another Activity?

Asked

Viewed 148 times

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, now I need to use company in Activity Company, and state in State Activity. How can I send these arrays?

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

1 answer

4


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

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

To rescue again the String in Activity, just insert you to JSONObject using the method getStringExtra(). See 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 about the Intent, see in the documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.