Passing JSON array between activities through inte

2020-07-13 11:29发布

问题:

This has been asked once before but that didn't work for me at all. So I thought I'll just ask again.

I have a JSONarray that I want to pass to my second activity using an intent.

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.

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'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.

This was the other question about this subject: passing jsonarray from 1 activity to another

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.

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

Thanks


EDIT

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.

But it's still not working for me. This is part of my second activity:

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


}

}

The error is the "method getString(String) is undefined for the type String". I must be doing something stupid.

回答1:

getExtras() returns Bundle, not Intent. Change your code like this and try.

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


回答2:

You can't call getString() on String because that method is defined on JSONObject NOT String. Hence compiler error. You have to parse the string into the JSON objects then you can use that to pull out that data:

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