Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing JSON array between activities through intent

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.

like image 393
Martin van der Woude Avatar asked Jan 08 '12 03:01

Martin van der Woude


People also ask

How pass JSON object to another activity in Android?

PutExtra("json1", json); StartActivity(i); and the destination activity is savedInstanceState = new Bundle(); savedInstanceState = Intent. Extras; var obj1 = savedInstanceState. GetString("json1"); var obj2 = JsonConvert.

How to Pass JSON array in intent?

You should convert JsonArray to String then attach it to Intent and send it. JSONObject jObject = new JSONObject("Your Json Response"); Intent obj_intent = new Intent(Main. this, Main1.


2 Answers

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

Bundle b = getIntent().getExtras(); 
String userdata=b.getString("userdata"); 
like image 86
kosa Avatar answered Nov 15 '22 03:11

kosa


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");
}
like image 26
chubbsondubs Avatar answered Nov 15 '22 03:11

chubbsondubs