通过意图传递活动之间的JSON数组

问题描述:

这已经被问过一次,但没有工作对我来说都没有。所以我想我就再问。

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

我有一个JSONarray,我想用一个意图传递给我的第二个活动。

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

这是我的code,连接到MySQL数据库,并把它用到一个JSONarray用户数据的一部分。这一切工作至今。

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

我想通过一个意图送我的完整JSONarray我的第二个活动(的loggedIn)。所以,我可以显示例如ct_email = json_data.getString(电子邮件);这是与第一活动获取从MySQL阵列中的另一个值。

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.

这是有关这一议题的其他问题: passing jsonarray从1活动到另一个

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

感谢

修改

感谢您的快速答复家伙。我是新来的StackOverflow所以请原谅我的错误就code标志等我尝试做这一切正常。

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


}

}

该错误是方法的getString(串)是没有定义字符串类型。 我必须做一些愚蠢的。

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

getExtras()返回包,而不是意图。改变你的code这样和尝试。

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

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