Passing JSON Array Between Actions Using Intents

It was asked once, but it did not work for me at all. So I thought that I would just ask again.

I have a JSONarray that I want to pass to my second action using the intent.

This is the part of my code that connects to the mysql database and puts the user data in JSONarray. It all works so far.

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);
            }
    }

I would like to send my full JSONarray for the purpose of my second lesson (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 receives from mysql.

That was another question on this: transferring jsonarray from one activity to another

, , , , .

Intent b = getIntent().getExtras();
String userdata=b.getString("userdata");


. Stackoverflow, .. .

. :

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");


}

}

- getString (String) undefined String ". - .

+5
2

getExtras() Bundle, Intent. .

Bundle b = getIntent().getExtras(); 
String userdata=b.getString("userdata"); 
+6

getString() String, JSONObject NOT String. , . JSON, :

public void myfunction(){

  Bundle b = getIntent().getExtras();

  // parse the JSON passed as a string.
  JSONObject json = new JSONObject( b.getString("userdata") );
  String ct_email = json.getString("email");
}
+6

All Articles