How to pass a JSON object to a new activity

I have an application that should load JSON from a URL using AsyncTask and onPostExecute () to pass this JSON object to the next activity using the putExtra method, but I constantly get an error message that says: "putExtra can only be used to pass strings, not objects "? How to fix it?

+6
source share
3 answers

Do this when passing the parameter:

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

And in your new action, analyze it like this:

 JSONObject obj = new JSONObject(getIntent().getStringExtra("json")); 
+25
source

You must pass a serializable or batch object via putExtra, since JSONObject is not a serializable and non-shared object, so you cannot pass it through putExtra.

Therefore, you need to make the Parcelable or Serializable class to parse the JSONObject, then you can use the putExtra method to pass it.

Another option is to pass a JSON String from jsonOjbect.

+1
source

In the operation in which you receive JSON data, write the following code to send TAB activity

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

to access data, that is, to a JSON object. Write the following code in the Activity tab

  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 file that is present in the data. for example, "Business Identifier" 1 here the business identifier is the JSON value, replace it with

0
source

Source: https://habr.com/ru/post/924264/


All Articles