How to pass JSON Object to new activity

前端 未结 3 745
太阳男子
太阳男子 2020-12-10 17:58

I have an application that needs to download JSON from URL using AsyncTask and on onPostExecute() pass that JSON Object to next Activity using putExtra method, but I\'m cons

相关标签:
3条回答
  • 2020-12-10 18:13

    You should pass a serializable or parcelable object through putExtra, as JSONObject is neither serializable nor parcelable object, and so you cant pass it through putExtra.

    So you need to make either a Parcelable or Serializable class from parsing a JSONObject, then you can use putExtra method to pass it.

    The other option is pass JSON String from jsonOjbect.

    0 讨论(0)
  • 2020-12-10 18:20

    In the Activity in which You are getting the JSON data write the following code to send to the TAB activity

    Intent i = new Intent(getApplicationContext(), Another_Activity.class);
                                i.putExtra("key", jsonObject.toString());
                                startActivity(i);
    

    to access the data ie JSON object Write the following code in tab Activity

       JSONObject jsonObject = new JSONObject(getIntent().getStringExtra("key"));
                        Toast.makeText(Another_Activity.this, ""+jsonObject.get("Your JSON VALUE"), Toast.LENGTH_SHORT).show();
    

    Your JSON VALUE is the filed which is present in the data. eg "Business Id" 1 here business id is the JSON value ,replace it with yours

    0 讨论(0)
  • 2020-12-10 18:24

    Do this when passing the param:

    intent.putExtra("json", jsonobj.toString());
    

    And in your new activity parse it like so:

    JSONObject obj = new JSONObject(getIntent().getStringExtra("json"));
    
    0 讨论(0)
提交回复
热议问题