Java 空数组列表
因此,在我的android应用程序中,我试图将Json链接转换为列表视图,但当我发送ArrayList时,得到的却是一个空的 我的代码: 类获取数据Java 空数组列表,java,android,Java,Android,因此,在我的android应用程序中,我试图将Json链接转换为列表视图,但当我发送ArrayList时,得到的却是一个空的 我的代码: 类获取数据 public class fetchData extends AsyncTask<Void,Void,Void> { ArrayList<Person> persons = new ArrayList<Person>(); String data=""; String dataParse
public class fetchData extends AsyncTask<Void,Void,Void> {
ArrayList<Person> persons = new ArrayList<Person>();
String data="";
String dataParsed="";
String singleParsed="";
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://jsonplaceholder.typicode.com/users");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ( line != null){
line = bf.readLine();
data = data + line;
}
JSONArray array = new JSONArray(data);
for (int i=0;i<array.length();i++){
JSONObject jo = (JSONObject) array.get(i);
Person p = new Person(jo.getString("id"),jo.getString("name"),jo.getString("username"),jo.getString("email"));
this.persons.add(p);
singleParsed =p.toString();
dataParsed = dataParsed + singleParsed;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
MainActivity.informations= this.dataParsed;
}
}
public class MainActivity extends AppCompatActivity {
Button moreInfos;
public static ArrayList<Person> persons = new ArrayList<Person>();
public static String informations;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
moreInfos = (Button) findViewById(R.id.goInfos);
fetchData process = new fetchData();
process.execute();
moreInfos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Informations.class);
String message = informations;
intent.putExtra("msg", message);
startActivity(intent);
// normally we need to pass the arraylist that contains the list of our persons
}
});
}
}
公共类fetchData扩展异步任务{
ArrayList persons=新建ArrayList();
字符串数据=”;
字符串dataParsed=“”;
字符串singleParsed=“”;
@凌驾
受保护的空位背景(空位…空位){
试一试{
URL=新URL(“https://jsonplaceholder.typicode.com/users");
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader bf=新的BufferedReader(新的InputStreamReader(inputStream));
字符串行=”;
while(行!=null){
line=bf.readLine();
数据=数据+行;
}
JSONArray数组=新的JSONArray(数据);
对于(int i=0;i活动中的列表为空,因为您从未实际将任何内容放入其中。请记住,异步任务中的列表与活动中的列表不同,因此您必须实际复制它
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
MainActivity.informations = this.dataParsed;
// defensively copy
MainActivity.persons = new ArrayList<>(this.persons);
}
@覆盖
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
MainActivity.informations=this.dataParsed;
//防御性抄袭
MainActivity.persons=新数组列表(this.persons);
}
您还可以在代码中改进一些其他方面(比如在应该使用回调时在活动中使用静态字段),但这应该可以解决当前的问题。您有:
public static ArrayList<Person> persons = new ArrayList<Person>();
相反。但当然:使用这样的静态变量,并盲目地(从其他类)将元素推入其中,很可能是个坏主意。为什么定义两个同名的arraylist?static
fieldMainActivity。persons
从不被触摸,因此保持为空。process.getPersons().size()
告诉你?无关:请阅读java命名修女会。类名大写。始终如此。你还应该更喜欢List foo=new ArrayList()
而不是使用ArrayList作为字段类型!@Selaron它告诉我它有0个元素你确定process.doInBackground()
在检查列表大小时,实际上完成了背景工作并找到了一个人?也许你检查得太快了。
ArrayList<Person> persons = new ArrayList<Person>();
MainActivity.persons.add(p);