如何将json数据传递到下一个活动和下一个活动我想将它传递给片段?

时间:2017-11-08 07:17:28

标签: android

username = editText_name.getText().toString() ;
                password = editText_password.getText().toString();

                request = new StringRequest(Request.Method.POST, login_url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if(jsonObject.names().get(1).equals("data")){
                                Toast.makeText(getApplicationContext(),"Successfully Logged IN",Toast.LENGTH_LONG).show();

//这里我将数据分配给用户名,详细信息分配给密码

                                username = jsonObject.getString("data");
                                password = jsonObject.getString("details");
                                Intent intent = new Intent(MainActivity.this,Dashboard.class);

//因为我调试用户名是返回null

                                intent.putExtra("username",username);
                                intent.putExtra("password",password);
                                startActivity(intent);

1 个答案:

答案 0 :(得分:0)

username = editText_name.getText().toString() ;
                password = editText_password.getText().toString();

                request = new StringRequest(Request.Method.POST, login_url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if(jsonObject.names().get(1).equals("data")){
                                /*to pass data to the next activity*/
                               username = jsonObject.getString("data");
                            password = jsonObject.getString("details");
                            Intent intent = new Intent(MainActivity.this,Dashboard.class);

                            startActivity(intent);
                            intent.putExtra("username",username);
                            intent.putExtra("password",password);
                            startActivity(intent);
Toast.makeText(getApplicationContext(),"Successfully Logged IN",Toast.LENGTH_LONG).show();

下一个活动获取数据

String name = getIntent().getStringExtra("username");
String pass = getIntent().getStringExtra("details");

/ *现在将其传递给片段* /

FragmentX f = new FragmentX();
Bundle b = new Bundle();
b.putExtra("username",name);
b.putExtra("details",pass);
f.setArguments(b);

/ 现在处于片段的onCreate方法 /

Bundle b = getArguments();
String name = b.getStringExtra("username");
String pass = b.getStringExtra("details");

快照到onCreate()方法 like this