Java 将JSON传递到新活动-Android应用程序
下面的代码将json返回到textView中。如何将此文本视图中的数据传递给其他活动Java 将JSON传递到新活动-Android应用程序,java,android,json,Java,Android,Json,下面的代码将json返回到textView中。如何将此文本视图中的数据传递给其他活动 private void showJSON(String response){ String name=""; String address=""; String vc = ""; try { JSONObject jsonObject = new JSONObject(response); JSONArray result = jsonObje
private void showJSON(String response){
String name="";
String address="";
String vc = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
name = collegeData.getString(Config.KEY_NAME);
address = collegeData.getString(Config.KEY_ADDRESS);
vc = collegeData.getString(Config.KEY_VC);
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText("Name:\t"+name+"\nAddress:\t" +address+ "\nVice Chancellor:\t"+ vc);
}
这是我正在关注的问题。我知道可以使用intent来完成,但我不确定在这个示例中如何使用它
谢谢
String json=textViewResult.getText().toString();
startActivity(new Intent(this,YourActivity.class).putExtra("data",json));
这就是您想要的吗?使用putExtra传递字符串数据
Intent i = new Intent(youractivity.this, SecondActivity.class);
i.putExtra("name",name);
i.putExtra("address",address);
i.putExtra("vc",vc);
startActivity(i);
在第二次活动中
String name=getIntent().getStringExtra("name");
String address=getIntent().getStringExtra("address");
String vc=getIntent().getStringExtra("vc");
Intent intent = getIntent();
String jsonString = intent.getStringExtra("jsonObject");
JSONObject jsonObject = new JSONObject(jsonString);
或
从活动传递json数据
Intent i = new Intent(youractivity.this, SecondActivity.class);
intent.putExtra("jsonObject", jsonObject.toString());
startActivity(i);
在第二次活动中
String name=getIntent().getStringExtra("name");
String address=getIntent().getStringExtra("address");
String vc=getIntent().getStringExtra("vc");
Intent intent = getIntent();
String jsonString = intent.getStringExtra("jsonObject");
JSONObject jsonObject = new JSONObject(jsonString);
谢谢大家的回答。这有助于我理解如何使用意图,并且我能够解决以下问题: 活动1
String json= textViewResult.getText().toString();
Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("Data",json);
startActivity(i);
活动2
Bundle b = getIntent().getExtras();
String json = b.getString("Data");
TextView jData = (TextView) findViewById(R.id.textView1);
jData.setText(json);
问题是什么?我如何将文本视图中的数据传递给另一个活动?使用intent和putextra在本例中演示如何工作是否可以?谢谢您的回答。我也能用同样的方法解决我的问题。谢谢你的回答。我能以类似的方式解决我的问题。