意图的JSONObject到另一个活动 [英] Intent JSONObject to another activity

查看:226
本文介绍了意图的JSONObject到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递的JSONObject在其他部分的另一个活动中,他低于code,所以我可以在其他活动使用的配置文件的详细信息,

 公共无效redirectToActivity(JSONObject的结果,JSONObject的轮廓){

    尝试 {
        字符串状态= result.getString(状态);

        如果(status.equals(204)){
            意向意图=新的意图(这一点,ActivityMenu.class);
            intent.putExtra(用户,电子邮件);
            this.startActivity(意向);
        }
        其他 {
            意向意图=新的意图(这一点,RegisterActivity.class);
            this.startActivity(意向);
            //这里我想通过JSONObject的配置文件来RegisterActivity
        }
    }赶上(JSONException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
}
 

解决方案

通过一个的JSONObject 您可以使用字符串。例如:

 意向意图=新的意图(这一点,RegisterActivity.class);
 intent.putExtra(个人资料,profile.toString());
 intent.putExtra(结果,result.toString());
 this.startActivity(意向);
 

的toString()实施的JSONObject ,类RN codeS的对象作为一个紧凑的JSON字符串。在你的 RegisterActivity ,检索字符串:

 字符串配置= getIntent()getStringExtra(配置文件)。
 JSONObject的profileJSON =新的JSONObject(资料)
 

I want to pass JSONObject to another activity in the else section in he below code so i can use profile details in the other activity,

public void redirectToActivity(JSONObject result, JSONObject profile){

    try {           
        String status = result.getString("status");

        if (status.equals("204")) {
            Intent intent = new Intent(this, ActivityMenu.class);
            intent.putExtra("user", "email");
            this.startActivity(intent);
        } 
        else {
            Intent intent = new Intent(this, RegisterActivity.class);
            this.startActivity(intent);
            //here I want to pass JSONObject profile to RegisterActivity
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

解决方案

to pass a JSONObject you can use a String. For instance:

 Intent intent = new Intent(this, RegisterActivity.class);
 intent.putExtra("profile", profile.toString());
 intent.putExtra("result", result.toString());
 this.startActivity(intent);

toString() implementation of the JSONObject, class rncodes the object as a compact JSON string. In your RegisterActivity, to retrieve the String:

 String profile = getIntent().getStringExtra("profile");
 JSONObject profileJSON = new JSONObject(profile)

这篇关于意图的JSONObject到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆