gpt4 book ai didi

java - 无法将 JSON 数据发送到其他 Android Activity

转载 作者:行者123 更新时间:2023-12-02 13:42:18 25 4
gpt4 key购买 nike

我对 Android 还很陌生。我正在尝试做的事情:我通过API下载了JSON格式的数据。我正在尝试将其与其他一些数据 bundle 在一起并将其发送到我的第二个 Activity 。我背后的逻辑是,我创建了一个全局变量“JSONData”,并将我在“onPostExecute”中下载的数据分配给它。但是,当我尝试通过 Intent ( bundle )将其发送到第二个 Activity 时,仅显示第一条数据。我的猜测是,该包是在下载 JSON 数据之前发送的。数据肯定是通过 API 下载的,因为我使用 TextView 显示它。任何人都可以给我一些关于如何解决这个问题的提示吗?我将非常感激:)

public class MainActivity extends AppCompatActivity {
private String JSONData;
private TextView Data_Test;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TestView = (TextView)findViewById(R.id.TestView);
}

public class JSONTask extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();

InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";

while ((line = reader.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();


} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}


@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
JSONData = result;
/*Data_Test.setText(JSONData);*/


}
}


public void btnType0(View view) {
new JSONTask().execute("URL_with_API");
String activity_title = getResources().getString(R.string.housing_button);
Intent intent = new Intent(this, DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", JSONData);
intent.putExtras(extras);
startActivity(intent);
}

在我的第二个 Activity 中,我收到了这样的 bundle :

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_data);

Bundle bundle = getIntent().getExtras();

String activity_title = bundle.getString("title");
TextView txtView = (TextView) findViewById(R.id.txtTitle);
txtView.setText(activity_title);

String JSONData = bundle.getString("JSON_Object");
TextView txtView1 = (TextView) findViewById(R.id.data_test);
txtView1.setText(JSONData);

}

最佳答案

哥们,你做错了!!

new JSONTask().execute("URL_with_API"); 您无法在 JSONData 变量中获取值,因为您已将其声明为全局变量,并且在 AsyncTask 中分配值这是后台进程,因此这一行将进入后台,下一行将立即执行。下面

String activity_title = getResources().getString(R.string.housing_button);
Intent intent = new Intent(this, DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", JSONData);// so here JSONData always being null
intent.putExtras(extras);
startActivity(intent);

您必须像这样在 onPostExecute 中启动 Activity

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String activity_title = getResources().getString(R.string.housing_button);
Intent intent = new Intent(this, DisplayDataActivity.class);
Bundle extras = new Bundle();
extras.putString("title", activity_title);
extras.putString("JSON_Object", result);
intent.putExtras(extras);
startActivity(intent);
}



public void btnType0(View view) {
new JSONTask().execute("URL_with_API");
}

Or just pass Url in required activity and fetch data in that activity using url in onCreate

关于java - 无法将 JSON 数据发送到其他 Android Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42678627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com