Android 网上获取json加载到Spinner不显示的问题解决方案

我最近尝试从网站上读取json并将数据加载到spinner中,json能够正常读取,但是有时候spinner的数据显示有问题,再查阅了大量文档之后自己总结了一下几点.

1: 异步线程获取json

  要注意在线程中是不能修改UI界面的,必须要主线程才能修改UI界面,因为多条线程修改UI可能会造成UI崩溃.

  在异步线程中需要等数据加载结束了,在重载的 onPostExecute 方法中把数据加载给spinner适配器.

  代码如下:

 1 private class getJSON extends AsyncTask<Void, Void, Void> {
 2 
 3         Activity activity;
 4         Context context;
 5 
 6         public getJSON(Activity activity, Context context) {
 7             this.activity = activity;
 8             this.context = context;
 9         }
10 
11         @Override
12         protected void onPreExecute() {
13             super.onPreExecute();
14             Toast.makeText(MainActivity.this,"trying to get json",Toast.LENGTH_SHORT).show();
15         }
16 
17         @Override
18         protected Void doInBackground(Void... voids) {
19             try {
20                 URL url = new URL("https://api.myjson.com/bins/o9qje");
21                 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
22                 InputStream inputStream = httpURLConnection.getInputStream();
23                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
24 
25                 String temp = "";
26                 while (temp != null) {
27                     temp = bufferedReader.readLine();
28                     JsonData += temp;
29                 }
30 
31                 JSONArray jsonArray = new JSONArray(JsonData);
32                 for (int i = 0; i < jsonArray.length(); i++) {
33                     JSONObject jsonObject = jsonArray.getJSONObject(i);
34                     UserInfo userInfo = new UserInfo();
35                     carId_list.add(Integer.toString(jsonObject.getInt("CarId")));
36                     Balance_list.add(Integer.toString(jsonObject.getInt("Balance")));
37                     userInfo.setCarid(jsonObject.getInt("CarId"));
38                     userInfo.setBalance(jsonObject.getInt("Balance"));
39                     userInfoList.add(userInfo);
40 
41                 }
42 
43 
44             } catch (MalformedURLException e) {
45                 e.printStackTrace();
46             } catch (JSONException e) {
47                 e.printStackTrace();
48             } catch (IOException e) {
49                 e.printStackTrace();
50             }
51             return null;
52         }
53 
54         @Override
55         protected void onPostExecute(Void aVoid) {
56             super.onPostExecute(aVoid);
57                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, carId_list);
58                 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
59                 CarId_Spinner.setAdapter(adapter);
60                 //默认选中第1个
61                 CarId_Spinner.setSelection(0, true);
62                 balance.setText(Integer.toString(Integer.parseInt(Balance_list.get(0))) + " 元");
63 
64         }
65     }

 

2: Thread的方式获取json加载的方法

  Thread 的方法和异步线程基本一样,我的做法是实现runnable接口,在handler中修改adapter

new Thread(new getJson()).start(); //实现runnable接口

  Handler 就如其名字,我们可以用它来修改UI界面

  在Thead中需要传送一个msg.what标记这是哪一个任务

handler.sendEmptyMessage(1);

  完整代码:

  

 1 public class getJson implements Runnable {
 2         @Override
 3         public void run() {
 4             try {
 5                 URL url = new URL("https://api.myjson.com/bins/o9qje");
 6                 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
 7                 InputStream inputStream = httpURLConnection.getInputStream();
 8                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
 9 
10                 String temp = "";
11                 while (temp != null) {
12                     temp = bufferedReader.readLine();
13                     JsonData += temp;
14                 }
15 
16                 JSONArray jsonArray = new JSONArray(JsonData);
17                 for (int i = 0; i < jsonArray.length(); i++) {
18                     JSONObject jsonObject = jsonArray.getJSONObject(i);
19                     UserInfo userInfo = new UserInfo();
20                     carId_list.add(Integer.toString(jsonObject.getInt("CarId")));
21                     Balance_list.add(Integer.toString(jsonObject.getInt("Balance")));
22                     userInfo.setCarid(jsonObject.getInt("CarId"));
23                     userInfo.setBalance(jsonObject.getInt("Balance"));
24                     userInfoList.add(userInfo);
25 
26                     Log.d("userInfoList: ", String.valueOf(userInfoList.get(i)));
27                 }
28                 handler.sendEmptyMessage(1);
29             } catch (MalformedURLException e) {
30                 e.printStackTrace();
31             } catch (JSONException e) {
32                 e.printStackTrace();
33             } catch (IOException e) {
34                 e.printStackTrace();
35             }
36         }
37     }

  

  Handler代码:

 1 //创建handler更新Spinner
 2     Handler handler = new Handler() {
 3         @Override
 4         public void handleMessage(Message msg) {
 5             super.handleMessage(msg);
 6             if (msg.what == 1) {
 7                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, carId_list);
 8                 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 9                 CarId_Spinner.setAdapter(adapter);
10                 //默认选中第1个
11                 CarId_Spinner.setSelection(0, true);
12                 balance.setText(Integer.toString(Integer.parseInt(Balance_list.get(0))) + " 元");
13             }
14         }
15     };

  

3: 如果前面两步都尝试过,还是没有显示,说明有可能是XML中Spinner显示的问题,可以尝试更改XML <Spinner> Pandding值 

 

posted @ 2019-05-14 20:13  kevin162726  阅读(625)  评论(1编辑  收藏  举报