瑞客论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

楼主: iceberg

黑马史上最全面的微服务全技术栈课程(资料完整)

    [复制链接]

0

主题

1711

帖子

1万

积分

永久会员

Rank: 8Rank: 8

威望
4375
贡献
4216
热心值
0
金币
2474
注册时间
2020-4-7
发表于 2021-10-26 23:57 | 显示全部楼层
黑马史上最全面的微服务全技术栈课程(资料完整)
回复

使用道具 举报

0

主题

99

帖子

952

积分

高级会员

Rank: 4

威望
404
贡献
459
热心值
0
金币
89
注册时间
2021-10-1
发表于 2021-10-27 00:04 | 显示全部楼层
强烈支持楼主ing……
回复

使用道具 举报

22

主题

1097

帖子

1万

积分

论坛元老

Rank: 8Rank: 8

威望
3402
贡献
3613
热心值
3
金币
5600
注册时间
2020-2-11
发表于 2021-10-28 08:34 | 显示全部楼层
public class MainActivity extends AppCompatActivity {

    private static final int WHAT_SUCCESS = 1;
    private static final int WHAT_FAIL = 2;

    private ListView lv_main;
    private LinearLayout ll_main_loading;
    private List<ShopInfo> data;
    private ShopInfoAdapter adapter;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case WHAT_SUCCESS:
                    ll_main_loading.setVisibility(View.GONE);
                    // 显示列表
                    lv_main.setAdapter(adapter);
                    break;
                case WHAT_FAIL:
                    ll_main_loading.setVisibility(View.GONE);
                    Toast.makeText(MainActivity.this, "加载数据失败!", (int) 1).show();
                    break;
                default:
            }
        }
    };

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

        lv_main = findViewById(R.id.lv_main);
        ll_main_loading = findViewById(R.id.ll_main_loading);
        adapter = new ShopInfoAdapter();

        // 1.主线程,显示提示视图
        ll_main_loading.setVisibility(View.VISIBLE);
        // 2.分线程,联网请求
        // 启动分线程请求服务器动态加载数据并显示
        new Thread(() -> {
            try {
                String jsonStr = requestJson();
                System.out.println(jsonStr);
                Gson gson = new Gson();
                Log.e("gson:", gson + "");
                data = gson.fromJson(jsonStr, new TypeToken<List<ShopInfo>>() {
                }.getType());
                Log.e("json:", data + "");
                // 3.主线程,更新界面
                // 发送请求成功的消息
                handler.sendEmptyMessage(WHAT_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                // 发送请求失败的消息
                handler.sendEmptyMessage(WHAT_FAIL);
            }
        }).start();
    }

    /**
     * 联网请求得到json字符串
     *
     * @return
     */
    private String requestJson() throws Exception {
        String result = "";
        // 192.168.124.4
        // 192.168.0.103
        String path = "http://192.168.124.4:9999/vod/gets";
        // 1.得到连接对象
        URL url = new URL(path);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //(4)设置连接超时,读取数据超时
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        //(5)连接服务器
        connection.connect();
        int responseCode = connection.getResponseCode();
        if (responseCode == 200) {
            //(6)发请求,得到响应数据
            InputStream is = connection.getInputStream();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = is.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, len);
            }
            byteArrayOutputStream.close();
            is.close();
            result = byteArrayOutputStream.toString();
            // (7)断开连接
            connection.disconnect();
        } else {
            // 也可以抛出异常
        }
        return result;
    }

    class ShopInfoAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(MainActivity.this, R.layout.item_main, null);
            }
            // 得到当前行的数据对象
            ShopInfo shopInfo = data.get(position);
            // 得到当前行的子view
            TextView name = convertView.findViewById(R.id.tv_item_name);
            TextView id = convertView.findViewById(R.id.tv_item_content);
            ImageView imageView = convertView.findViewById(R.id.iv_item_icon);
            // 设置数据显示
            name.setText(shopInfo.getVideoName());
            id.setText(shopInfo.getId());
            // 根据图片路径启动分线程动态请求服务加载图片并显示
            return convertView;
        }
    }
}
回复

使用道具 举报

0

主题

1669

帖子

1万

积分

论坛元老

Rank: 8Rank: 8

威望
5770
贡献
5098
热心值
0
金币
2629
注册时间
2021-9-2
发表于 2021-10-28 08:37 | 显示全部楼层
我只是路过打酱油的。
回复

使用道具 举报

0

主题

17

帖子

370

积分

月费会员

Rank: 3Rank: 3

威望
206
贡献
144
热心值
0
金币
20
注册时间
2021-10-28
发表于 2021-10-28 17:14 | 显示全部楼层
强烈支持楼主ing……
回复

使用道具 举报

2

主题

2625

帖子

1万

积分

论坛元老

Rank: 8Rank: 8

威望
6568
贡献
4546
热心值
0
金币
1450
注册时间
2021-8-26
发表于 2021-10-28 19:43 | 显示全部楼层
强烈支持楼主ing……
回复

使用道具 举报

0

主题

227

帖子

1433

积分

金牌会员

Rank: 6Rank: 6

威望
861
贡献
507
热心值
0
金币
65
注册时间
2021-2-17
发表于 2021-10-28 22:10 | 显示全部楼层
66666666666666
回复

使用道具 举报

0

主题

121

帖子

111

积分

注册会员

Rank: 2

威望
49
贡献
60
热心值
0
金币
2
注册时间
2020-4-15
发表于 2021-10-28 22:12 | 显示全部楼层
真是难得给力的帖子啊。
回复

使用道具 举报

3

主题

1289

帖子

1469

积分

金牌会员

Rank: 6Rank: 6

威望
894
贡献
572
热心值
0
金币
3
注册时间
2021-7-1
发表于 2021-10-31 12:48 | 显示全部楼层
真是难得给力的帖子啊。
回复

使用道具 举报

0

主题

14

帖子

83

积分

注册会员

Rank: 2

威望
32
贡献
35
热心值
0
金币
16
注册时间
2021-11-1
发表于 2021-11-1 14:25 | 显示全部楼层
1111111
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|瑞客论坛 |网站地图

GMT+8, 2024-5-14 19:13

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表