Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing JSON array between activities through intent
    text
    copied!<p>This has been asked once before but that didn't work for me at all. So I thought I'll just ask again.</p> <p>I have a JSONarray that I want to pass to my second activity using an intent.</p> <p>This is a part of my code which connects to a mysql database and puts data about a user in a JSONarray. This all works so far.</p> <pre><code>try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); sb = new StringBuilder(); sb.append(reader.readLine() + "\n"); String line="0"; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } String ct_user; String ct_pass; Intent personal = new Intent(Home.this, Loggedin.class); try{ jArray = new JSONArray(result); JSONObject json_data=null; json_data = jArray.getJSONObject(0); ct_user = json_data.getString("user"); ct_pass = json_data.getString("pass"); if (passwordstring.equals(ct_pass)){ Bundle b = new Bundle(); b.putString("userdata",json_data.toString()); personal.putExtras(b); startActivity(personal); } } </code></pre> <p>I'd like to send my complete JSONarray through an intent to my second activity (Loggedin). So that I can display for example ct_email = json_data.getString("email"); which is another value in the array that the first activity gets from mysql.</p> <p>This was the other question about this subject: <a href="https://stackoverflow.com/questions/7686531/passing-jsonarray-from-1-activity-to-another">passing jsonarray from 1 activity to another</a></p> <p>That solution didn't work for me because in the second activity errors kept saying it couldn't convert bundle to intent, I tried everything I could think of.</p> <pre><code>Intent b = getIntent().getExtras(); String userdata=b.getString("userdata"); </code></pre> <p>Thanks</p> <hr> <p>EDIT</p> <p>Thanks for the quick answers guys. I'm new to Stackoverflow so pardon my mistakes regarding code markings etc. I try to do it all properly.</p> <p>But it's still not working for me. This is part of my second activity:</p> <pre><code>public class Loggedin extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loggedin); myfunction(); } public void myfunction(){ Bundle b = getIntent().getExtras(); String userdataArray = b.getString("userdata"); String ct_email; ct_email = userdataArray.getString("email"); } } </code></pre> <p>The error is the "method getString(String) is undefined for the type String". I must be doing something stupid.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload