如何使用 putExtra 来获取JsonObject?我想让JSON文本显示另一个活动。
@Override
protected String doInBackground(Void... v) {
HashMap<String, String> params = new HashMap<>();
params.put(KEY_MEMBER_ID, memberId);
params.put(KEY_PWD, pwd);
JSONParser jParser = new JSONParser();
JSONObject json = jParser.sendPostRequest(GlobalVariabel.URL_LOGIN_MEMBER, params);
System.out.println("URL :######### "+GlobalVariabel.URL_LOGIN_MEMBER);
try {
status = json.getString("STATUS");
System.out.println("STATUSSSSSSSSSSSSSSSSSS : " + status);
System.out.println(json);
JSONObject jObj = json.getJSONObject("PROFILE");
if (jObj != null) {
user_id = jObj.getString("USER_ID");
profile_url = jObj.getString("PROFILE_URL");
date_joined = jObj.getString("DATE_JOINED");
phone_type = jObj.getString("PHONE_TYPE");
email = jObj.getString("EMAIL");
name = jObj.getString("NAME");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
我想获取 JsonObject 电子邮件,名称使用intent.putExtra到另一个活动。
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra(".... ", ....);
//intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
答案 0 :(得分:1)
json实际上是一个具有确定格式的“字符串”文本。
第一个选项:
我建议您将它传递给String以通过putExtra发送它,然后将其重新映射到json对象:
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("JSON_OBJECT", json.toString());
//intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
并通过以下方式检索:
String jsonObject = intent.getStringExtra("JSON_OBJECT");
之后,您将不得不重新构建json对象。
第二个选项:
将jsonObject转换为实现Serializable或Parcelable的POJO对象并通过以下方式发送:
intent.putExtra("POJO_OBJECT", myJsonObject);
并通过以下方式检索:
MyJsonObject myjsonObject = (MyJsonObject)intent.getParcelableExtra("POJO_OBJECT", myJsonObject);
答案 1 :(得分:1)
发送json
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("json", jsonObject.toString());
startActivity(intent);
用于检索
if(getIntent().hasExtra("json")) {
JsonObject mJsonObject = new JsonObject(getIntent().getStringExtra("json"));
}
答案 2 :(得分:0)
试试这个 //将全局字符串decalre为String JsonOutput =“”;
@Override
protected String doInBackground(Void... v) {
HashMap<String, String> params = new HashMap<>();
params.put(KEY_MEMBER_ID, memberId);
params.put(KEY_PWD, pwd);
JSONParser jParser = new JSONParser();
JSONObject json = jParser.sendPostRequest(GlobalVariabel.URL_LOGIN_MEMBER, params);
System.out.println("URL :######### "+GlobalVariabel.URL_LOGIN_MEMBER);
try {
status = json.getString("STATUS");
System.out.println("STATUSSSSSSSSSSSSSSSSSS : " + status);
System.out.println(json);
JSONObject jObj = json.getJSONObject("PROFILE");
JsonOutput=jobj.toString(); //convert your json to string here.
if (jObj != null) {
user_id = jObj.getString("USER_ID");
profile_url = jObj.getString("PROFILE_URL");
date_joined = jObj.getString("DATE_JOINED");
phone_type = jObj.getString("PHONE_TYPE");
email = jObj.getString("EMAIL");
name = jObj.getString("NAME");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
postExecute()中的
proctected void onPostExcute(){
Intent intent = new Intent(yourActivity.this, MainActivity.class);
intent.putExtra("YOUR_KEY ", JsonOutput);// result contains all jsonObject value
//intent.setFlags(intent.getFlags() |Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
在MainActivity.class中//您想要获取此详细信息。
String Jsonoutput=getIntent().getExtras().getString("Your Key");
JSONObject job=new JSOObject(Jsonoutput);
//now parse this job to get your name and email.or anuy other data in jobj.
希望这有帮助。
答案 3 :(得分:-1)
就这样做
转到onPostExcute()方法
proctected void onPostExcute(String result){
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("YOUR_KEY ", result);// result contains all jsonObject value
//intent.setFlags(intent.getFlags() |Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);}
希望这会对你有所帮助。 :)