-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
459 lines (233 loc) · 315 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>魏尔肖的blog</title>
<subtitle>无问西东</subtitle>
<link href="/atom.xml" rel="self"/>
<link href="http://yoursite.com/"/>
<updated>2020-12-13T09:30:04.303Z</updated>
<id>http://yoursite.com/</id>
<author>
<name>weierxiao</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title>计网实验报告</title>
<link href="http://yoursite.com/2020/12/12/%E8%AE%A1%E7%BD%91%E5%AE%9E%E9%AA%8C%E6%8A%A5%E5%91%8A/"/>
<id>http://yoursite.com/2020/12/12/%E8%AE%A1%E7%BD%91%E5%AE%9E%E9%AA%8C%E6%8A%A5%E5%91%8A/</id>
<published>2020-12-12T08:31:07.000Z</published>
<updated>2020-12-13T09:30:04.303Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><table><thead><tr><th><strong><em>\</em>实验名称**</strong></th><th>实验1 WireShark的使用</th><th></th><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong><em>\</em>实验时间**</strong></td><td>2020年10月7日 10:00-11:40时</td><td><strong><em>\</em>实验地点**</strong></td><td>计通楼210</td><td><strong><em>\</em>实验学时**</strong></td><td>2学时</td></tr><tr><td><strong><em>\</em>实验类型**</strong></td><td>验证</td><td><strong><em>\</em>实验性质**</strong></td><td>必修</td><td></td><td></td></tr><tr><td><strong><em>\</em>实验环境**</strong></td><td>网络环境:如实验室以太网、寝室无线局域网等硬件配置:包括CPU、主存容量、网络适配器数据率等操作系统:windows实验软件:如wireshark、IE等</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验目的**</strong></td><td>1. 认识协议分析软件WireShark。2. 初步熟悉WireShark的使用方法。</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验原理*****</strong>*(预备知识)****</td><td>Wireshark 是网络包分析工具。网络包分析工具的主要作用是尝试捕获网络包, 并尝试显示包的尽可能详细的情况。 你可以把网络包分析工具当成是一种用来测量有什么东西从网线上进出的测量工具,就好像使电工用来测量进入电信的电量的电度表一样。(当然比那个更高级) 过去的此类工具要么是过于昂贵,要么是属于某人私有,或者是二者兼顾。 Wireshark出现以后,这种现状得以改变。 Wireshark可能算得上是今天能使用的最好的开元网络分析软件。 主要应用于网络管理员用来解决网络问题、网络安全工程师用来检测安全隐患、开发人员用来测试协议执行情况和用来学习网络协议等场合。</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验内容(实验步骤和方法)**</strong></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td>1、熟悉界面 (1)第一部分是菜单和工具栏,wireshark提供的所有功能都可以在这一部分中找到。(2)第二部分是被捕获包的列表,其中包含被捕获包的一般信息,如被捕获的时间、源和目的IP地址、所属的协议类型,以及包的类型等信息。(3)第三部分显示第二部分已选中的包的每个域的具体信息,从以太网帧的首部到该包中负载内容,都显示得清清楚楚。(4)第四部分显示已选中包的16进制和ASCII表示,帮助用户了解一个包的本来样子。 2、抓包 (1)选择“Capture”菜单项中的 “Option”,这时会弹出一个对话框,如下所示。 这个对话框中的栏目虽然很多,但一般只需配置其中两项。一项是 “Capture Filter”栏。在这个栏中,可以输入过滤规则,用于规定wireshark捕获包的种类;如果不输入过滤规则,则wireshark将捕获所有从网卡发送或收到的包。另外一项是 “Update list of packets in real time”选项,请大家一定要选中这一项,这样可以使Ethereal在捕获包的同时,实时地把捕获的包显示出来。(2)在完成如上配置后,点击 “Start”按钮,Ethereal便开始捕获包。3、对所抓取的TCP数据包解释 帧号 时间 源地址 目的地址 高层协议 包内信息概况 No. Time Source Destination Protocol Info 1 0.000000 202.203.44.225 202.203.208.32 TCP 2764 > http [SYN] Seq=0 Len=0 MSS=1460 源端口>目的端口[请求建立TCP链接]————————————————————————————————— 以下为物理层的数据帧概况 Frame 1 (62 bytes on wire, 62 bytes captured) 1号帧,线路62字节,实际捕获62字节 Arrival Time: Jan 21, 2008 15:17:33.910261000 捕获日期和时间 [Time delta from previous packet:0.00000 seconds]此包与前一包的时间间隔 [Time since reference or first frame: 0.00 seconds]此包与第1帧的间隔时间 Frame Number: 1 帧序号 Packet Length: 62 bytes 帧长度 Capture Length: 62 bytes 捕获长度 [Frame is marked: False] 此帧是否做了标记:否 [Protocols in frame: eth:ip:tcp] 帧内封装的协议层次结构 [Coloring Rule Name: HTTP] 用不同颜色染色标记的协议名称:HTTP [Coloring Rule String: http || tcp.port == 80] 染色显示规则的字符串:————————————————————————————————— 以下为数据链路层以太网帧头部信息 Ethernet II, Src: AcerTech_5b:d4:61 (00:00:e2:5b:d4:61), Dst: Jetcell_e5:1d:0a (00:d0:2b:e5:1d:0a) 以太网协议版本II,源地址:厂名_序号(网卡地址),目的:厂名_序号(网卡地址) Destination: Jetcell_e5:1d:0a (00:d0:2b:e5:1d:0a) 目的:厂名_序号(网卡地址) Source: AcerTech_5b:d4:61 (00:00:e2:5b:d4:61) 源:厂名_序号(网卡地址) Type: IP (0x0800) 帧内封装的上层协议类型为IP(十六进制码0800)————————————————————————————————— 以下为互联网层IP包头部信息 Internet Protocol, Src: 202.203.44.225 (202.203.44.225), Dst: 202.203.208.32 (202.203.208.32) 互联网协议,源IP地址,目的IP地址 Version: 4 互联网协议IPv4(请参考书上P122页IPv4数据报字段结构) Header length: 20 bytes IP包头部长度 Differentiated Services Field:0x00(DSCP 0x00:Default;ECN:0x00)差分服务字段 Total Length: 48 IP包的总长度 Identification:0x8360 (33632) 标志字段 Flags: 标记字段(在路由传输时,是否允许将此IP包分段,教材125页) Fragment offset: 0 分段偏移量(将一个IP包分段后传输时,本段的标识) Time to live: 128 生存期TTL Protocol: TCP (0x06) 此包内封装的上层协议为TCP Header checksum: 0xe4ce [correct] 头部数据的校验和 Source: 202.203.44.225 (202.203.44.225) 源IP地址 描述一下WireShark的设置选项,比如如何设置选定网卡,如何设置过滤器等;</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验结果(根据实验内容回答问题)**</strong></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td>1.WireShark协议分析软件的功能有哪些?Wireshark的常用功能分为四个方面: 一般分析任务 (1)找出在一个网络内发送数据包最多的主机 (2)查看网络通信 (3)查看某个主机使用了哪些程序 (4)基本正常的网络通信 (5)验证特有的网络操作 (6)了解尝试连接无线网络的用户 (7)同时捕获多个网络的数据 (8)实施无人值守数据捕获 (9)捕获并分析到/来自一个特定主机或子网的数据 (10)通过FTP或HTTP查看和重新配置文件传输 (11)从其他捕获工具导入跟踪文件 (12)使用最少的资源捕获数据 故障任务 (1)为故障创建一个自定义的分析环境 (2)确定路径,客户端和服务延迟 (3)确定TCP问题 (4)检查HTTP代理问题 (5)检查应用程序错误响应 (6)通过查看图形显示的结果,找到相关的网络问题 (7)确定重载的缓冲区 (8)比较缓慢的通信到正常通信的一个基准 (9)找出重复的IP地址 (10)确定DHCP服务或网络代理问题 (11)确定WLAN信号强度问题 (12)检测WLAN连接的次数 (13)检查各种网络配置错误 (14)确定应用程序正在加载一个网络片段 安全分析(网络取证)任务 (1)为网络取证创建一个自定义分析环境 (2)检查使用非标准端口的应用程序 (3)确定到/来自可疑主机的数据 (4)查看哪台主机正在尝试获取一个IP地址 (5)确定phone home数据 (6)确定网络侦查环境 (7)全球定位和映射远程目标地址 (8)检查可疑数据重定向 (9)检查单个TCP或UDP客户端和服务器之间的会话 (10)检查到恶意畸形的帧 (11)在网络数据中找出攻击签名的关键因素 应用程序分析任务 (1)了解应用程序和协议如何工作 (2)了解图形应用程序的带宽使用情况 (3)确定是否将支持应用程序的链接 (4)更新/升级后检查应用程序性能 (5)从一个新安装的应用程序中检查错误响应 (6)确定从哪个用户正在运行一个特定的应用程序 (7)检查应用程序如何使用传输协议,如TCP或UDP2.WireShark主界面有哪几部分组成?打开桌面上的wireshark工具,Wireshark主界面可以分为如下七个部分,1:主菜单栏、2:快捷方式、3:过滤栏、4:数据包列表区、5:数据包详细信息区、6:比特区、7:数据包统计区域菜单栏选项介绍1、Flie(文件)栏用于打开,合并捕捉文件,保存,打印,另存,导出捕捉文件的全部或另存,以及退出Wireshark等等操作2、Edit(编辑)菜单栏,包括查询数据包,时间设置,数据包标志,设置预设参数等3、View(试图)控制捕捉数据的显示方式,包括颜色,字体缩放,将包显示在分离的窗口,展开或收缩详情面板4、Capture(捕获)菜单栏,选择网卡,开始,停止抓包,过滤器等操作5、Analyze(分析)菜单栏,显示过滤器,tcp、udp包追踪等功能6、Statistics(统计)栏,数据汇总信息,会话选项,节点统计,IO图,流量图等,做出会话报表7、Telephony(电话)支持语音流量抓取功能8、Tools(工具)栏9、Help帮助界面快捷方式菜单1、使用工具中的常用按钮,分别有:查看网卡,列出接口选项,启动,停止,重置,打开,着色…等过滤栏1、过滤基于arp协议的数据包,在Filter栏输入arp,回车,列出基于arp的所有数据包数据包列表区1、数据包列表区列出了包的时间流、源IP、目的IP、协议、信息数据详细信息区1、在列表区选中数据包的时候,数据包的详细信息显示在下方,我们称为详细信息区比特区1、数据包字节,十六进制信息,通过比特区看到更详细的字段信息<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps3.jpg" alt="img">)3.如何理解所抓到的包的内容?<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps4.jpg" alt="img"></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>成绩:**</strong></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table><table><thead><tr><th><strong><em>\</em>实验名称**</strong></th><th>实验二 802.3协议分析和以太网</th><th></th><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong><em>\</em>实验时间**</strong></td><td>2020年 月 日 10:00-11:40时</td><td><strong><em>\</em>实验地点**</strong></td><td>计通楼210</td><td><strong><em>\</em>实验学时**</strong></td><td>2学时</td></tr><tr><td><strong><em>\</em>实验类型**</strong></td><td>验证</td><td><strong><em>\</em>实验性质**</strong></td><td>必修</td><td></td><td></td></tr><tr><td><strong><em>\</em>实验环境**</strong></td><td>网络环境:如实验室以太网、寝室无线局域网等硬件配置:包括CPU、主存容量、网络适配器数据率等操作系统:windows实验软件:如wireshark、IE等</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验目的**</strong></td><td>1.分析802.3协议2.熟悉以太网帧的格式3.熟悉ARP报文的格式</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验原理*****</strong>*(预备知识)****</td><td>要深入理解网络协议,需要仔细观察协议实体之间交换的报文序列。为探究协议操作细节,可使协议实体执行某些动作,观察这些动作及其影响。这些任务可以在仿真环境下或在如因特网这样的真实网络环境中完成。观察在正在运行协议实体间交换报文的基本工具被称为分组嗅探器(packet sniffer)。顾名思义,一个分组嗅探器捕获(嗅探)计算机发送和接收的报文。一般情况下,分组嗅探器将存储和显示出被捕获报文的各协议头部字段内容。图1为一个分组嗅探器的结构。<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps5.png" alt="img"> 图1右边是计算机上正常运行的协议(在这里是因特网协议)和应用程序(如:web浏览器和ftp客户端)。分组嗅探器(虚线框中的部分)是附加计算机普通软件上的,主要有两部分组成。分组捕获库接收计算机发送和接收的每一个链路层帧的拷贝。高层协议(如:HTTP、FTP、TCP、UDP、DNS、IP等)交换的报文都被封装在链路层帧(Frame)中,并沿着物理介质(如以太网的电缆)传输。图1假设所使用的物理媒体是以太网,上层协议的报文最终封装在以太网帧中。 分组嗅探器的第二个组成部分是分析器。分析器用来显示协议报文所有字段的内容。为此,分析器必须能够理解协议所交换的所有报文的结构。例如:我们要显示图1中HTTP协议所交换的报文的各个字段。分组分析器理解以太网帧格式,能够识别包含在帧中的IP数据报。分组分析器也要理解IP数据报的格式,并能从IP数据报中提取出TCP报文段。然后,它需要理解TCP报文段,并能够从中提取出HTTP消息。最后,它需要理解HTTP消息。 WireShark是一种可以运行在Windows, UNIX, Linux等操作系统上的分组分析器.用户界面如图2所示。最初,各窗口中并无数据显示。WireShark的界面主要有五个组成部分:<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps6.jpg" alt="img"> 图2 WireShark的用户界面 命令菜单(command menus):命令菜单位于窗口的最顶部,是标准的下拉式菜单。最常用菜单命令有两个:File、Capture。File菜单允许你保存捕获的分组数据或打开一个已被保存的捕获分组数据文件或退出WireShark程序。Capture菜单允许你开始捕获分组。 捕获分组列表(listing of captured packets):按行显示已被捕获的分组内容,其中包括:WireShark赋予的分组序号、捕获时间、分组的源地址和目的地址、协议类型、分组中所包含的协议说明信息。单击某一列的列名,可以使分组按指定列进行排序。在该列表中,所显示的协议类型是发送或接收分组的最高层协议的类型。 分组头部明细(details of selected packet header):显示捕获分组列表窗口中被选中分组的头部详细信息。包括:与以太网帧有关的信息,与包含在该分组中的IP数据报有关的信息。单击以太网帧或IP数据报所在行左边的向右或向下的箭头可以展开或最小化相关信息。另外,如果利用TCP或UDP承载分组,WireShark也会显示TCP或UDP协议头部信息。最后,分组最高层协议的头部字段也会显示在此窗口中。 分组内容窗口(packet content):以ASCII码和十六进制两种格式显示被捕获帧的完整内容。 显示筛选规则(display filter specification):在该字段中,可以填写协议的名称或其他信息,根据此内容可以对分组列表窗口中的分组进行过滤。</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验内容(实验步骤和方法)**</strong></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong>1.</strong> WireShark的使用及捕获并分析以太帧(1) 清空浏览器缓存(在IE窗口中,选择“工具/Internet选项/删除文件”命令)。(2) 启动WireShark,开始分组捕获。(3) 启动主机上的web浏览器。(4) 启动WireShark。窗口中没有任何分组列表。(5) 开始分组捕获:选择“capture”下拉菜单中的“Optios”命令,会出现如图1所示的“WireShark: Capture Options”窗口,可以设置分组捕获的选项。<a href="#blogid=63258d650100fvkp&url=http://static14.photo.sina.com.cn/orignal/63258d65g798514b42ebd&690"><img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps7.png" alt="img"></a> 图1 WireShark的Capture Option(6) 在实验中,可以使用窗口中显示的默认值。在“WireShark: Capture Options”窗口的最上面有一个“interface”下拉菜单,其中显示计算机中所安装的网络接口(即网卡)。当计算机具有多个活动网卡(装有多块网卡,并且均正常工作)时,需要选择其 中一个用来发送或接收分组的网络接口(如某个有线接口)。(7) 随后,单击“Start”开始进行分组捕获,所有由选定网卡发送和接收的分组都将被捕获。(8) 开始分组捕获后,会出现分组捕获统计窗口。该窗口统计显示各类已捕获分组的数量。在该窗口中有一个“stop”按钮,可以停止分组的捕获。(9) 在运行分组捕获的同时,在浏览器地址栏中输入某网页的URL,如:<a href="http://www.baidu.com" target="_blank" rel="noopener">www.baidu.com</a>。为显示该网页,浏览器需要连接<a href="http://www.baidu.com" target="_blank" rel="noopener">www.baidu.com</a>的服务器,并与之交换HTTP消息,以下载该网页。包含这些HTTP消息的以太网帧(Frame)将被WireShark捕获。 WireShark主窗口显示已捕获的你的计算机与其他网络实体交换的所有协议报文,其中一部分就是与<a href="http://www.baidu.com" target="_blank" rel="noopener">www.baidu.com</a>服务器交换的HTTP消息。(10) 在显示筛选编辑框中输入“http”,单击“apply”,分组列表窗口将只显示HTTP消息。(11) 选择分组列表窗口中的第一条HTTP消息。它应该是你的计算机发向<a href="http://www.baidu.com" target="_blank" rel="noopener">www.baidu.com</a>服务器的HTTP GET(HTTP定义的用于获取/查询资源信息的方法)消息。(12) 选择“Analyze->Enabled Protocols”,取消对IP复选框的选择,单击OK。当你选择该消息后,以太网帧、IP数据报、TCP报文段、以及HTTP消息首部信息都将显示在分组首部子窗口中。单击分组首部详细信息子窗口中向右和向下箭头,可以最小化帧、以太网、IP、TCP信息显示量,可以最大化HTTP协议相关信息 的显示量。 (13) 选择包含HTTP GET消息的以太网帧,在分组详细信息窗口中,展开Ethernet II部分。根据操作,回答“五、实验报告内容”中的1-4题(14) 选择包含HTTP 响应消息第一个字节的以太网帧,根据操作,回答“五、实验报告内容”中的5-8题<strong>2.</strong> <strong><em>\</em>ARP**</strong>(1) 利用MS-DOS命令:arp -a查看主机上ARP缓存的内容。根据操作,回答“四、实验报告内容”中的9题。(2) 利用MS-DOS命令:arp -d * 以清除主机中ARP缓存的内容。(3) 清除浏览器缓存。(4) 启动WireShark,开始分组捕获。(5) 在浏览器的地址栏中输入:<a href="http://www.baidu.com" target="_blank" rel="noopener">www.baidu.com</a> 浏览器将显示百度搜索网页。(6) 停止分组捕获。选择“Analyze->Enabled Protocols”,取消对IP复选框的选择,单击OK。根据操作,回答“五、实验报告内容”中的10-13题。</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验结果(根据实验内容回答问题)**</strong></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps8.jpg" alt="img">)4.你的主机的48位以太网地址(MAC地址)是多少? 5.目标MAC地址是<a href="http://www.baidu.com服务器的MAC地址吗?如果不是,该地址是什么设备的MAC地址?不是,是该实验室路由器的mac地址。![img](https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps9.jpg)6">www.baidu.com服务器的MAC地址吗?如果不是,该地址是什么设备的MAC地址?不是,是该实验室路由器的mac地址。![img](https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps9.jpg)6</a>. 给出Frame头部Type字段(2字节)的十六进制值。<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps10.jpg" alt="img">8c7. 在包含“HTTP GET”的以太网帧中,字符“G”的位置(是第几个字节,假设Frame头部第一个字节的顺序为1)?558. 以太Frame的源MAC地址是多少?该地址是你主机的MAC地址吗?是<a href="http://www.baidu.com服务器的MAC地址吗?如果不是,该地址是什么设备的MAC地址?地址是:8c:dc:d4:3d:54:71,不是,是路由器的mac地址![img](https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps11.jpg)9.以太网帧的目的MAC地址是多少?该地址是你主机的地址吗?地址是:8c:dc:d4:41:01:00,不是![img](https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps12.jpg)10">www.baidu.com服务器的MAC地址吗?如果不是,该地址是什么设备的MAC地址?地址是:8c:dc:d4:3d:54:71,不是,是路由器的mac地址![img](https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps11.jpg)9.以太网帧的目的MAC地址是多少?该地址是你主机的地址吗?地址是:8c:dc:d4:41:01:00,不是![img](https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps12.jpg)10</a>. 给出Frame头部2-字节Type字段的十六进制值。<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps13.jpg" alt="img"> 11.在包含“OK”以太网帧中,从该帧的第一个字节算起,”O”是第几个字节? <img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps14.jpg" alt="img"> 12.写下你主机ARP缓存中的内容。其中每一列的含义是什么?接口是自己的地址,Internet是路由器分配的地址,剩下的是动态的物理地<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps15.jpg" alt="img">址。13.包含ARP请求报文的以太网帧的源地址和目的地址的十六进制值各是多少?8c:dc:d4:41:00:2d ff ff ff ff ff ff <img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps16.jpg" alt="img">14. 给出Frame头部Type字段的十六进制值。Ff ff ff<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps17.jpg" alt="img">15. 在ARP报文中是否包含发送方的IP地址?包含<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps18.jpg" alt="img">)<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps19.jpg" alt="img">)16.包含ARP响应(reply)报文的以太网帧中,源地址和目的地址的十六进制值各是多少?8c:dc:d4:41:00:2d 8c:dc:d4:3d:54:71</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>成绩:**</strong></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table><table><thead><tr><th><strong><em>\</em>实验名称**</strong></th><th>实验三 IP层协议分析</th><th></th><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong><em>\</em>实验时间**</strong></td><td>2020年 月 日 10:00-11:40时</td><td><strong><em>\</em>实验地点**</strong></td><td>计通楼210</td><td><strong><em>\</em>实验学时**</strong></td><td>2学时</td></tr><tr><td><strong><em>\</em>实验类型**</strong></td><td>验证</td><td><strong><em>\</em>实验性质**</strong></td><td>必修</td><td></td><td></td></tr><tr><td><strong><em>\</em>实验环境**</strong></td><td>网络环境:如实验室以太网、寝室无线局域网等硬件配置:包括CPU、主存容量、网络适配器数据率等操作系统:windows实验软件:如wireshark、IE等</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验目的**</strong></td><td>1. 了解ICMP、IP数据包格式;2. 掌握IP 数据报的组成和各字段的功能,分析数据报的IP首部;3. 理解ARP、ICMP与IP的关系;4. 理解ARP命令、PING命令与ARP、ICMP协议的关系;5. 熟悉ARP和ICMP协议包格式;6. 了解ARP、ICMP会话过程。</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验原理*****</strong>*(预备知识)****</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验内容(实验步骤和方法)**</strong></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td>(说明:以下所截界面上的数据与你电脑上的数据会有所不同)1. 首先进入MS DOS字符界面 2. 在MS DOS下使用ARP –d *命令清除自己电脑中MAC和IP映射表。见图1所示<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps20.jpg" alt="img"> 图1 ARP及PING命令运行结果3. 回到windows图形界面下4. 启动WireShark,开始捕获分组。5. 在MS DOS下键入Ping <a href="http://www.baidu.com" target="_blank" rel="noopener">www.baidu.com</a>,见图1所示。6. 回到WireShark并停止抓包 ,见图2所示。7. 查找到ARP请求和应答数据包,回答实验报告内容中的1-2题8. 查到PING命令执行时,产生的ICMP请求和应答报文,回答实验报告内容中的3-5题<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps21.jpg" alt="img"> 图2 WireShark抓包结果</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验结果(根据实验内容回答问题)**</strong></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td>1. 什么是ARP?ARP与IP的关系。 ARP,即地址解析协议,实现通bai过duIP地址得知其物理地址。以太网通信zhi需要经过二层封装后再把数据发出去,dao但是我们有时只知道对方的IP地址,而不知道对方的MAC地址,这时我们可以通过ARP请求来获取对方的MAC地址。ARP请求是广播,所以对方能收到,对方收到ARP请求后解析报文发现目的IP是自己,于是把自己的MAC地址填入ARP包头的SMAC中发回给我们,这样我们就知道了对方的MAC地址,可以进行通信了。 2. ARP请求和应答数据包的数据部分的内容是什么?代表什么意思? <img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps22.jpg" alt="img"> 3. 什么是ICMP?ICMP与IP的关系。两者都是网络层的协议,ip用于网际互联,icmp用于探测ICMP是:Internet 控制信息协议(ICMP)是 IP 组的一个整合部分。它是TCP/IP协议集中的一个子协议,属于网络层协议,主要用于在主机与路由器之间传递控制信息,包括报告错误、交换受限控制和状态信息等。当遇到IP数据无法访问目标、IP路由器无法按当前的传输速率转发数据包等情况时,会自动发送ICMP消息。我们可以通过Ping命令发送ICMP回应请求消息并记录收到ICMP回应回复消息。通过这些消息来对网络或主机的故障提供参考依据 4. ICMP的请求和应答报文的头部各字段的十六进制值分别是什么,代表什么含义?数据部分的内容以及IP头部的十六进制值分别是什么?内容。<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps23.jpg" alt="img"> 数据内容:<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps24.jpg" alt="img"> Ip:<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps25.jpg" alt="img"> 5. 什么是“会话”?本实验中那些协议有会话过程?IP有否“会话”?文件传输过程就是一次会话,tcp、http、icmp、ARP, 通过TCP/IP协议建立起来的会话,会话可以理解为一个应用程序。也就是说通过网络建立其连接的应用程序.6. 你主机的IP地址是什么?你所访问的主页所在服务器的IP地址是什么?(未用)主机的IP地址:172.18.3.109,baidu: 14.215.177.38<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps26.jpg" alt="img"></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>成绩:**</strong></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table><table><thead><tr><th><strong><em>\</em>实验名称**</strong></th><th>实验四 TCP协议分析</th><th></th><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong><em>\</em>实验时间**</strong></td><td>2020年 月 日 10:00-11:40时</td><td><strong><em>\</em>实验地点**</strong></td><td>计通楼210</td><td><strong><em>\</em>实验学时**</strong></td><td>2学时</td></tr><tr><td><strong><em>\</em>实验类型**</strong></td><td>验证</td><td><strong><em>\</em>实验性质**</strong></td><td>必修</td><td></td><td></td></tr><tr><td><strong><em>\</em>实验环境**</strong></td><td>网络环境:如实验室以太网、寝室无线局域网等硬件配置:包括CPU、主存容量、网络适配器数据率等操作系统:windows实验软件:如wireshark、IE等</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验目的**</strong></td><td>1. 熟悉TCP协议的基本原理2. 利用WireShark对TCP协议进行分析</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验原理*****</strong>*(预备知识)****</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验内容(实验步骤和方法)**</strong></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td>1. 捕获大量的由本地主机到远程服务器的TCP分组(1) 启动WireShark,开始分组捕获。(2) 启动浏览器,打开<a href="http://www.uzz.edu.cn/jsjx/index.asp" target="_blank" rel="noopener">http:// www.sina.com.cn/ </a>网页, (3) 停止分组捕获。2. 浏览追踪信息(1) 在显示筛选规则编辑框中输入“tcp”,可以看到在本地主机和服务器之间传输的一系列tcp和HTTP消息,你应该能看到包含SYN Segment的三次握手。也可以看到有主机向服务器发送的一个HTTP GET消息和一系列的“http continuation”报文。(2) 根据操作回答“四、实验报告内容”中的1-2题。3. TCP基础 根据操作回答“四、实验报告内容”中的3-10题4. TCP拥塞控制(1) 在WireShark已捕获分组列表子窗口中选择一个TCP 报文段。选择菜单: Statistics->TCP Stream Graph-> Time-Sequence-Graph(Stevens)。你会看到如下所示的图。<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps27.png" alt="img"> (2) 根据操作回答“四、实验报告内容”中的11-12题。</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验结果(根据实验内容回答问题)**</strong></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td>1. 向<a href="http://www.sina.com.cn服务器传送文件的客户端主机的IP地址和TCP端口号分别是多少?主机">www.sina.com.cn服务器传送文件的客户端主机的IP地址和TCP端口号分别是多少?主机</a> Src:172.18.3.109<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps28.jpg" alt="img">端口 src port:50946 2. <a href="http://www.sina.com.cn服务器的IP地址是多少?对这一连接,它用来发送和接收TCP报文的端口号是多少?主机">www.sina.com.cn服务器的IP地址是多少?对这一连接,它用来发送和接收TCP报文的端口号是多少?主机</a> Src:183.60.95.219<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps29.jpg" alt="img">端口 src port:443 3. 客户服务器之间用于初始化TCP连接的TCP SYN报文段的序号(sequence number)是多少?在该报文段中,是用什么来标示该报文段是SYN报文段的?报文段的序号是0,用seq来表示报文段 4. 服务器向客户端发送的SYNACK报文段序号是多少?该报文段中,Acknowledgement字段的值是多少?<a href="http://www.sina.com.cn服务器是如何决定此值的?在该报文段中,是用什么来标识该报文段是SYN">www.sina.com.cn服务器是如何决定此值的?在该报文段中,是用什么来标识该报文段是SYN</a> ACK报文段的?Sequence number:0 (relative sequence number)Acknowledgment number: 1 (relative ack number)用Flags: 0x002 (SYN)来标识该报文段 5. 包含HTTP GET消息的TCP报文段的序号是多少?Sequence number: 1 (relative sequence number)<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps30.jpg" alt="img"> 6. <img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps31.jpg" alt="img">如果将包含HTTP GET消息的TCP报文段看作是TCP连接上的第一个报文段,那么该TCP连接上的第六个报文段的序号是多少?是何时发送的?该报文段所对应的ACK是何时接收的?序号为755<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps32.jpg" alt="img"> 发送时间为10.43.05<img src="C:%5CUsers%5C18408%5CAppData%5CLocal%5CTemp%5Cksohtml18368%5Cwps33.jpg" alt="img"> 7. 前六个TCP报文段的长度各是多少?都是1400 <img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps34.jpg" alt="img"> 8. 在整个跟踪过程中,接收端向发送端通知的最小可用缓存是多少?限制发送端的传输以后,接收端的缓存是否仍然不够用?最小可用缓存是15872 没有发生过<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps35.jpg" alt="img"> 9. 在跟踪文件中是否有重传的报文段?判断的依据是什么?没有,从TCP报文段的序列号中可以看出。从源端发往目的端的序号逐渐递增,如果这其中有重传的报文段,则其序号中应该有小于其临近的分组序号的分组,在图中未看到这样的分组,所以没有被重传的分组。10. TCP连接的吞吐率(bytes transferred per unit time,单位时间传输的字节数)是多少?写出你的计算过程。传输的字节数是153010传输时间是2.362480-1.153008=1.209472吞吐率(取整)=153010/1.209472=126509 byte/s11. 利用Time-Sequence-Graph(Stevens) plotting工具,浏览由客户端向服务器发送的报文段序号和时间对应关系图。你能否辨别出TCP慢启动阶段的起止,以及在何处转入避免拥塞阶段? 慢启动阶段,无避免拥塞阶段<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps36.jpg" alt="img">12. 阐述所测量到的数据与TCP理想化的行为有何不同?在慢启动阶段时,如果有网络拥塞的话,发送方会根据AIMD算法进行调整。在此实验中,在发送方还可以发送数据的时候,已经没有数据需要发送了。在Web应用中,有些web对象比较小,在慢启动还没有结束之前,传送就已经结束了,所以传送小的web对象受到TCP慢启动阶段的影响,导致较长的延迟。</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>成绩:**</strong></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验名称**</strong></td><td>实验五 HTTP和DNS分析</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验时间**</strong></td><td>2020年 月 日 10:00-11:40时</td><td><strong><em>\</em>实验地点**</strong></td><td>计通楼210</td><td><strong><em>\</em>实验学时**</strong></td><td>2学时</td></tr><tr><td><strong><em>\</em>实验类型**</strong></td><td>验证</td><td><strong><em>\</em>实验性质**</strong></td><td>必修</td><td></td><td></td></tr><tr><td><strong><em>\</em>实验环境**</strong></td><td>网络环境:如实验室以太网、寝室无线局域网等硬件配置:包括CPU、主存容量、网络适配器数据率等操作系统:windows实验软件:如wireshark、IE等</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验目的**</strong></td><td>1. 熟悉并掌握WireShark的基本操作,了解网络协议实体间的交互以及报文交换。2. 分析HTTP和DNS协议</td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验原理*****</strong>*(预备知识)****</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验内容(实验步骤和方法)**</strong></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong>1.</strong> HTTP分析(1) 启动WireShark,开始分组捕获。(2) 启动主机上的web浏览器,在浏览器的地址栏中输入: <a href="http://www.baidu.com" target="_blank" rel="noopener">www.baidu.com</a>;浏览器将显示百度搜索网页。(3) 在窗口的显示过滤规则编辑框处输入“http”,分组列表子窗口中将只显示所捕获到的HTTP消息。选择分组列表窗口中的第一条http报文。它应该是你的计算机发向<a href="http://www.baidu.com" target="_blank" rel="noopener">www.baidu.com</a>服务器的HTTP GET报文。(4) 停止分组捕获。 根据捕获窗口内容,回答“实验报告内容”中的1-4题。2. 跟踪并分析DNS nslookup工具允许主机向指定的DNS服务器查询某个DNS记录。如果没有指明DNS服务器,nslookup将把查询请求发向默认的DNS服务器。 nslookup的一般格式是: nslookup –option1 –option2 host-to-find dns-server ipconfig命令用来显示你当前的TCP/IP信息,包括:你的地址、DNS服务器的地址、适配器的类型等信息。如果,要显示与主机相关的信息用命令: ipconfig/all 如果查看DNS缓存中的记录用命令: ipconfig/displaydns 要清空DNS缓存,用命令: ipconfig /flushdns 运行以上命令需要进入MSDOS环境。(开始菜单->运行->输入命令“cmd”)(1) 利用ipconfig命令清空主机上的DNS缓存。启动浏览器,并将浏览器的缓存清空。(2) 启动WireShark,在显示过滤筛选规则编辑框处输入: “ip.addr = = your_IP_address”(如:ip.addr= =10.17.7.23) 过滤器将会删除所有目的地址和源地址与指定IP地址都不同的分组。(3) 开始WireShark分组捕获。(4) 在浏览器的地址栏中输入:<a href="http://www.baidu.com" target="_blank" rel="noopener">www.baidu.com</a>后,回车。(5) 停止分组捕获。(6) 根据操作回答“实验报告内容”中的5-8题。 (7) 开始WireShark分组捕获。(8) <a href="http://www.sin.com上进行nslookup(即执行命令:nslookup">www.sin.com上进行nslookup(即执行命令:nslookup</a> <a href="http://www.sin.com)。(9)" target="_blank" rel="noopener">www.sin.com)。(9)</a> 停止分组捕获。(10) 根据操作回答“实验报告内容” 中的 9-11题。</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>实验结果(根据实验内容回答问题)**</strong></td><td></td><td></td><td></td><td></td><td></td></tr><tr><td>此页不够可加页<strong>1.</strong> HTTP分析(1) 从发出HTTP GET消息到接收到HTTP OK响应报文共需要多长时间?(在默认的情况下,分组列表窗口中Time列的值是从WireShark开始追踪到分组被捕获时总的时间, 以秒为单位。若要按time-of-day格式显示Time列的值,需选择View下拉菜单,再选择Time Display Format,然后选择Time-of-day。)共需 (2) <img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps37.jpg" alt="img">写出第3步所显示的HTTP消息头部行信息并说明其含义? (3) <img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps38.jpg" alt="img">你的浏览器使用的是HTTP1.0,还是HTTP1.1?你所访问的Web服务器所使用HTTP协议的版本号是多少? <img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps39.jpg" alt="img"> <img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps40.jpg" alt="img">都是 HTTP1.1 (4) 从服务器向你的浏览器返回response消息的状态代码是多少?表示什么意思? 状态代码是302,表示重定向,如果完成请求还必须采取进一步的行动。2. 跟踪并分析DNS(5) 定位到DNS查询消息和查询响应报文,这两种报文的发送是基于UDP还是基于TCP的? <img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps41.jpg" alt="img">)<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps42.jpg" alt="img"> 都是UDP (6) <img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps43.jpg" alt="img">DNS查询消息的目的端口号是多少?DNS查询响应消息的源端口号是多少? 目的端口号是53,DNS查询响应消息的源端口号是53。 (7) DNS查询消息发送的目的地的IP地址是多少?利用ipconfig命令(ipconfig/all查看你主机的本地DNS服务器的IP地址。这两个地址相同吗?<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps44.jpg" alt="img"> DNS查询消息发送的目的地的IP地址是222.246.129.80;这两个地址相同 (8) 考虑一下你的主机随后发送的TCP SYN Segment, 包含SYN Segment的IP分组头部中目的IP地址是否与在DNS查询响应消息中提供的某个IP地址相对应?<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps45.jpg" alt="img">与源地址ip相同,DNS目的ip不同。 <img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps46.jpg" alt="img"> (9) DNS查询消息的目的端口号是多少?DNS查询响应消息的源端口号是多少? DNS查询消息的目的端口号是53,DNS查询响应消息的源端口号是53 (10) DNS查询消息发送的目的地的IP地址是多少?这个地址是你的默认本地DNS服务器的地址吗?<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps47.jpg" alt="img"> DNS查询消息发送的目的地的IP地址是222.246.129.80;这两个地址相同 (11) 检查DNS查询响应消息,其中提供了多少个“answers”?每个answers包含哪些内容?<img src="https://cdn.jsdelivr.net/gh/xiaohongri/Yafine-imgs/images/wps48.jpg" alt="img"> 检查DNS查询响应消息,其中提供了1个“answers”,</td><td></td><td></td><td></td><td></td><td></td></tr><tr><td><strong><em>\</em>成绩:**</strong></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>颜色表及html代码</title>
<link href="http://yoursite.com/2020/10/01/%E9%A2%9C%E8%89%B2%E8%A1%A8%E5%8F%8Ahtml%E4%BB%A3%E7%A0%81/"/>
<id>http://yoursite.com/2020/10/01/%E9%A2%9C%E8%89%B2%E8%A1%A8%E5%8F%8Ahtml%E4%BB%A3%E7%A0%81/</id>
<published>2020-10-01T08:31:07.000Z</published>
<updated>2020-10-02T09:23:27.737Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p><a href="http://xh.5156edu.com/page/z1015m9220j18754.html" target="_blank" rel="noopener">http://xh.5156edu.com/page/z1015m9220j18754.html</a></p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Android Studio导入和删除jar包</title>
<link href="http://yoursite.com/2020/09/30/Android%20Studio%E5%AF%BC%E5%85%A5%E5%92%8C%E5%88%A0%E9%99%A4jar%E5%8C%85/"/>
<id>http://yoursite.com/2020/09/30/Android%20Studio%E5%AF%BC%E5%85%A5%E5%92%8C%E5%88%A0%E9%99%A4jar%E5%8C%85/</id>
<published>2020-09-30T10:31:07.000Z</published>
<updated>2020-10-02T09:33:33.849Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>之前使用Eclipse打包的jar在对方AS上编译报错,Arcgis for Android 不再支持Eclipse……总之,有很多很多原因促使自己从Eclipse转到AS学习和使用。这里不再比较Eclipse和AS的不同,事实上两者的理念非常不同。</p><p>1、Android Studio 导入jar包</p><p>AS中没有Eclipse的【Build Path】,也没有找到很多博客讲到【Add as Library】,如<a href="http://blog.csdn.net/qq_16628781/article/details/49280291。操作如下:" target="_blank" rel="noopener">http://blog.csdn.net/qq_16628781/article/details/49280291。操作如下:</a></p><p>(1)复制jar包到libs物理路径下</p><p>将复制的jar包放到app路径下的libs文件夹里,如果没有libs文件夹,则创建libs文件夹。</p><p>(2)打开Project Structure(或者右击工程,选择【Open Module Settings】)</p><p>【File】——【Project Structure】,在【Dependencies】中点击右上角的“+”,选择【File Dependency】<img src="https://img-blog.csdn.net/20170404161745950?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcmVud3VkYW8yNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="img"></p><p><img src="https://img-blog.csdn.net/20170404161954502" alt="img"></p><p>(3)在select path对话框中选择要添加的jar包</p><p><img src="https://img-blog.csdn.net/20170404162057549" alt="img"></p><p>一路OK。</p><p>(4)查看buildl.gradle该文件会在打包时编译</p><p><img src="https://img-blog.csdn.net/20170404162540337" alt="img"></p><p>2、删除jar包</p><p>(1)是否可以直接删除jar包?No,这是因为该文件路径已经加入编译路径,需要先删除。<img src="https://img-blog.csdn.net/20170404162337256" alt="img"></p><p>(2)在build.gradle中手动删除compiled files</p><p>以删除ips.jar包为例,需要删除</p><pre><code>compile files('libs/ips.jar')</code></pre><p>(3)可视化操作</p><p>打开Project Structure,选择要删除的jar包,然后点击右上角的红色“-”,OK。</p><p>(4)delete jar包</p><p>右击jar,选择delete,此时jar包已被清除。</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Android 调用快递鸟api 实现物流跟踪(在途监控)</title>
<link href="http://yoursite.com/2020/09/30/Android%20%E8%B0%83%E7%94%A8%E5%BF%AB%E9%80%92%E9%B8%9Fapi%20%E5%AE%9E%E7%8E%B0%E7%89%A9%E6%B5%81%E8%B7%9F%E8%B8%AA%EF%BC%88%E5%9C%A8%E9%80%94%E7%9B%91%E6%8E%A7%EF%BC%89/"/>
<id>http://yoursite.com/2020/09/30/Android%20%E8%B0%83%E7%94%A8%E5%BF%AB%E9%80%92%E9%B8%9Fapi%20%E5%AE%9E%E7%8E%B0%E7%89%A9%E6%B5%81%E8%B7%9F%E8%B8%AA%EF%BC%88%E5%9C%A8%E9%80%94%E7%9B%91%E6%8E%A7%EF%BC%89/</id>
<published>2020-09-30T08:31:07.000Z</published>
<updated>2020-10-02T09:22:33.515Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>效果</p><pre><code>![img](https://img-blog.csdn.net/20180917110653522?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI3NDAwMzM1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)</code></pre><p>官网地址:<a href="http://www.kdniao.com/api-monitor" target="_blank" rel="noopener">http://www.kdniao.com/api-monitor</a></p><pre class=" language-html"><code class="language-html">/** 工具类 **/import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import java.security.MessageDigest;import java.util.HashMap;import java.util.Map;/** * Created by Administrator on 2018/9/13 0013. */public class LogisticsUtils { private String code;//物流公司编码 private String logisticCode;//物流单号 private String orderSN;//订单号(可不传) public LogisticsUtils(String orderSN,String code, String logisticCode) { this.orderSN = orderSN; this.code = code; this.logisticCode = logisticCode; } //电商ID private String EBusinessID="请到快递鸟官网申请http://www.kdniao.com/ServiceApply.aspx"; //电商加密私钥,快递鸟提供,注意保管,不要泄漏 private String AppKey="请到快递鸟官网申请http://www.kdniao.com/ServiceApply.aspx"; //请求url private String ReqURL="http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx"; /** * Json方式 查询订单物流轨迹 * @throws Exception */ public String getOrderTracesByJson() throws Exception{ String requestData= "{'OrderCode':'','ShipperCode':'" + code + "','LogisticCode':'" + logisticCode + "'}"; Map<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>String,</span> <span class="token attr-name">String</span><span class="token punctuation">></span></span> params = new HashMap<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>String,</span> <span class="token attr-name">String</span><span class="token punctuation">></span></span>(); params.put("RequestData", urlEncoder(requestData, "UTF-8")); params.put("EBusinessID", EBusinessID); params.put("RequestType", "1002"); String dataSign=encrypt(requestData, AppKey, "UTF-8"); params.put("DataSign", urlEncoder(dataSign, "UTF-8")); params.put("DataType", "2"); String result=sendPost(ReqURL, params); //根据公司业务处理返回的信息...... return result; } /** * MD5加密 * @param str 内容 * @param charset 编码方式 * @throws Exception */ @SuppressWarnings("unused") private String MD5(String str, String charset) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes(charset)); byte[] result = md.digest(); StringBuffer sb = new StringBuffer(32); for (int i = 0; i < result.length; i++) { int val = result[i] & 0xff; if (val <= 0xf) { sb.append("0"); } sb.append(Integer.toHexString(val)); } return sb.toString().toLowerCase(); } /** * base64编码 * @param str 内容 * @param charset 编码方式 * @throws UnsupportedEncodingException */ private String base64(String str, String charset) throws UnsupportedEncodingException { String encoded = base64Encode(str.getBytes(charset)); return encoded; } @SuppressWarnings("unused") private String urlEncoder(String str, String charset) throws UnsupportedEncodingException{ String result = URLEncoder.encode(str, charset); return result; } /** * 电商Sign签名生成 * @param content 内容 * @param keyValue Appkey * @param charset 编码方式 * @throws UnsupportedEncodingException ,Exception * @return DataSign签名 */ @SuppressWarnings("unused") private String encrypt (String content, String keyValue, String charset) throws Exception { if (keyValue != null) { return base64(MD5(content + keyValue, charset), charset); } return base64(MD5(content, charset), charset); } /** * 向指定 URL 发送POST方法的请求 * @param url 发送请求的 URL * @param params 请求的参数集合 * @return 远程资源的响应结果 */ @SuppressWarnings("unused") private String sendPost(String url, Map<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>String,</span> <span class="token attr-name">String</span><span class="token punctuation">></span></span> params) { OutputStreamWriter out = null; BufferedReader in = null; StringBuilder result = new StringBuilder(); try { URL realUrl = new URL(url); HttpURLConnection conn =(HttpURLConnection) realUrl.openConnection(); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // POST方法 conn.setRequestMethod("POST"); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.connect(); // 获取URLConnection对象对应的输出流 out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); // 发送请求参数 if (params != null) { StringBuilder param = new StringBuilder(); for (Map.Entry<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>String,</span> <span class="token attr-name">String</span><span class="token punctuation">></span></span> entry : params.entrySet()) { if(param.length()>0){ param.append("&"); } param.append(entry.getKey()); param.append("="); param.append(entry.getValue()); System.out.println(entry.getKey()+":"+entry.getValue()); } System.out.println("param:"+param.toString()); out.write(param.toString()); } // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8")); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { e.printStackTrace(); L.i("TAG_LogisticsException","e="+e.toString()); } //使用finally块来关闭输出流、输入流 finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result.toString(); } private static char[] base64EncodeChars = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' }; public static String base64Encode(byte[] data) { StringBuffer sb = new StringBuffer(); int len = data.length; int i = 0; int b1, b2, b3; while (i < len) { b1 = data[i++] & 0xff; if (i == len) { sb.append(base64EncodeChars[b1 >>> 2]); sb.append(base64EncodeChars[(b1 & 0x3) << 4]); sb.append("=="); break; } b2 = data[i++] & 0xff; if (i == len) { sb.append(base64EncodeChars[b1 >>> 2]); sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]); sb.append(base64EncodeChars[(b2 & 0x0f) << 2]); sb.append("="); break; } b3 = data[i++] & 0xff; sb.append(base64EncodeChars[b1 >>> 2]); sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]); sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]); sb.append(base64EncodeChars[b3 & 0x3f]); } return sb.toString(); }}</code></pre><p><strong>Activity调用</strong></p><p><strong>//开启线程(主线程不能进行耗时操作)</strong></p><pre class=" language-html"><code class="language-html">new Thread(){ @Override public void run() { super.run(); LogisticsUtils api=new LogisticsUtils("订单号(可不传)","物流公司编码","物流单号"); String resultJson= null; try { resultJson = api.getOrderTracesByJson(); mLogistics=JSONObject.parseObject(resultJson, Logistics.class);//使用的的fastJson 解析 } catch (Exception e) { e.printStackTrace(); L.i("TAG_LogisticsException",e.toString()); }finally { mHandler.sendEmptyMessage(1);//在主线程更新ui } }}.start();private Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(mLogistics!=null){ //设置数据 } }};</code></pre><p>//实体类</p><pre class=" language-html"><code class="language-html">public class Logistics { private String LogisticCode; private String ShipperCode; private String State; private String EBusinessID; private boolean Success; private List<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TracesBean</span><span class="token punctuation">></span></span> Traces; public String getLogisticCode() { return LogisticCode; } public void setLogisticCode(String LogisticCode) { this.LogisticCode = LogisticCode; } public String getShipperCode() { return ShipperCode; } public void setShipperCode(String ShipperCode) { this.ShipperCode = ShipperCode; } public String getState() { return State; } public void setState(String State) { this.State = State; } public String getEBusinessID() { return EBusinessID; } public void setEBusinessID(String EBusinessID) { this.EBusinessID = EBusinessID; } public boolean isSuccess() { return Success; } public void setSuccess(boolean Success) { this.Success = Success; } public List<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TracesBean</span><span class="token punctuation">></span></span> getTraces() { return Traces; } public void setTraces(List<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TracesBean</span><span class="token punctuation">></span></span> Traces) { this.Traces = Traces; } public static class TracesBean { /** * AcceptStation : 【金华市】 【兰溪】(0579-88903278) 的 鲍光荣 (15356793398) 已揽收 * AcceptTime : 2018-08-15 18:45:57 */ private String AcceptStation; private String AcceptTime; public String getAcceptStation() { return AcceptStation; } public void setAcceptStation(String AcceptStation) { this.AcceptStation = AcceptStation; } public String getAcceptTime() { return AcceptTime; } public void setAcceptTime(String AcceptTime) { this.AcceptTime = AcceptTime; } }}</code></pre><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Android之Adapter用法总结</title>
<link href="http://yoursite.com/2020/09/29/Android%E4%B9%8BAdapter%E7%94%A8%E6%B3%95%E6%80%BB%E7%BB%93/"/>
<id>http://yoursite.com/2020/09/29/Android%E4%B9%8BAdapter%E7%94%A8%E6%B3%95%E6%80%BB%E7%BB%93/</id>
<published>2020-09-29T08:31:07.000Z</published>
<updated>2020-10-02T09:21:51.330Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>原文地址为: <a href="http://www.javaxxz.com/thread-366867-1-1.html" target="_blank" rel="noopener">Android之Adapter用法总结</a> </p><p>1.概念</p><p> Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带。在常见的View(ListView,GridView)等地方都需要用到Adapter。如下图直观的表达了Data、Adapter、View三者的关系:</p><p><img src="http://www.itdaan.com/imgs/7/6/3/0/90/5a031ddfef1b419fc712b2b85a30c61e.jpe" alt="img"></p><p>Android中所有的Adapter一览:</p><p><img src="http://www.itdaan.com/imgs/6/7/4/0/44/7d1d63e8855f237fec67a3f9fa0ed077.jpe" alt="img"></p><p> 由图可以看到在Android中与Adapter有关的所有接口、类的完整层级图。在我们使用过程中可以根据自己的需求实现接口或者继承类进行一定的扩展。比较常用的有 BaseAdapter,SimpleAdapter,ArrayAdapter,SimpleCursorAdapter等。</p><ul><li>BaseAdapter是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性;</li><li>ArrayAdapter支持泛型操作,最为简单,只能展示一行字。</li><li>SimpleAdapter有最好的扩充性,可以自定义出各种效果。</li><li>SimpleCursorAdapter可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。如需要实现更复杂的UI也可以重写其他方法。可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。</li></ul><p>2.应用案例</p><p>1)<strong>ArrayAdapter</strong></p><p>列表的显示需要三个元素:</p><p>a.ListVeiw 用来展示列表的View。</p><p>b.适配器 用来把数据映射到ListView上的中介。</p><p>c.数据 具体的将被映射的字符串,图片,或者基本组件。</p><p>案例一</p><pre><code>public class ArrayAdapterActivity extends ListActivity { @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//列表项的数据 String[] strs = {"1","2","3","4","5"}; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,strs); setListAdapter(adapter); } }</code></pre><p>案例二</p><pre><code> public class MyListView extends Activity {private ListView listView;//private List<String> data = new ArrayList<String>(); @Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); listView = new ListView(this); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData())); setContentView(listView); }private List<String> getData(){ List<String> data = new ArrayList<String>(); data.add("测试数据1"); data.add("测试数据2"); data.add("测试数据3"); data.add("测试数据4");return data; } }</code></pre><p> 上面代码使用了ArrayAdapter(Context context, int textViewResourceId, List<t> objects)来装配数据,要装配这些数据就需要一个连接ListView视图对象和数组数据的适配器来两者的适配工作,ArrayAdapter的构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。同时用setAdapter()完成适配的最后工作。效果图如下:</t></p><p><img src="http://www.itdaan.com/imgs/4/1/2/6/24/1c56fb0a27d372e7b3e4c6d0a627e9fb.jpe" alt="img"></p><p><strong>2)\</strong>SimpleAdapter**<br>** simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。</p><p>案例一</p><p>simple.xml</p><pre><code><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid:id="@+id/img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="5dp"/><TextViewandroid:id="@+id/title"android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff"android:textSize="20sp"/></LinearLayout>public class SimpleAdapterActivity extends ListActivity { @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.simple, new String[] { "title", "img" }, new int[] { R.id.title, R.id.img }); setListAdapter(adapter); }private List<Map<String, Object>> getData() {//map.put(参数名字,参数值) List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "摩托罗拉"); map.put("img", R.drawable.icon); list.add(map); map = new HashMap<String, Object>(); map.put("title", "诺基亚"); map.put("img", R.drawable.icon); list.add(map); map = new HashMap<String, Object>(); map.put("title", "三星"); map.put("img", R.drawable.icon); list.add(map);return list; } }</code></pre><p>案例二<br> 下面的程序是实现一个带有图片的类表。首先需要定义好一个用来显示每一个列内容的xml,vlist.xml</p><pre><code><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px"/><LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"><TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="22px" /><TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="13px" /></LinearLayout></LinearLayout>public class MyListView3 extends ListActivity {// private List<String> data = new ArrayList<String>(); @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist,new String[]{"title","info","img"},new int[]{R.id.title,R.id.info,R.id.img}); setListAdapter(adapter); }private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "G1"); map.put("info", "google 1"); map.put("img", R.drawable.i1); list.add(map); map = new HashMap<String, Object>(); map.put("title", "G2"); map.put("info", "google 2"); map.put("img", R.drawable.i2); list.add(map); map = new HashMap<String, Object>(); map.put("title", "G3"); map.put("info", "google 3"); map.put("img", R.drawable.i3); list.add(map);return list; } }</code></pre><p> <strong>使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。</strong>HashMap的每个键值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局vlist.xml。下面做适配,new一个SimpleAdapter参数一次是:this,布局文件(vlist.xml),HashMap的 title 和 info,img。布局文件的组件id,title,info,img。布局文件的各组件分别映射到HashMap的各元素上,完成适配。</p><p>运行效果如下图:</p><p><img src="http://www.itdaan.com/imgs/2/3/3/5/59/9e39493c7b6162a06ce2563889a4b7f7.jpe" alt="img"></p><p><strong>3)SimpleCursorAdapter</strong></p><pre><code>public class SimpleCursorAdapterActivity extends ListActivity { @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//获得一个指向系统通讯录数据库的Cursor对象获得数据来源 Cursor cur = getContentResolver().query(People.CONTENT_URI, null, null, null, null); startManagingCursor(cur);//实例化列表适配器 ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cur, new String[] {People.NAME}, new int[] {android.R.id.text1}); setListAdapter(adapter); } }</code></pre><p><strong>一定要以数据库作为数据源的时候,才能使用SimpleCursorAdapter,</strong>这里特别需要注意的一点是:不要忘了在AndroidManifest.xml文件中加入权限</p><pre><code><uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission></code></pre><p>效果如下:</p><p><img src="http://www.itdaan.com/imgs/2/2/3/7/21/988a4034d7f7b644d2def0983311ad70.jpe" alt="img"></p><p><strong>4)BaseAdapter</strong></p><p> 有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮。添加按钮首先要写一个有按钮的xml文件,然后自然会想到用上面的方法定义一个适配器,然后将数据映射到布局文件上。但是事实并非这样,因为按钮是无法映射的,即使你成功的用布局文件显示出了按钮也无法添加按钮的响应,这时就要研究一下ListView是如何现实的了,而且必须要重写一个类继承BaseAdapter。下面的示例将显示一个按钮和一个图片,两行字如果单击按钮将删除此按钮的所在行。并告诉你ListView究竟是如何工作的。</p><p>vlist2.xml</p><pre><code><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"><ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px"/><LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"><TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="22px" /><TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="13px" /></LinearLayout><Button android:id="@+id/view_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/s_view_btn" android:layout_gravity="bottom|right" /></LinearLayout></code></pre><pre><code>/**002 * @author 003 *004 */005 public class MyListView4 extends ListActivity {006 007 008 private List<Map<String, Object>> mData;009 010 @Override011 public void onCreate(Bundle savedInstanceState) {012 super.onCreate(savedInstanceState);013 mData = getData();014 MyAdapter adapter = new MyAdapter(this);015 setListAdapter(adapter);016 }017 018 private List<Map<String, Object>> getData() {019 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();020 021 Map<String, Object> map = new HashMap<String, Object>();022 map.put("title", "G1");023 map.put("info", "google 1");024 map.put("img", R.drawable.i1);025 list.add(map);026 027 map = new HashMap<String, Object>();028 map.put("title", "G2");029 map.put("info", "google 2");030 map.put("img", R.drawable.i2);031 list.add(map);032 033 map = new HashMap<String, Object>();034 map.put("title", "G3");035 map.put("info", "google 3");036 map.put("img", R.drawable.i3);037 list.add(map);038 039 return list;040 }041 042 // ListView 中某项被选中后的逻辑043 @Override044 protected void onListItemClick(ListView l, View v, int position, long id) {045 046 Log.v("MyListView4-click", (String)mData.get(position).get("title"));047 }048 049 /**050 * listview中点击按键弹出对话框051 */052 public void showInfo(){053 new AlertDialog.Builder(this)054 .setTitle("我的listview")055 .setMessage("介绍...")056 .setPositiveButton("确定", new DialogInterface.OnClickListener() {057 @Override058 public void onClick(DialogInterface dialog, int which) {059 }060 })061 .show();062 063 }064 065 066 067 public final class ViewHolder{068 public ImageView img;069 public TextView title;070 public TextView info;071 public Button viewBtn;072 }073 074 075 public class MyAdapter extends BaseAdapter{076 077 private LayoutInflater mInflater;078 079 080 public MyAdapter(Context context){081 this.mInflater = LayoutInflater.from(context);082 }083 @Override084 public int getCount() {085 // TODO Auto-generated method stub086 return mData.size();087 }088 089 @Override090 public Object getItem(int arg0) {091 // TODO Auto-generated method stub092 return null;093 }094 095 @Override096 public long getItemId(int arg0) {097 // TODO Auto-generated method stub098 return 0;099 }100 101 @Override102 public View getView(int position, View convertView, ViewGroup parent) {103 104 ViewHolder holder = null;105 if (convertView == null) {106 107 holder=new ViewHolder(); 108 109 convertView = mInflater.inflate(R.layout.vlist2, null);110 holder.img = (ImageView)convertView.findViewById(R.id.img);111 holder.title = (TextView)convertView.findViewById(R.id.title);112 holder.info = (TextView)convertView.findViewById(R.id.info);113 holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);114 convertView.setTag(holder);115 116 }else {117 118 holder = (ViewHolder)convertView.getTag();119 }120 121 122 holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));123 holder.title.setText((String)mData.get(position).get("title"));124 holder.info.setText((String)mData.get(position).get("info"));125 126 holder.viewBtn.setOnClickListener(new View.OnClickListener() {127 128 @Override129 public void onClick(View v) {130 showInfo(); 131 }132 });133 134 135 return convertView;136 }137 138 } 139 }</code></pre><p> 下面将对上述代码,做详细的解释,listView在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得到listView的长度(这也是为什么在开始的第一张图特别的标出列表长度),然后根据这个长度,调用getView()逐一绘制每一行。如果你的getCount()返回值是0的话,列表将不显示同样return 1,就只显示一行。</p><p> 系统显示列表时,首先实例化一个适配器(这里将实例化自定义的适配器)。当手动完成适配时,必须手动映射数据,这需要重写getView()方法。系统在绘制列表的每一行的时候将调用此方法。getView()有三个参数,position表示将显示的是第几行,covertView是从布局文件中inflate来的布局。我们用LayoutInflater的方法将定义好的vlist2.xml文件提取成View实例用来显示。然后将xml文件中的各个组件实例化(简单的findViewById()方法)。这样便可以将数据对应到各个组件上了。但是按钮为了响应点击事件,需要为它添加点击监听器,这样就能捕获点击事件。至此一个自定义的listView就完成了,现在让我们回过头从新审视这个过程。系统要绘制ListView了,他首先获得要绘制的这个列表的长度,然后开始绘制第一行,怎么绘制呢?调用getView()函数。在这个函数里面首先获得一个View(实际上是一个ViewGroup),然后再实例并设置各个组件,显示之。好了,绘制完这一行了。那再绘制下一行,直到绘完为止。在实际的运行过程中会发现listView的每一行没有焦点了,这是因为Button抢夺了listView的焦点,只要布局文件中将Button设置为没有焦点就OK了。</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>如何在Fragment中使用findViewById</title>
<link href="http://yoursite.com/2020/09/29/%E5%A6%82%E4%BD%95%E5%9C%A8Fragment%E4%B8%AD%E4%BD%BF%E7%94%A8findViewById/"/>
<id>http://yoursite.com/2020/09/29/%E5%A6%82%E4%BD%95%E5%9C%A8Fragment%E4%B8%AD%E4%BD%BF%E7%94%A8findViewById/</id>
<published>2020-09-29T08:31:07.000Z</published>
<updated>2020-10-02T09:20:51.618Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>findViewById方法却只能被用在Activity类中,如果想在fragment中使用,</p><p>需要在findViewById前面 添加getView();</p><p>下面是一段代码,</p><pre><code>public class SquareFragment extends Fragment { private TextView sq; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View square = inflater.inflate(R.layout.square_layout, container, false); sq =getView().findViewById(R.id.square); sq.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { MainActivity ma = (MainActivity) getActivity(); ma.setTabSelection(2); } }); return square; }}</code></pre><p>这样用getView()前面没有问题,但是运行会发现空指针异常,原因呢是在使用了onCreateView创建视图, inflater插入的布局,用getView()引用,不是识别是哪一个布局</p><p>所以就报了空指针,</p><p>正确的写法如下</p><pre><code>public class SquareFragment extends Fragment { private TextView sq; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View square = inflater.inflate(R.layout.square_layout, container, false); sq = square.findViewById(R.id.square); sq.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { MainActivity ma = (MainActivity) getActivity(); ma.setTabSelection(2); } }); return square; }}</code></pre><p>在findViewById前面添加 插入的那个布局,它就能识别是哪一个布局了。</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>安卓连接数据库实现登陆注册</title>
<link href="http://yoursite.com/2020/09/28/%E5%AE%89%E5%8D%93%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AE%9E%E7%8E%B0%E7%99%BB%E9%99%86%E6%B3%A8%E5%86%8C/"/>
<id>http://yoursite.com/2020/09/28/%E5%AE%89%E5%8D%93%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AE%9E%E7%8E%B0%E7%99%BB%E9%99%86%E6%B3%A8%E5%86%8C/</id>
<published>2020-09-28T10:31:07.000Z</published>
<updated>2020-10-02T09:32:03.369Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>有一段时间没有更新博客了,以前小编都是发布有关spring boot的博客,今天换换口味,为大家带来安卓的Demo。<br>下载地址<a href="https://download.csdn.net/download/weixin_43055096/11049453" target="_blank" rel="noopener">https://download.csdn.net/download/weixin_43055096/11049453</a></p><h1 id="前期准备"><a href="#前期准备" class="headerlink" title="前期准备"></a>前期准备</h1><p><strong>1:下载安装并配置Andriod studio</strong></p><p><strong>2:准备好mysql-connector-java-5.1.47.jar,这个驱动包网上都能下载</strong></p><p><strong>3:创建一个空的Activity</strong></p><p><strong>4:修改权限(很重要)</strong><br>如图<br><img src="https://img-blog.csdnimg.cn/20190322135725826.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzA1NTA5Ng==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p><h1 id="主体"><a href="#主体" class="headerlink" title="主体"></a>主体</h1><p>1.导入mysql-connector-java-5.1.47.jar包 然后build library(其他的包可以不用管) <img src="https://img-blog.csdnimg.cn/20190322133449887.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzA1NTA5Ng==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p><p>2.创建JdbcUtil</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>wuge<span class="token punctuation">.</span>dbtext<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>File<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>FileInputStream<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span>Connection<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span>DriverManager<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Properties<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">JdbcUtil</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token keyword">static</span> JdbcUtil instance<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">static</span> JdbcUtil <span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>instance <span class="token operator">==</span>null<span class="token punctuation">)</span><span class="token punctuation">{</span> instance <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">JdbcUtil</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> instance<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> Connection <span class="token function">getConnection</span><span class="token punctuation">(</span>String dbName<span class="token punctuation">,</span>String name<span class="token punctuation">,</span>String password<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> Class<span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">"com.mysql.jdbc.Driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String url <span class="token operator">=</span> <span class="token string">"jdbc:mysql://192.168.43.104:3306/"</span><span class="token operator">+</span>dbName<span class="token punctuation">;</span> <span class="token keyword">return</span> DriverManager<span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span>name<span class="token punctuation">,</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> null<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> Connection <span class="token function">getConnection</span><span class="token punctuation">(</span>String file<span class="token punctuation">)</span><span class="token punctuation">{</span> File f <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>f<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> null<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span> Properties pro <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> Class<span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">"com.mysql.jdbc.Driver"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> pro<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String url <span class="token operator">=</span> pro<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"url"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String name <span class="token operator">=</span> pro<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String password <span class="token operator">=</span> pro<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"password"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> DriverManager<span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span>name<span class="token punctuation">,</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> null<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token number">123456789101112131415161718192021222324252627282930313233343536373839404142434445464748</span></code></pre><h6 id="小编这里有两个getConnection,这里我们用到的是第一个,第二个是为了更加方便,直接把数据库相关信息写到文件通过文件找到相应的账号密码以及url"><a href="#小编这里有两个getConnection,这里我们用到的是第一个,第二个是为了更加方便,直接把数据库相关信息写到文件通过文件找到相应的账号密码以及url" class="headerlink" title="小编这里有两个getConnection,这里我们用到的是第一个,第二个是为了更加方便,直接把数据库相关信息写到文件通过文件找到相应的账号密码以及url"></a>小编这里有两个getConnection,这里我们用到的是第一个,第二个是为了更加方便,直接把数据库相关信息写到文件通过文件找到相应的账号密码以及url</h6><h3 id="JdbcUtil注意点"><a href="#JdbcUtil注意点" class="headerlink" title="JdbcUtil注意点"></a>JdbcUtil注意点</h3><p>1.这里的url是会动态变化的,他是根据你连接的网络不同而改变的,如何查找呢,下图给出查找的方式。</p><p>我们直接在控制台输入ipconfig ,红色箭头的地方就是我们需要的ip地址<br><img src="https://img-blog.csdnimg.cn/20190322134214982.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzA1NTA5Ng==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p><p>3.创建 UserDao</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>wuge<span class="token punctuation">.</span>dbtext<span class="token punctuation">;</span><span class="token keyword">import</span> android<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Log<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span>Connection<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span>PreparedStatement<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span>ResultSet<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>sql<span class="token punctuation">.</span>SQLException<span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token keyword">static</span> android<span class="token punctuation">.</span>content<span class="token punctuation">.</span>ContentValues<span class="token punctuation">.</span>TAG<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserDao</span> <span class="token punctuation">{</span> JdbcUtil jdbcUtil <span class="token operator">=</span> JdbcUtil<span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//第一个参数为数据库名称,第二个参数为数据库账号 第三个参数为数据库密码</span> Connection conn <span class="token operator">=</span> jdbcUtil<span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span><span class="token string">"Dbtext"</span><span class="token punctuation">,</span><span class="token string">"root"</span><span class="token punctuation">,</span><span class="token string">"password"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//注册</span> <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">register</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span>String password<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>conn<span class="token operator">==</span>null<span class="token punctuation">)</span><span class="token punctuation">{</span> Log<span class="token punctuation">.</span><span class="token function">i</span><span class="token punctuation">(</span>TAG<span class="token punctuation">,</span><span class="token string">"register:conn is null"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//进行数据库操作</span> String sql <span class="token operator">=</span> <span class="token string">"insert into user(name,password) values(?,?)"</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> PreparedStatement pre <span class="token operator">=</span> conn<span class="token punctuation">.</span><span class="token function">prepareStatement</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span> pre<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> pre<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> pre<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">finally</span> <span class="token punctuation">{</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> conn<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//登录</span> <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">login</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span>String password<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>conn<span class="token operator">==</span>null<span class="token punctuation">)</span><span class="token punctuation">{</span> Log<span class="token punctuation">.</span><span class="token function">i</span><span class="token punctuation">(</span>TAG<span class="token punctuation">,</span><span class="token string">"register:conn is null"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span> String sql <span class="token operator">=</span> <span class="token string">"select * from user where name=? and password=?"</span><span class="token punctuation">;</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> PreparedStatement pres <span class="token operator">=</span> conn<span class="token punctuation">.</span><span class="token function">prepareStatement</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span> pres<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> pres<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span> ResultSet res <span class="token operator">=</span> pres<span class="token punctuation">.</span><span class="token function">executeQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">boolean</span> t <span class="token operator">=</span> res<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> t<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token number">123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263</span></code></pre><p>3.完成咋们的xml 小编这里做的很简陋</p><pre class=" language-java"><code class="language-java"><span class="token operator"><</span><span class="token operator">?</span>xml version<span class="token operator">=</span><span class="token string">"1.0"</span> encoding<span class="token operator">=</span><span class="token string">"utf-8"</span><span class="token operator">?</span><span class="token operator">></span><span class="token operator"><</span>android<span class="token punctuation">.</span>support<span class="token punctuation">.</span>constraint<span class="token punctuation">.</span>ConstraintLayout xmlns<span class="token operator">:</span>android<span class="token operator">=</span><span class="token string">"http://schemas.android.com/apk/res/android"</span> xmlns<span class="token operator">:</span>app<span class="token operator">=</span><span class="token string">"http://schemas.android.com/apk/res-auto"</span> xmlns<span class="token operator">:</span>tools<span class="token operator">=</span><span class="token string">"http://schemas.android.com/tools"</span> android<span class="token operator">:</span>layout_width<span class="token operator">=</span><span class="token string">"match_parent"</span> android<span class="token operator">:</span>layout_height<span class="token operator">=</span><span class="token string">"match_parent"</span> tools<span class="token operator">:</span>context<span class="token operator">=</span><span class="token string">".MainActivity"</span><span class="token operator">></span> <span class="token operator"><</span>EditText android<span class="token operator">:</span>id<span class="token operator">=</span><span class="token string">"@+id/password"</span> android<span class="token operator">:</span>layout_width<span class="token operator">=</span><span class="token string">"wrap_content"</span> android<span class="token operator">:</span>layout_height<span class="token operator">=</span><span class="token string">"wrap_content"</span> android<span class="token operator">:</span>ems<span class="token operator">=</span><span class="token string">"10"</span> android<span class="token operator">:</span>inputType<span class="token operator">=</span><span class="token string">"textPersonName"</span> android<span class="token operator">:</span>hint<span class="token operator">=</span><span class="token string">"请输入密码"</span> app<span class="token operator">:</span>layout_constraintBottom_toBottomOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintEnd_toEndOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintHorizontal_bias<span class="token operator">=</span><span class="token string">"0.5"</span> app<span class="token operator">:</span>layout_constraintStart_toStartOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintTop_toTopOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintVertical_bias<span class="token operator">=</span><span class="token string">"0.4"</span> <span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>EditText android<span class="token operator">:</span>id<span class="token operator">=</span><span class="token string">"@+id/name"</span> android<span class="token operator">:</span>layout_width<span class="token operator">=</span><span class="token string">"wrap_content"</span> android<span class="token operator">:</span>layout_height<span class="token operator">=</span><span class="token string">"wrap_content"</span> android<span class="token operator">:</span>ems<span class="token operator">=</span><span class="token string">"10"</span> android<span class="token operator">:</span>hint<span class="token operator">=</span><span class="token string">"请输入用户名"</span> android<span class="token operator">:</span>inputType<span class="token operator">=</span><span class="token string">"textPersonName"</span> app<span class="token operator">:</span>layout_constraintBottom_toBottomOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintEnd_toEndOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintHorizontal_bias<span class="token operator">=</span><span class="token string">"0.5"</span> app<span class="token operator">:</span>layout_constraintStart_toStartOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintTop_toTopOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintVertical_bias<span class="token operator">=</span><span class="token string">"0.2"</span> <span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>Button android<span class="token operator">:</span>id<span class="token operator">=</span><span class="token string">"@+id/register"</span> android<span class="token operator">:</span>layout_width<span class="token operator">=</span><span class="token string">"wrap_content"</span> android<span class="token operator">:</span>layout_height<span class="token operator">=</span><span class="token string">"wrap_content"</span> android<span class="token operator">:</span>onClick<span class="token operator">=</span><span class="token string">"fun"</span> android<span class="token operator">:</span>text<span class="token operator">=</span><span class="token string">"注册"</span> app<span class="token operator">:</span>layout_constraintBottom_toBottomOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintEnd_toEndOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintHorizontal_bias<span class="token operator">=</span><span class="token string">"0.8"</span> app<span class="token operator">:</span>layout_constraintStart_toStartOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintTop_toTopOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintVertical_bias<span class="token operator">=</span><span class="token string">"0.6"</span> <span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>Button android<span class="token operator">:</span>id<span class="token operator">=</span><span class="token string">"@+id/login"</span> android<span class="token operator">:</span>layout_width<span class="token operator">=</span><span class="token string">"wrap_content"</span> android<span class="token operator">:</span>layout_height<span class="token operator">=</span><span class="token string">"wrap_content"</span> android<span class="token operator">:</span>onClick<span class="token operator">=</span><span class="token string">"fun"</span> android<span class="token operator">:</span>text<span class="token operator">=</span><span class="token string">"登录"</span> app<span class="token operator">:</span>layout_constraintBottom_toBottomOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintEnd_toEndOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintHorizontal_bias<span class="token operator">=</span><span class="token string">"0.2"</span> app<span class="token operator">:</span>layout_constraintStart_toStartOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintTop_toTopOf<span class="token operator">=</span><span class="token string">"parent"</span> app<span class="token operator">:</span>layout_constraintVertical_bias<span class="token operator">=</span><span class="token string">"0.6"</span> <span class="token operator">/</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>android<span class="token punctuation">.</span>support<span class="token punctuation">.</span>constraint<span class="token punctuation">.</span>ConstraintLayout <span class="token operator">></span><span class="token number">1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162</span></code></pre><p>效果如下<img src="https://img-blog.csdnimg.cn/20190322134935773.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzA1NTA5Ng==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br>4.我们开始写主页面</p><pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>wuge<span class="token punctuation">.</span>dbtext<span class="token punctuation">;</span><span class="token keyword">import</span> android<span class="token punctuation">.</span>content<span class="token punctuation">.</span>Intent<span class="token punctuation">;</span><span class="token keyword">import</span> android<span class="token punctuation">.</span>os<span class="token punctuation">.</span>Looper<span class="token punctuation">;</span><span class="token keyword">import</span> android<span class="token punctuation">.</span>os<span class="token punctuation">.</span>Trace<span class="token punctuation">;</span><span class="token keyword">import</span> android<span class="token punctuation">.</span>support<span class="token punctuation">.</span>v7<span class="token punctuation">.</span>app<span class="token punctuation">.</span>AppCompatActivity<span class="token punctuation">;</span><span class="token keyword">import</span> android<span class="token punctuation">.</span>os<span class="token punctuation">.</span>Bundle<span class="token punctuation">;</span><span class="token keyword">import</span> android<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Log<span class="token punctuation">;</span><span class="token keyword">import</span> android<span class="token punctuation">.</span>view<span class="token punctuation">.</span>View<span class="token punctuation">;</span><span class="token keyword">import</span> android<span class="token punctuation">.</span>widget<span class="token punctuation">.</span>EditText<span class="token punctuation">;</span><span class="token keyword">import</span> android<span class="token punctuation">.</span>widget<span class="token punctuation">.</span>Toast<span class="token punctuation">;</span><span class="token keyword">import</span> com<span class="token punctuation">.</span>google<span class="token punctuation">.</span>gson<span class="token punctuation">.</span>JsonObject<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>json<span class="token punctuation">.</span>JSONObject<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>File<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>IOException<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>HashMap<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Map<span class="token punctuation">;</span><span class="token keyword">import</span> okhttp3<span class="token punctuation">.</span>Call<span class="token punctuation">;</span><span class="token keyword">import</span> okhttp3<span class="token punctuation">.</span>Callback<span class="token punctuation">;</span><span class="token keyword">import</span> okhttp3<span class="token punctuation">.</span>Response<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MainActivity</span> <span class="token keyword">extends</span> <span class="token class-name">AppCompatActivity</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> String TAG<span class="token operator">=</span><span class="token string">"MainActivity"</span><span class="token punctuation">;</span> <span class="token keyword">private</span> EditText name<span class="token punctuation">;</span> <span class="token keyword">private</span> EditText password<span class="token punctuation">;</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">onCreate</span><span class="token punctuation">(</span>Bundle savedInstanceState<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">onCreate</span><span class="token punctuation">(</span>savedInstanceState<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">setContentView</span><span class="token punctuation">(</span>R<span class="token punctuation">.</span>layout<span class="token punctuation">.</span>activity_main<span class="token punctuation">)</span><span class="token punctuation">;</span> name <span class="token operator">=</span> <span class="token function">findViewById</span><span class="token punctuation">(</span>R<span class="token punctuation">.</span>id<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> password <span class="token operator">=</span> <span class="token function">findViewById</span><span class="token punctuation">(</span>R<span class="token punctuation">.</span>id<span class="token punctuation">.</span>password<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//用户根据点击事件来找到相应的功能</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">fun</span><span class="token punctuation">(</span>View v<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">switch</span> <span class="token punctuation">(</span>v<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">case</span> R<span class="token punctuation">.</span>id<span class="token punctuation">.</span>register<span class="token operator">:</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String n <span class="token operator">=</span> name<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String psw <span class="token operator">=</span> password<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> UserDao ud <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">UserDao</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">boolean</span> result <span class="token operator">=</span>ud<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span>psw<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>result<span class="token punctuation">)</span><span class="token punctuation">{</span> Looper<span class="token punctuation">.</span><span class="token function">prepare</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Toast toast <span class="token operator">=</span> Toast<span class="token punctuation">.</span><span class="token function">makeText</span><span class="token punctuation">(</span>MainActivity<span class="token punctuation">.</span><span class="token keyword">this</span><span class="token punctuation">,</span><span class="token string">"注册成功!"</span><span class="token punctuation">,</span>Toast<span class="token punctuation">.</span>LENGTH_SHORT<span class="token punctuation">)</span><span class="token punctuation">;</span> toast<span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Looper<span class="token punctuation">.</span><span class="token function">loop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> Log<span class="token punctuation">.</span><span class="token function">i</span><span class="token punctuation">(</span>TAG<span class="token punctuation">,</span><span class="token string">"fun"</span><span class="token operator">+</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//以上为jdbc注册</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> R<span class="token punctuation">.</span>id<span class="token punctuation">.</span>login<span class="token operator">:</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> String n <span class="token operator">=</span> name<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String psw <span class="token operator">=</span> password<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>n<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token operator">||</span>psw<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Looper<span class="token punctuation">.</span><span class="token function">prepare</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Toast toast <span class="token operator">=</span> Toast<span class="token punctuation">.</span><span class="token function">makeText</span><span class="token punctuation">(</span>MainActivity<span class="token punctuation">.</span><span class="token keyword">this</span><span class="token punctuation">,</span><span class="token string">"输入不能为空!"</span><span class="token punctuation">,</span>Toast<span class="token punctuation">.</span>LENGTH_SHORT<span class="token punctuation">)</span><span class="token punctuation">;</span> toast<span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Looper<span class="token punctuation">.</span><span class="token function">loop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> UserDao ud <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">UserDao</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Boolean result <span class="token operator">=</span> ud<span class="token punctuation">.</span><span class="token function">login</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span>psw<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>result<span class="token punctuation">)</span><span class="token punctuation">{</span> Looper<span class="token punctuation">.</span><span class="token function">prepare</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Toast toast<span class="token operator">=</span>Toast<span class="token punctuation">.</span><span class="token function">makeText</span><span class="token punctuation">(</span>MainActivity<span class="token punctuation">.</span><span class="token keyword">this</span><span class="token punctuation">,</span><span class="token string">"用户名不存在或密码错误!"</span><span class="token punctuation">,</span>Toast<span class="token punctuation">.</span>LENGTH_SHORT<span class="token punctuation">)</span><span class="token punctuation">;</span> toast<span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Looper<span class="token punctuation">.</span><span class="token function">loop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span> Looper<span class="token punctuation">.</span><span class="token function">prepare</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Toast toast<span class="token operator">=</span>Toast<span class="token punctuation">.</span><span class="token function">makeText</span><span class="token punctuation">(</span>MainActivity<span class="token punctuation">.</span><span class="token keyword">this</span><span class="token punctuation">,</span><span class="token string">"登录成功"</span><span class="token punctuation">,</span>Toast<span class="token punctuation">.</span>LENGTH_SHORT<span class="token punctuation">)</span><span class="token punctuation">;</span> toast<span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//一下代码为跳转界面</span> <span class="token comment" spellcheck="true">// Intent intent=new Intent(MainActivity.this,info.class);</span> <span class="token comment" spellcheck="true">//intent.putExtra("name",n);</span> <span class="token comment" spellcheck="true">// startActivity(intent);</span> Looper<span class="token punctuation">.</span><span class="token function">loop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//以上为jdbc登录</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token number">123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101</span></code></pre><p>小编这里用到了线程 ,想告诉大家的是 ,所有的Http请求都要写在子线程里面,而且如果想在子线程里面修改UI线程,比如我们的Toast 我们就要引入<br>Looper.prepare(); Looper.loop();</p><h1 id="子线程修改UI线程的方法"><a href="#子线程修改UI线程的方法" class="headerlink" title="子线程修改UI线程的方法"></a>子线程修改UI线程的方法</h1><ol><li>Looper.prepare();<br>//TODO<br>Looper.loop();</li></ol><p>2.在Activity 可用runUiThread</p><p>3.Handler</p><p>小编最推荐的是第三种 第二种有局限 只能在同一个Activity上面使用</p><h1 id="最后"><a href="#最后" class="headerlink" title="最后"></a>最后</h1><p>到这里就就结束了 最后展示效果图<br>(小编这里是老人机)<br><img src="https://img-blog.csdnimg.cn/20190322140056687.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzA1NTA5Ng==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/20190322135938991.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzA1NTA5Ng==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Android studio 打包apk后发给别人,安装失败</title>
<link href="http://yoursite.com/2020/09/27/Android%20studio%20%E6%89%93%E5%8C%85apk%E5%90%8E%E5%8F%91%E7%BB%99%E5%88%AB%E4%BA%BA%EF%BC%8C%E5%AE%89%E8%A3%85%E5%A4%B1%E8%B4%A5/"/>
<id>http://yoursite.com/2020/09/27/Android%20studio%20%E6%89%93%E5%8C%85apk%E5%90%8E%E5%8F%91%E7%BB%99%E5%88%AB%E4%BA%BA%EF%BC%8C%E5%AE%89%E8%A3%85%E5%A4%B1%E8%B4%A5/</id>
<published>2020-09-27T10:31:07.000Z</published>
<updated>2020-10-02T09:31:21.051Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>Android studio 打包apk后发给别人,安装失败。</p><p>失败原因显示:应用是非正式发布版本,当前设备不支持安装。</p><p>在自己手机上也无法安装,之后连接USB进行调试时可以运行,打包成APK安装就会显示下图错误:</p><p><a href="https://img2020.cnblogs.com/blog/1271267/202007/1271267-20200708221826346-2000750887.jpg" target="_blank" rel="noopener"><img src="https://img2020.cnblogs.com/blog/1271267/202007/1271267-20200708221826346-2000750887.jpg" alt="img"></a></p><p><strong><em>2\</em></strong>|<strong><em>0**</em></strong>原因分析:**</p><blockquote><p>造成该问题的原因是Android Studio 3.0会在debug apk的manifest文件application标签里自动添加 android:testOnly=”true”属性。该属性导致在IDE中使用Run生成的apk在大部分手机上只能用adb install -t 来安装。</p></blockquote><p> 这种apk在某些手机上甚至安装不了。</p><p><strong><em>3\</em></strong>|<strong><em>0**</em></strong>解决方法:**</p><p>在gradle.properties 文件中添加如下指令:</p><pre><code>android.injected.testOnly=false</code></pre><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>仿淘宝物流的跟踪图,不需要自定义时间轴</title>
<link href="http://yoursite.com/2020/09/27/%E4%BB%BF%E6%B7%98%E5%AE%9D%E7%89%A9%E6%B5%81%E7%9A%84%E8%B7%9F%E8%B8%AA%E5%9B%BE%EF%BC%8C%E4%B8%8D%E9%9C%80%E8%A6%81%E8%87%AA%E5%AE%9A%E4%B9%89%E6%97%B6%E9%97%B4%E8%BD%B4/"/>
<id>http://yoursite.com/2020/09/27/%E4%BB%BF%E6%B7%98%E5%AE%9D%E7%89%A9%E6%B5%81%E7%9A%84%E8%B7%9F%E8%B8%AA%E5%9B%BE%EF%BC%8C%E4%B8%8D%E9%9C%80%E8%A6%81%E8%87%AA%E5%AE%9A%E4%B9%89%E6%97%B6%E9%97%B4%E8%BD%B4/</id>
<published>2020-09-27T08:31:07.000Z</published>
<updated>2020-10-02T09:19:53.247Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>最近遇到一个项目需要实现如下图这种流程</p><p><img src="https://img-blog.csdn.net/20180716143614365?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Z1emhvbmdiaW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="img"></p><p>看了网上很多都是要自定义时间轴配合ListView来实现,但是本项目的流程又区别于淘宝物流的跟踪图,所以自己写了一个流程图,代码如下:</p><p>item的布局 view_process_item.xml:</p><pre class=" language-html"><code class="language-html"><span class="token prolog"><?xml version="1.0" encoding="utf-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>LinearLayout</span> <span class="token attr-name"><span class="token namespace">xmlns:</span>android</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://schemas.android.com/apk/res/android<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>background</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@color/col_ffffff<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>orientation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>vertical<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>tools</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://schemas.android.com/tools<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>LinearLayout</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/ll_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>orientation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>horizontal<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>RelativeLayout</span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>120dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>40dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_gravity</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>center_vertical<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TextView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/tv_title<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>paddingLeft</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>5dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>textSize</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@dimen/text_12sp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>textColor</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@color/col_3f6db7<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">tools:</span>text</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>撰写审计日志<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TextView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/tv_date<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_below</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@id/tv_title<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>paddingLeft</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>5dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>textSize</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@dimen/text_12sp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>textColor</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@color/col_3f6db7<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">tools:</span>text</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>2018-12-10 10:50<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>RelativeLayout</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>LinearLayout</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/ll_line<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>14dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>orientation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>vertical<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>gravity</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>center_horizontal<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TextView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/line_before<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>1dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>40dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>background</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@color/col_3f6db7<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>gravity</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>center_horizontal<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TextView</span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>14dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>14dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>background</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@drawable/shape_circle_process<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TextView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/line_after<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>1dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>40dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>background</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@color/col_3f6db7<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>gravity</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>center_horizontal<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>LinearLayout</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>RelativeLayout</span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_marginLeft</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>10dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_marginRight</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>10dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>minHeight</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>40dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>padding</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>2dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_gravity</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>center<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>gravity</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>center_vertical<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>background</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@drawable/shap_tangle_bg<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TextView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/tv_name<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>textSize</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@dimen/text_15sp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">tools:</span>text</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>张三<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TextView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/tv_advice<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_toRightOf</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@id/tv_name<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_marginLeft</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>10dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>textSize</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@dimen/text_15sp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">tools:</span>text</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>同意<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TextView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/tv_result<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_below</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@id/tv_name<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>textSize</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@dimen/text_15sp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">tools:</span>text</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>审核同意地方撒还记得哈返回撒到了范德萨看了凤凰读书了发大水范德萨范德萨发生<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>RelativeLayout</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>LinearLayout</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>LinearLayout</span><span class="token punctuation">></span></span></code></pre><p>自定义的drawable:</p><p>1.进度节点圆圈 shape_circle_process.xml</p><pre class=" language-html"><code class="language-html"><span class="token prolog"><?xml version="1.0" encoding="utf-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>shape</span> <span class="token attr-name"><span class="token namespace">xmlns:</span>android</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://schemas.android.com/apk/res/android<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>shape</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>oval<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>corners</span> <span class="token attr-name"><span class="token namespace">android:</span>radius</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>7dp<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>solid</span> <span class="token attr-name"><span class="token namespace">android:</span>color</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@color/col_3f6db7<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>shape</span><span class="token punctuation">></span></span></code></pre><p>2.审批内容背景 shape_tangle_bg.xml</p><pre class=" language-html"><code class="language-html"><span class="token prolog"><?xml version="1.0" encoding="utf-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>shape</span> <span class="token attr-name"><span class="token namespace">xmlns:</span>android</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://schemas.android.com/apk/res/android<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>shape</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>rectangle<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>stroke</span> <span class="token attr-name"><span class="token namespace">android:</span>color</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@color/col_636363<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>1px<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>solid</span> <span class="token attr-name"><span class="token namespace">android:</span>color</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@color/col_f0f0f0<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>shape</span><span class="token punctuation">></span></span></code></pre><p>Activity中的布局:</p><pre class=" language-html"><code class="language-html"><span class="token prolog"><?xml version="1.0" encoding="utf-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>LinearLayout</span> <span class="token attr-name"><span class="token namespace">xmlns:</span>android</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://schemas.android.com/apk/res/android<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>app</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://schemas.android.com/apk/res-auto<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>tools</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://schemas.android.com/tools<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>orientation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>vertical<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>background</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@color/col_ffffff<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">tools:</span>context</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>.business.table.tack.ProcessTakeTableActivity<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ScrollView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/scrollview<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>LinearLayout</span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>orientation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>vertical<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>RelativeLayout</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/rlt_begin<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_marginLeft</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>110dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_marginTop</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>20dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>gravity</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>center_vertical<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>visibility</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>gone<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ImageView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/iv_begin<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>35dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>35dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@drawable/icon_process_time<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TextView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/tv_begin<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>35dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_marginLeft</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>10dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_toRightOf</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@id/iv_begin<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>gravity</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>center_vertical<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>minHeight</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>20dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>textColor</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@color/col_3f6db7<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>textSize</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@dimen/text_18sp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>text</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>流程开始<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>RelativeLayout</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>LinearLayout</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/ll_process_container<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>orientation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>vertical<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>RelativeLayout</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/rlt_end<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_marginLeft</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>110dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_marginBottom</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>20dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>visibility</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>gone<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>ImageView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/iv_end<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>35dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>35dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@drawable/icon_process_time<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TextView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/tv_end<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>35dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_marginLeft</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>10dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_toRightOf</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@id/iv_end<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>gravity</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>center_vertical<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>minHeight</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>20dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_alignBaseline</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@id/iv_end<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>textColor</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@color/col_3f6db7<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>textSize</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@dimen/text_18sp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>text</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>流程结束<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>RelativeLayout</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>LinearLayout</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>ScrollView</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>TextView</span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/tv_no_process_data<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>gravity</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>center<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>text</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@string/no_data<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>visibility</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>gone<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>LinearLayout</span><span class="token punctuation">></span></span></code></pre><p>其中rlt_begin为流程开始的布局,ll_process_container控件为动态添加流程的容器,alt_end为流程结束的布局。</p><p>Activity中的代码:</p><p>initProcessView();//此方法为动态添加流程子节点的方法</p><p>Activity具体代码如下:</p><pre class=" language-html"><code class="language-html"> @BindView(R.id.scrollview) ScrollView scrollView; /**无数据提示*/ @BindView(R.id.tv_no_process_data) TextView tvNoDate; /**审批流程容器*/@BindView(R.id.ll_process_container) LinearLayout llProcessContainer; @BindView(R.id.rlt_begin) RelativeLayout rltBegin; @BindView(R.id.rlt_end) RelativeLayout rltEnd; /**流程节点集合*/ private List<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>GetProcessTakeTableBean</span><span class="token punctuation">></span></span> processList=new ArrayList<>();private void initProcessView() { llProcessContainer.removeAllViews(); for(int i = 0; i <processList.size();i++){ LinearLayout linearLayout= (LinearLayout) LayoutInflater.from(this).inflate(R.layout.view_process_item,null); TextView tvTitle=linearLayout.findViewById(R.id.tv_title); TextView tvDate=linearLayout.findViewById(R.id.tv_date); TextView tvName=linearLayout.findViewById(R.id.tv_name); TextView tvAdvice=linearLayout.findViewById(R.id.tv_advice); TextView tvResult=linearLayout.findViewById(R.id.tv_result); TextView tvLineAfter=linearLayout.findViewById(R.id.line_after); if(StringUtil.isNotEmpty(processList.get(i).getNode_NAME())){ tvTitle.setText(processList.get(i).getNode_NAME()); } if(processList.get(i).getHandletime() != 0){ tvDate.setText(DateUtils.millis2String(processList.get(i).getHandletime(),"yyyy-MM-dd HH:mm")); } if(StringUtil.isNotEmpty(processList.get(i).getHandler_NAME())){ tvName.setText(processList.get(i).getHandler_NAME()); } if(StringUtil.isNotEmpty(processList.get(i).getHandle_ADVICE())){ tvAdvice.setText(processList.get(i).getHandle_ADVICE()); } if(StringUtil.isNotEmpty(processList.get(i).getHandle_RESULT())){ tvResult.setVisibility(View.VISIBLE); tvResult.setText(processList.get(i).getHandle_RESULT()); }else{ tvResult.setVisibility(View.GONE); } rltBegin.setVisibility(View.VISIBLE); int status=processList.get(i).getAudit_LEVEL(); switch (status){ case 0: case 1: case 2: rltEnd.setVisibility(View.GONE); if(i == processList.size()-1){ tvLineAfter.setVisibility(View.GONE); } break; case 3: rltEnd.setVisibility(View.VISIBLE); break; default:break; } llProcessContainer.addView(linearLayout); }}</code></pre><p>上面的代码就是动态添加子节点数据的方法!当没有进程时,默认显示暂无相关数据的TextView,有数据时显示流程图!processList中存放的数据对象换成你自己的对象即可,亲测没有什么适配问题,如发现有bug,或有更好的方法请反馈于我。 </p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Android App启动时网络安全配置错误</title>
<link href="http://yoursite.com/2020/09/26/Android%20App%E5%90%AF%E5%8A%A8%E6%97%B6%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8%E9%85%8D%E7%BD%AE%E9%94%99%E8%AF%AF/"/>
<id>http://yoursite.com/2020/09/26/Android%20App%E5%90%AF%E5%8A%A8%E6%97%B6%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8%E9%85%8D%E7%BD%AE%E9%94%99%E8%AF%AF/</id>
<published>2020-09-26T10:31:07.000Z</published>
<updated>2020-10-02T09:30:37.469Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="Android-App启动时网络安全配置错误"><a href="#Android-App启动时网络安全配置错误" class="headerlink" title="Android App启动时网络安全配置错误"></a>Android App启动时网络安全配置错误</h1><p><a href="https://www.pimspeak.com/android-app-network-config.html" target="_blank" rel="noopener">2020-07-22</a> by weierxiao</p><p>Android App启动时报错,错误信息</p><pre><code>No Network Security Config specified, using platform default</code></pre><p>意思是说没有指定网络安全设配置,使用平台默认的。</p><p>解决步骤:</p><ol><li><p>编辑AndroidManifest, 在<code>application</code>中添加:</p><pre class=" language-java"><code class="language-java">android<span class="token operator">:</span>usesCleartextTraffic<span class="token operator">=</span><span class="token string">"true"</span>android<span class="token operator">:</span>networkSecurityConfig<span class="token operator">=</span><span class="token string">"@xml/network_security_config"</span></code></pre><p>再加上</p><pre class=" language-java"><code class="language-java"><span class="token operator"><</span>uses<span class="token operator">-</span>library android<span class="token operator">:</span>name<span class="token operator">=</span><span class="token string">"org.apache.http.legacy"</span> android<span class="token operator">:</span>required<span class="token operator">=</span><span class="token string">"false"</span><span class="token operator">/</span><span class="token operator">></span></code></pre></li><li><p>在xml目录中新建<code>network_security_config.xml</code>,内容如下:</p><pre class=" language-xml"><code class="language-xml"><span class="token prolog"><?xml version="1.0" encoding="utf-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>network-security-config</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>base-config</span> <span class="token attr-name">cleartextTrafficPermitted</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>true<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>network-security-config</span><span class="token punctuation">></span></span></code></pre></li></ol><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Fragment实例精讲——底部导航栏+ViewPager滑动切换页面</title>
<link href="http://yoursite.com/2020/09/26/Fragment%E5%AE%9E%E4%BE%8B%E7%B2%BE%E8%AE%B2%E2%80%94%E2%80%94%E5%BA%95%E9%83%A8%E5%AF%BC%E8%88%AA%E6%A0%8F+ViewPager%E6%BB%91%E5%8A%A8%E5%88%87%E6%8D%A2%E9%A1%B5%E9%9D%A2/"/>
<id>http://yoursite.com/2020/09/26/Fragment%E5%AE%9E%E4%BE%8B%E7%B2%BE%E8%AE%B2%E2%80%94%E2%80%94%E5%BA%95%E9%83%A8%E5%AF%BC%E8%88%AA%E6%A0%8F+ViewPager%E6%BB%91%E5%8A%A8%E5%88%87%E6%8D%A2%E9%A1%B5%E9%9D%A2/</id>
<published>2020-09-26T08:31:07.000Z</published>
<updated>2020-10-02T09:19:13.808Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p><a href="https://www.runoob.com/w3cnote/android-tutorial-fragment-demo4.html" target="_blank" rel="noopener">https://www.runoob.com/w3cnote/android-tutorial-fragment-demo4.html</a></p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>STM32_Debug 使用ST-Link进行调试出现Error:Flash Download Failed-"Cortex-M3" 解决方案</title>
<link href="http://yoursite.com/2020/09/25/STM32_Debug%20%E4%BD%BF%E7%94%A8ST-Link%E8%BF%9B%E8%A1%8C%E8%B0%83%E8%AF%95%E5%87%BA%E7%8E%B0ErrorFlash%20Down%20%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/"/>
<id>http://yoursite.com/2020/09/25/STM32_Debug%20%E4%BD%BF%E7%94%A8ST-Link%E8%BF%9B%E8%A1%8C%E8%B0%83%E8%AF%95%E5%87%BA%E7%8E%B0ErrorFlash%20Down%20%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/</id>
<published>2020-09-25T10:31:07.000Z</published>
<updated>2020-10-02T09:38:12.914Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>在Debug窗口依次注意以下几个点:</p><p><strong>1.选择与主控芯片相配套的芯片</strong><br><img src="https://img-blog.csdn.net/20180908094245359?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1paWGluXw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="img"><br><strong>2.选择ST-Link Debugger—-此处注意该页面最下面一行可不更改(亲测有效)</strong><br><img src="https://img-blog.csdn.net/20180908094334973?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1paWGluXw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述"><br><strong>3.接上图点击进入Setting,依次选择</strong><br><img src="https://img-blog.csdn.net/2018090809441162?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1paWGluXw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述"><br><strong>4.进入Flash Download 注意同样选中与主控芯片相配套的flash类型,出现Error:Flash Download Failed-“Cortex-M3” 问题,多由于该处没有选择正确</strong><br><img src="https://img-blog.csdn.net/20180908100245830?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1paWGluXw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述"><br><img src="https://img-blog.csdn.net/20180908094437559?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1paWGluXw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述"><br>注:此处本人也存在疑惑,若为“Med-density”与芯片选型配套,在此基础上再进行类型选择时,就会出现以上界面,没有了“Med-density”这一类型;若再次更换芯片选型,“Med-density”这一类型又会重新出现,不知这次什么“bug”,还望大神如若看到,能解决疑惑。<br><strong>5.再进入Utilities设置(多数人会忘记),依次进行设置</strong><br><img src="https://img-blog.csdn.net/20180908094446930?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1paWGluXw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述"><br><strong>6.Well Done</strong><br><img src="https://img-blog.csdn.net/20180908094456376?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1paWGluXw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="这里写图片描述"></p><p>最后说明:这个问题,一而再再而三出现,一次次解决,一次次又忘记具体操作细节,特地记下来,作为参考。</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>消息队列RabbitMQ的python接口使用</title>
<link href="http://yoursite.com/2020/09/25/%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97RabbitMQ%E7%9A%84python%E6%8E%A5%E5%8F%A3%E4%BD%BF%E7%94%A8/"/>
<id>http://yoursite.com/2020/09/25/%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97RabbitMQ%E7%9A%84python%E6%8E%A5%E5%8F%A3%E4%BD%BF%E7%94%A8/</id>
<published>2020-09-25T10:31:07.000Z</published>
<updated>2020-10-02T09:28:54.374Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="消息队列RabbitMQ的python接口使用"><a href="#消息队列RabbitMQ的python接口使用" class="headerlink" title="消息队列RabbitMQ的python接口使用"></a><a href="https://www.cnblogs.com/qxcheng/p/12334880.html" target="_blank" rel="noopener">消息队列RabbitMQ的python接口使用</a></h1><p>消息队列是一种常用的开发中间件,适用于异步、分布式、解耦合等业务场景中,而RabbitMQ是其中一种常用的消息队列,今天来总结一下RabbitMQ在python端的使用方法。</p><h1 id="1-发送接收基础"><a href="#1-发送接收基础" class="headerlink" title="1.发送接收基础"></a>1.发送接收基础</h1><p>python一般使用pika库来操作RabbitMQ,需要先用pip安装。</p><pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true">#1 建立连接</span>connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span><span class="token string">'localhost'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 建立一个连接</span>channel <span class="token operator">=</span> connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 建立此连接下的一个频道</span>channel<span class="token punctuation">.</span>queue_declare<span class="token punctuation">(</span>queue<span class="token operator">=</span><span class="token string">'hello'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 声明一个队列</span><span class="token comment" spellcheck="true">#2 发送消息</span>channel<span class="token punctuation">.</span>basic_publish<span class="token punctuation">(</span>exchange<span class="token operator">=</span><span class="token string">''</span><span class="token punctuation">,</span> routing_key<span class="token operator">=</span><span class="token string">'hello'</span><span class="token punctuation">,</span> body<span class="token operator">=</span><span class="token string">'Hello World!'</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">#3 接收消息</span>channel<span class="token punctuation">.</span>basic_consume<span class="token punctuation">(</span>on_message_callback<span class="token operator">=</span>callback<span class="token punctuation">,</span> queue<span class="token operator">=</span><span class="token string">'hello'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 声明消息处理回调函数</span>channel<span class="token punctuation">.</span>start_consuming<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 开始消费消息,并进入死循环</span><span class="token keyword">def</span> <span class="token function">callback</span><span class="token punctuation">(</span>channel<span class="token punctuation">,</span> method<span class="token punctuation">,</span> properties<span class="token punctuation">,</span> body<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Received %r"</span> <span class="token operator">%</span> <span class="token punctuation">(</span>body<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># body是消息体,根据具体业务进行解析处理</span> time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 模拟处理消息</span> channel<span class="token punctuation">.</span>basic_ack<span class="token punctuation">(</span>delivery_tag <span class="token operator">=</span> method<span class="token punctuation">.</span>delivery_tag<span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 处理完成后,发送ack进行消息确认,消息在服务端安全删除</span><span class="token comment" spellcheck="true">#4 关闭连接</span>connection<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span></code></pre><h1 id="2-完整接口示例"><a href="#2-完整接口示例" class="headerlink" title="2.完整接口示例"></a>2.完整接口示例</h1><pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> time<span class="token keyword">import</span> random<span class="token keyword">import</span> pika<span class="token keyword">from</span> pika<span class="token punctuation">.</span>exceptions <span class="token keyword">import</span> ChannelClosed<span class="token punctuation">,</span> ConnectionClosed<span class="token comment" spellcheck="true"># rabbitmq 配置信息</span>MQ_CONFIG <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">"hostname"</span><span class="token punctuation">:</span> <span class="token string">"127.0.0.1"</span><span class="token punctuation">,</span> <span class="token string">"port"</span><span class="token punctuation">:</span> <span class="token number">8080</span><span class="token punctuation">,</span> <span class="token string">"vhost"</span><span class="token punctuation">:</span> <span class="token string">"/"</span><span class="token punctuation">,</span> <span class="token string">"username"</span><span class="token punctuation">:</span> <span class="token string">"guest"</span><span class="token punctuation">,</span> <span class="token string">"password"</span><span class="token punctuation">:</span> <span class="token string">"guest"</span><span class="token punctuation">,</span> <span class="token string">"exchange"</span><span class="token punctuation">:</span> <span class="token string">"exchange"</span><span class="token punctuation">,</span> <span class="token string">"queue"</span><span class="token punctuation">:</span> <span class="token string">"queue"</span><span class="token punctuation">,</span> <span class="token string">"routing_key"</span><span class="token punctuation">:</span> <span class="token string">"key"</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true"># 消息队列基类</span><span class="token keyword">class</span> <span class="token class-name">RabbitMQServer</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>config <span class="token operator">=</span> MQ_CONFIG <span class="token comment" spellcheck="true"># 配置文件加载</span> self<span class="token punctuation">.</span>host <span class="token operator">=</span> self<span class="token punctuation">.</span>config<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"hostname"</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 主机</span> self<span class="token punctuation">.</span>port <span class="token operator">=</span> self<span class="token punctuation">.</span>config<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"port"</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 端口</span> self<span class="token punctuation">.</span>username <span class="token operator">=</span> self<span class="token punctuation">.</span>config<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"username"</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 用户名</span> self<span class="token punctuation">.</span>password <span class="token operator">=</span> self<span class="token punctuation">.</span>config<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"password"</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 密码</span> self<span class="token punctuation">.</span>vhost <span class="token operator">=</span> self<span class="token punctuation">.</span>config<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"vhost"</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 虚拟主机,VirtualHost之间相互隔离</span> self<span class="token punctuation">.</span>exchange <span class="token operator">=</span> self<span class="token punctuation">.</span>config<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"exchange"</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 交换机</span> self<span class="token punctuation">.</span>queue <span class="token operator">=</span> self<span class="token punctuation">.</span>config<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"queue"</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 队列</span> self<span class="token punctuation">.</span>routing_key <span class="token operator">=</span> self<span class="token punctuation">.</span>config<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">"routing_key"</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 交换机和队列的绑定</span> self<span class="token punctuation">.</span>connection <span class="token operator">=</span> None self<span class="token punctuation">.</span>channel <span class="token operator">=</span> None <span class="token keyword">def</span> <span class="token function">reconnect</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> heartbeat<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">try</span><span class="token punctuation">:</span> <span class="token comment" spellcheck="true"># 关闭旧的连接</span> <span class="token keyword">if</span> self<span class="token punctuation">.</span>connection <span class="token operator">and</span> <span class="token operator">not</span> self<span class="token punctuation">.</span>connection<span class="token punctuation">.</span>is_closed<span class="token punctuation">:</span> self<span class="token punctuation">.</span>connection<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 构造登录参数</span> credentials <span class="token operator">=</span> pika<span class="token punctuation">.</span>PlainCredentials<span class="token punctuation">(</span>self<span class="token punctuation">.</span>username<span class="token punctuation">,</span> self<span class="token punctuation">.</span>password<span class="token punctuation">)</span> parameters <span class="token operator">=</span> pika<span class="token punctuation">.</span>ConnectionParameters<span class="token punctuation">(</span>self<span class="token punctuation">.</span>host<span class="token punctuation">,</span> self<span class="token punctuation">.</span>port<span class="token punctuation">,</span> self<span class="token punctuation">.</span>vhost<span class="token punctuation">,</span> credentials<span class="token punctuation">)</span> self<span class="token punctuation">.</span>connection <span class="token operator">=</span> pika<span class="token punctuation">.</span>BlockingConnection<span class="token punctuation">(</span>parameters<span class="token punctuation">)</span> self<span class="token punctuation">.</span>channel <span class="token operator">=</span> self<span class="token punctuation">.</span>connection<span class="token punctuation">.</span>channel<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 声明交换机</span> self<span class="token punctuation">.</span>channel<span class="token punctuation">.</span>exchange_declare<span class="token punctuation">(</span>exchange<span class="token operator">=</span>self<span class="token punctuation">.</span>exchange<span class="token punctuation">,</span> exchange_type<span class="token operator">=</span><span class="token string">"direct"</span><span class="token punctuation">,</span> durable<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 消费者实例</span> <span class="token keyword">if</span> isinstance<span class="token punctuation">(</span>self<span class="token punctuation">,</span> RabbitComsumer<span class="token punctuation">)</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>channel<span class="token punctuation">.</span>queue_declare<span class="token punctuation">(</span>queue<span class="token operator">=</span>self<span class="token punctuation">.</span>queue<span class="token punctuation">,</span> exclusive<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> durable<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 绑定队列</span> self<span class="token punctuation">.</span>channel<span class="token punctuation">.</span>queue_bind<span class="token punctuation">(</span>exchange<span class="token operator">=</span>self<span class="token punctuation">.</span>exchange<span class="token punctuation">,</span> queue<span class="token operator">=</span>self<span class="token punctuation">.</span>queue<span class="token punctuation">,</span> routing_key<span class="token operator">=</span>self<span class="token punctuation">.</span>routing_key<span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 表明最大阻塞未ack的消息数量</span> self<span class="token punctuation">.</span>channel<span class="token punctuation">.</span>basic_qos<span class="token punctuation">(</span>prefetch_count<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span> self<span class="token punctuation">.</span>channel<span class="token punctuation">.</span>basic_consume<span class="token punctuation">(</span>on_message_callback<span class="token operator">=</span>self<span class="token punctuation">.</span>consumer_callback<span class="token punctuation">,</span> queue<span class="token operator">=</span>self<span class="token punctuation">.</span>queue<span class="token punctuation">,</span> auto_ack<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 生产者实例</span> <span class="token keyword">elif</span> isinstance<span class="token punctuation">(</span>self<span class="token punctuation">,</span> RabbitPublisher<span class="token punctuation">)</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>channel<span class="token punctuation">.</span>queue_declare<span class="token punctuation">(</span>queue<span class="token operator">=</span>self<span class="token punctuation">.</span>queue<span class="token punctuation">,</span> exclusive<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span> durable<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token keyword">def</span> <span class="token function">excecute</span><span class="token punctuation">(</span>body<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">pass</span><span class="token comment" spellcheck="true"># 消费者</span><span class="token keyword">class</span> <span class="token class-name">RabbitComsumer</span><span class="token punctuation">(</span>RabbitMQServer<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> super<span class="token punctuation">(</span>RabbitComsumer<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">def</span> <span class="token function">consumer_callback</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> channel<span class="token punctuation">,</span> method<span class="token punctuation">,</span> properties<span class="token punctuation">,</span> body<span class="token punctuation">)</span><span class="token punctuation">:</span> result <span class="token operator">=</span> execute<span class="token punctuation">(</span>body<span class="token punctuation">)</span> <span class="token comment" spellcheck="true"># 模拟处理消息</span> <span class="token keyword">if</span> channel<span class="token punctuation">.</span>is_open<span class="token punctuation">:</span> <span class="token keyword">if</span> result<span class="token punctuation">:</span> channel<span class="token punctuation">.</span>basic_ack<span class="token punctuation">(</span>delivery_tag<span class="token operator">=</span>method<span class="token punctuation">.</span>delivery_tag<span class="token punctuation">)</span> <span class="token keyword">else</span><span class="token punctuation">:</span> <span class="token comment" spellcheck="true"># 处理不成功时,发送no_ack</span> channel<span class="token punctuation">.</span>basic_nack<span class="token punctuation">(</span>delivery_tag<span class="token operator">=</span>method<span class="token punctuation">.</span>delivery_tag<span class="token punctuation">,</span> multiple<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span> requeue<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token keyword">if</span> <span class="token operator">not</span> channel<span class="token punctuation">.</span>is_open<span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Callback 接收频道关闭,无法ack"</span><span class="token punctuation">)</span> <span class="token keyword">def</span> <span class="token function">start_consumer</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span> <span class="token keyword">try</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>reconnect<span class="token punctuation">(</span><span class="token punctuation">)</span> self<span class="token punctuation">.</span>channel<span class="token punctuation">.</span>start_consuming<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">except</span> ConnectionClosed <span class="token keyword">as</span> e<span class="token punctuation">:</span> <span class="token comment" spellcheck="true"># 保证连接断开重连</span> self<span class="token punctuation">.</span>reconnect<span class="token punctuation">(</span><span class="token punctuation">)</span> time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">except</span> ChannelClosed <span class="token keyword">as</span> e<span class="token punctuation">:</span> <span class="token comment" spellcheck="true"># 保证连接断开重连</span> self<span class="token punctuation">.</span>reconnect<span class="token punctuation">(</span><span class="token punctuation">)</span> time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span> self<span class="token punctuation">.</span>reconnect<span class="token punctuation">(</span><span class="token punctuation">)</span> time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true"># 生产者</span><span class="token keyword">class</span> <span class="token class-name">RabbitPublisher</span><span class="token punctuation">(</span>RabbitMQServer<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> super<span class="token punctuation">(</span>RabbitPublisher<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">def</span> <span class="token function">start_publish</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>reconnect<span class="token punctuation">(</span><span class="token punctuation">)</span> i <span class="token operator">=</span> <span class="token number">1</span> <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span> message <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"value"</span><span class="token punctuation">:</span> i<span class="token punctuation">}</span> <span class="token keyword">try</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>channel<span class="token punctuation">.</span>basic_publish<span class="token punctuation">(</span>exchange<span class="token operator">=</span>self<span class="token punctuation">.</span>exchange<span class="token punctuation">,</span> routing_key<span class="token operator">=</span>self<span class="token punctuation">.</span>routing_key<span class="token punctuation">,</span> body<span class="token operator">=</span>message<span class="token punctuation">)</span> i <span class="token operator">+=</span> <span class="token number">1</span> time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">except</span> ConnectionClosed <span class="token keyword">as</span> e<span class="token punctuation">:</span> self<span class="token punctuation">.</span>reconnect<span class="token punctuation">(</span><span class="token punctuation">)</span> time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">except</span> ChannelClosed <span class="token keyword">as</span> e<span class="token punctuation">:</span> self<span class="token punctuation">.</span>reconnect<span class="token punctuation">(</span><span class="token punctuation">)</span> time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span> self<span class="token punctuation">.</span>reconnect<span class="token punctuation">(</span><span class="token punctuation">)</span> time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span></code></pre><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>MVC,MVP 和 MVVM 的图示</title>
<link href="http://yoursite.com/2020/09/25/MVC%EF%BC%8CMVP%20%E5%92%8C%20MVVM%20%E7%9A%84%E5%9B%BE%E7%A4%BA/"/>
<id>http://yoursite.com/2020/09/25/MVC%EF%BC%8CMVP%20%E5%92%8C%20MVVM%20%E7%9A%84%E5%9B%BE%E7%A4%BA/</id>
<published>2020-09-25T08:31:07.000Z</published>
<updated>2020-10-02T09:18:36.474Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>复杂的软件必须有清晰合理的架构,否则无法开发和维护。</p><p><a href="http://zh.wikipedia.org/wiki/MVC" target="_blank" rel="noopener">MVC</a>(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用。它本身<a href="http://www.ruanyifeng.com/blog/2007/11/mvc.html" target="_blank" rel="noopener">很容易理解</a>,但是要讲清楚,它与衍生的 MVP 和 MVVM 架构的区别就不容易了。</p><p>昨天晚上,我读了<a href="http://blog.nodejitsu.com/scaling-isomorphic-javascript-code/" target="_blank" rel="noopener">《Scaling Isomorphic Javascript Code》</a>,突然意识到,它们的区别非常简单。我用几段话,就可以说清。</p><p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015020102.jpg" alt="img"></p><p>(题图:摄于瓦伦西亚,西班牙,2014年8月)</p><h2 id="一、MVC"><a href="#一、MVC" class="headerlink" title="一、MVC"></a>一、MVC</h2><p>MVC模式的意思是,软件可以分成三个部分。</p><p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015020104.png" alt="img"></p><blockquote><ul><li>视图(View):用户界面。</li><li>控制器(Controller):业务逻辑</li><li>模型(Model):数据保存</li></ul></blockquote><p>各部分之间的通信方式如下。</p><p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015020105.png" alt="img"></p><blockquote><ol><li>View 传送指令到 Controller</li><li>Controller 完成业务逻辑后,要求 Model 改变状态</li><li>Model 将新的数据发送到 View,用户得到反馈</li></ol></blockquote><p>所有通信都是单向的。</p><h2 id="二、互动模式"><a href="#二、互动模式" class="headerlink" title="二、互动模式"></a>二、互动模式</h2><p>接受用户指令时,MVC 可以分成两种方式。一种是通过 View 接受指令,传递给 Controller。</p><p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015020106.png" alt="img"></p><p>另一种是直接通过controller接受指令。</p><p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015020107.png" alt="img"></p><h2 id="三、实例:Backbone"><a href="#三、实例:Backbone" class="headerlink" title="三、实例:Backbone"></a>三、实例:Backbone</h2><p>实际项目往往采用更灵活的方式,以 <a href="http://documentcloud.github.com/backbone" target="_blank" rel="noopener">Backbone.js</a> 为例。</p><p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015020108.png" alt="img"></p><p>\1. 用户可以向 View 发送指令(DOM 事件),再由 View 直接要求 Model 改变状态。</p><p>\2. 用户也可以直接向 Controller 发送指令(改变 URL 触发 hashChange 事件),再由 Controller 发送给 View。</p><p>\3. Controller 非常薄,只起到路由的作用,而 View 非常厚,业务逻辑都部署在 View。所以,Backbone 索性取消了 Controller,只保留一个 Router(路由器) 。</p><h2 id="四、MVP"><a href="#四、MVP" class="headerlink" title="四、MVP"></a>四、MVP</h2><p>MVP 模式将 Controller 改名为 Presenter,同时改变了通信方向。</p><p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015020109.png" alt="img"></p><p>\1. 各部分之间的通信,都是双向的。</p><p>\2. View 与 Model 不发生联系,都通过 Presenter 传递。</p><p>\3. View 非常薄,不部署任何业务逻辑,称为”被动视图”(Passive View),即没有任何主动性,而 Presenter非常厚,所有逻辑都部署在那里。</p><h2 id="五、MVVM"><a href="#五、MVVM" class="headerlink" title="五、MVVM"></a>五、MVVM</h2><p>MVVM 模式将 Presenter 改名为 ViewModel,基本上与 MVP 模式完全一致。</p><p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015020110.png" alt="img"></p><p>唯一的区别是,它采用双向绑定(data-binding):View的变动,自动反映在 ViewModel,反之亦然。<a href="https://angularjs.org/" target="_blank" rel="noopener">Angular</a> 和 <a href="http://emberjs.com/" target="_blank" rel="noopener">Ember</a> 都采用这种模式。</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>RabbitMq使用中常见错误小结</title>
<link href="http://yoursite.com/2020/09/24/RabbitMq%E4%BD%BF%E7%94%A8%E4%B8%AD%E5%B8%B8%E8%A7%81%E9%94%99%E8%AF%AF%E5%B0%8F%E7%BB%93/"/>
<id>http://yoursite.com/2020/09/24/RabbitMq%E4%BD%BF%E7%94%A8%E4%B8%AD%E5%B8%B8%E8%A7%81%E9%94%99%E8%AF%AF%E5%B0%8F%E7%BB%93/</id>
<published>2020-09-24T10:31:07.000Z</published>
<updated>2020-10-02T09:27:57.206Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h4 id="环境"><a href="#环境" class="headerlink" title="环境"></a>环境</h4><hr><p>分别使用了三个库使用RabbitMq。</p><ol><li>使用python的pika库,适用于python。</li><li>使用rabbitmq-c库,-lrabbitmq,适用于c/c++语言。</li><li>使用AMQP-CPP库,-lamqpcpp,适用于c++。</li></ol><p>主要对使用过程中出现的调试错误进行小结,适合新手参考。</p><h4 id="pika库"><a href="#pika库" class="headerlink" title="pika库"></a>pika库</h4><hr><ol><li>pika.exceptions.ProbableAuthenticationError: ConnectionClosedByBroker: (403) ‘ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.’</li></ol><ul><li>登录信息不全,需要使用用户名和密码登录。或者用户名、密码不正确。</li></ul><p>如,<code>connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.1.2'))</code>,未使用用户名和密码等登录参数。可以参考下面的代码片段:</p><pre class=" language-py"><code class="language-py">#!/usr/bin/env pythonimport pikacredentials = pika.PlainCredentials('the_user', 'the_pass')parameters = pika.ConnectionParameters('132.45.23.14', 5672, '/', credentials)connection = pika.BlockingConnection(parameters)12345678910</code></pre><ol><li>pika.exceptions.ProbableAccessDeniedError: StreamLostError: (“Stream connection lost: ConnectionResetError(10054, ‘远程主机强迫关闭了一个现有的连接。’, None, 10054, None)”,)</li></ol><ul><li>服务未对该用户开放相关的权限,如 Virtual Host权限。需要登录服务端开放开头权限,以 Virtual Host权限为例:<ul><li>启动web管理服务:在安装目录执行:<code>rabbitmq-plugins enable rabbitmq_management</code></li><li>在浏览器中输入 <code>http://127.0.0.1:15672</code>,使用默认的用户名和密码登录:‘guest’, ‘guest’</li><li>增加一个新用户</li><li>点击用户名,点击开通权限即可</li></ul></li></ul><ol><li>pika.adapters.utils.connection_workflow.AMQPConnectorStackTimeout: Timeout during AMQP handshake’192.168.1.33’/(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, ‘’, (‘192.168.1.33’, 15672)); ssl=False</li></ol><ul><li>超时错误,可能是填写的端口号不正确</li></ul><ol><li>pika.exceptions.ChannelClosedByBroker: (406, “PRECONDITION_FAILED - inequivalent arg ‘durable’ for exchange ‘CLP_STG’ in vhost ‘/’: received ‘false’ but current is ‘true’”)</li></ol><ul><li>创建Exchange时,该Exchange已经存在,且本次创建时使用的参数与之前不符。具体到本异常,为</li></ul><pre><code> durable</code></pre><p> 属性设置的值不同。</p><ul><li><code>durable</code>属性默认为false,无消息持久化特性</li><li>创建Exchange时使用 <code>durable=True</code>设置为true,使用消息持久化特性</li></ul><h4 id="rabbitmq-c库"><a href="#rabbitmq-c库" class="headerlink" title="rabbitmq-c库"></a>rabbitmq-c库</h4><hr><ol><li>open channel error: server channel error 406h, message: PRECONDITION_FAILED - unknown delivery tag 1</li></ol><ul><li>消费者关闭了默认的ack机制,但仍然发送了ack,或者使用了ack,但发送ack时未使用与接收消息相同的通道</li></ul><ol><li>调用 amqp_channel_open 函数后,在管理界面查看通道已经打开,但该函数阻塞,一直未返回</li></ol><ul><li>可能是在多线程中使用了同一个连接,或者多线程使用了同一个通道。在我的测试中,多线程使用同一个连接时,即使通道不同,也会出现该问题</li><li>详情可以参考 <a href="http://cncc.bingj.com/cache.aspx?q=rabbitmq+c+client+amqp_channel_open+not+return&d=4617231636433151&mkt=en-US&setlang=en-US&w=pPf1fabPMvgzrOgPI09-_3KISbsg8Bsr" target="_blank" rel="noopener">amqp_channel_open issue</a></li></ul><h4 id="AMQP-CPP库"><a href="#AMQP-CPP库" class="headerlink" title="AMQP-CPP库"></a>AMQP-CPP库</h4><hr><p>使用libevent事件驱动库。</p><ol><li>执行了一系列创建工作,但在管理后台上查看时并未创建。</li></ol><ul><li>所有的指令会在事件循环启动后执行,所以需要启动 <code>event_base_dispatch(evbase);</code>循环</li><li>当持续有需要监听的事件时,该循环不会退出,所以不要在主进程中启动它,在线程中启动</li></ul><ol><li>如何注册事件回调函数</li></ol><ul><li>所有的操作都是异步的,所以执行某指令后返回的值并不能判断该指令执行成功,需要注册回调函数,以及时处理错误</li><li>AMQP-CPP库内部使用的是类似 <code>std::function<void(void)></code>形式的回调函数,在传入时使用 <code>std::bind</code>即可,参数使用占位符表示,如:</li></ul><pre class=" language-cpp"><code class="language-cpp">m_channel<span class="token operator">-</span><span class="token operator">></span><span class="token function">publish</span><span class="token punctuation">(</span>exchangeName<span class="token punctuation">,</span> routingKey<span class="token punctuation">,</span> msg<span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">onError</span><span class="token punctuation">(</span>std<span class="token operator">::</span><span class="token function">bind</span><span class="token punctuation">(</span><span class="token operator">&</span>Foo<span class="token operator">::</span>PublishMsgErrCb<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">,</span> std<span class="token operator">::</span>placeholders<span class="token operator">::</span>_1<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token number">12</span></code></pre><ol><li>报错:UNEXPECTED_FRAME - expected content header for class 60, got non content header frame instead</li></ol><ul><li>在功能测试通过的情况下,在压力测试下出现这个问题</li><li>经过调试,是多个线程使用了同一个通道造成的问题。在RabbitMq中,connection是线程安全的,但channel不能多个线程共用一个。</li><li>为不同的线程开启了不同的channel,问题解决</li></ul><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>ViewModel的使用</title>
<link href="http://yoursite.com/2020/09/24/ViewModel%E7%9A%84%E4%BD%BF%E7%94%A8/"/>
<id>http://yoursite.com/2020/09/24/ViewModel%E7%9A%84%E4%BD%BF%E7%94%A8/</id>
<published>2020-09-24T08:31:07.000Z</published>
<updated>2020-10-02T09:17:57.221Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="一、前言:"><a href="#一、前言:" class="headerlink" title="一、前言:"></a>一、前言:</h2><p>在页面(Activity/Fragment)很简单的情况下,通常我们会将UI交互,数据获取与处理等相关业务逻辑,全部写在页面中,但是在页面复杂的情况下,这样做是不合适的,它不符合<img src="https://math.jianshu.com/math?formula=%5Ccolor%7Bred%7D%7B%E2%80%9C%E5%8D%95%E4%B8%80%E8%B4%A3%E4%BB%BB%E2%80%9D%7D" alt="\color{red}{“单一责任”}">原则。页面只应该负责接收用户的交互,以及将数据展示到屏幕上,相关数据应该单独存放和处理。</p><p>为此,Android为我们提供了<img src="https://math.jianshu.com/math?formula=%5Ccolor%7Bred%7D%7BViewModel%7D" alt="\color{red}{ViewModel}">类,专门用于存放应用程序页面所需的数据。它将页面所需的数据从页面中剥离出来,页面只需要处理用户交互,以及负责展示数据的工作。</p><p><img src="https:////upload-images.jianshu.io/upload_images/11268516-09c54b7bdd39022d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/704/format/webp" alt="img"></p><p>图片.png</p><p>另外,如果我们的应用程序支持横竖屏切换,当用户旋转手机屏幕时,我们还需要考虑数据的存储与恢复。如果数据不进行存储,那么通常我们还需要重新去获取一次。</p><p>而ViewModel能为我们解决这个问题,它独立于配置变化。也就是说,屏幕旋转导致的Activity重建,并不会影响到ViewModel的生命周期。</p><blockquote><p>ViewModel这个名字可以这样理解:它是介于View(视图)和Model(模型数据)之间的这样一个东西,它起到了桥梁的作用,使得视图和数据既能够分离开,也能够保持通信。</p></blockquote><p><img src="https:////upload-images.jianshu.io/upload_images/11268516-93846692f30a88ae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/522/format/webp" alt="img"></p><p>Activity旋转重建与ViewModel生命周期之间的关系.png</p><blockquote><p>注意: ViewModel只是用来管理UI的数据的,千万不要让它持有View、Activity或者Fragment的引用(小心内存泄露)。</p></blockquote><h2 id="二、使用:"><a href="#二、使用:" class="headerlink" title="二、使用:"></a>二、使用:</h2><h4 id="1-在应用程序的build-gradle中加入依赖。"><a href="#1-在应用程序的build-gradle中加入依赖。" class="headerlink" title="1.在应用程序的build.gradle中加入依赖。"></a>1.在应用程序的build.gradle中加入依赖。</h4><pre class=" language-csharp"><code class="language-csharp"><span class="token comment" spellcheck="true">//viewMouble使用</span>implementation <span class="token string">'androidx.lifecycle:lifecycle-extensions:2.2.0'</span></code></pre><h4 id="2-写一个继承自ViewModel的类。"><a href="#2-写一个继承自ViewModel的类。" class="headerlink" title="2.写一个继承自ViewModel的类。"></a>2.写一个继承自ViewModel的类。</h4><pre class=" language-dart"><code class="language-dart">public <span class="token keyword">class</span> <span class="token class-name">TimerViewModel</span> <span class="token keyword">extends</span> <span class="token class-name">ViewModel</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/** * 由于屏幕旋转导致的Activity重建,该方法不会被调用 * * 只有ViewModel已经没有任何Activity与之有关联,系统则会调用该方法,你可以在此清理资源 * */</span> <span class="token metadata symbol">@Override</span> protected <span class="token keyword">void</span> <span class="token function">onCleared</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">onCleared</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>ViewModel是一个抽象类,其中只有一个方法onCleared(),当ViewModel不再被需要的时候,也就是与之相关的Activity都被销毁时,该方法会被系统调用,我们可以在这个方法里面执行一些资源释放的操作,以免内存泄漏。</p><blockquote><p>注意:既然ViewModel的销毁是由系统来判断和执行的,那么系统是如何判断的呢?是根据Context引用。因此,我们在使用ViewModel的时候,千万不能从外面传入Activity,Fragment或者View之类的含有Context引用的东西,否则系统会认为该ViewModel还在使用中,从而无法被系统销毁回收,导致内存泄漏的发生。</p></blockquote><h4 id="3-在页面中使用ViewModel。"><a href="#3-在页面中使用ViewModel。" class="headerlink" title="3. 在页面中使用ViewModel。"></a>3. 在页面中使用ViewModel。</h4><pre class=" language-kotlin"><code class="language-kotlin">TimerViewModel timerViewModel <span class="token operator">=</span> ViewModelProviders<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>TimerViewModel<span class="token punctuation">.</span>class<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>ViewModel的实例化并不是通过普通的<strong>new</strong>来完成的,而是通过<strong>ViewModelProviders</strong>来完成。<strong>ViewModelProviders</strong>会去判断<strong>ViewModel</strong>是否存在,若存在则直接返回,否则它会去创建一个<strong>ViewModel</strong>。</p><h4 id="4-实例"><a href="#4-实例" class="headerlink" title="4. 实例"></a>4. 实例</h4><p>前面我们提到,ViewModel最重要的作用是将界面和数据分离,并且独立于Activity的重建。为了验证这一点,我们在ViewModel中创建一个Timer计时器,每隔一秒钟,通过接口<strong>OnTimeChangeListener</strong>通知它的调用者。</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TimerViewModel</span> <span class="token keyword">extends</span> <span class="token class-name">ViewModel</span><span class="token punctuation">{</span> <span class="token keyword">private</span> String TAG <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">private</span> Timer timer<span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token keyword">int</span> currentSecond<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/** * 开始计时 * */</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">startTiming</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>timer <span class="token operator">==</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> currentSecond <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> timer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Timer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> TimerTask timerTask <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TimerTask</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> currentSecond<span class="token operator">++</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>onTimeChangeListener <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span> onTimeChangeListener<span class="token punctuation">.</span><span class="token function">onTimeChanged</span><span class="token punctuation">(</span>currentSecond<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> timer<span class="token punctuation">.</span><span class="token function">schedule</span><span class="token punctuation">(</span>timerTask<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//延迟3秒执行</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 通过接口的方式,完成对调用者的通知,这种方式不是太好,更好的方式是通过LiveData组件来实现 * */</span> <span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">OnTimeChangeListener</span> <span class="token punctuation">{</span> <span class="token keyword">void</span> <span class="token function">onTimeChanged</span><span class="token punctuation">(</span><span class="token keyword">int</span> second<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> OnTimeChangeListener onTimeChangeListener<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setOnTimeChangeListener</span><span class="token punctuation">(</span>OnTimeChangeListener onTimeChangeListener<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>onTimeChangeListener <span class="token operator">=</span> onTimeChangeListener<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/** * 由于屏幕旋转导致的Activity重建,该方法不会被调用 * * 只有ViewModel已经没有任何Activity与之有关联,系统则会调用该方法,你可以在此清理资源 * */</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">onCleared</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">onCleared</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Log<span class="token punctuation">.</span><span class="token function">d</span><span class="token punctuation">(</span>TAG<span class="token punctuation">,</span> <span class="token string">"onCleared()"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> timer<span class="token punctuation">.</span><span class="token function">cancel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>接着,在TimerActivity中监听OnTimeChangeListener发来的通知,并据此更新UI界面。</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TimerActivity</span> <span class="token keyword">extends</span> <span class="token class-name">AppCompatActivity</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">onCreate</span><span class="token punctuation">(</span>Bundle savedInstanceState<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">onCreate</span><span class="token punctuation">(</span>savedInstanceState<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">setContentView</span><span class="token punctuation">(</span>R<span class="token punctuation">.</span>layout<span class="token punctuation">.</span>activity_timer<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">iniComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">iniComponent</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">final</span> TextView tvTime <span class="token operator">=</span> <span class="token function">findViewById</span><span class="token punctuation">(</span>R<span class="token punctuation">.</span>id<span class="token punctuation">.</span>tvTime<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//通过ViewModelProviders得到ViewModel,如果ViewModel不存在就创建一个新的,如果已经存在就直接返回已经存在的</span> TimerViewModel timerViewModel <span class="token operator">=</span> ViewModelProviders<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>TimerViewModel<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> timerViewModel<span class="token punctuation">.</span><span class="token function">setOnTimeChangeListener</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">TimerViewModel<span class="token punctuation">.</span>OnTimeChangeListener</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">onTimeChanged</span><span class="token punctuation">(</span><span class="token keyword">final</span> <span class="token keyword">int</span> second<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//更新UI界面</span> <span class="token function">runOnUiThread</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> tvTime<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token string">"TIME:"</span> <span class="token operator">+</span> second<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> timerViewModel<span class="token punctuation">.</span><span class="token function">startTiming</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>行程序并旋转屏幕。可以看到,旋转屏幕导致Activity重建时,计时器并没有停止,也就是说,横竖屏状态下Activity对应的ViewModel是同一个。</p><p><img src="https:////upload-images.jianshu.io/upload_images/11268516-04b89c69753c401d.gif?imageMogr2/auto-orient/strip%7CimageView2/2/w/320/format/webp" alt="img"></p><p>1111.gif</p><p>使用ViewModel,不仅将界面和数据从代码上进行了分离,而且不再需要关心屏幕旋转带来的数据的丢失和获取问题。也许你会说<a href="https://links.jianshu.com/go?to=https%3A%2F%2Flink.zhihu.com%2F%3Ftarget%3Dhttps%3A%2F%2Fdeveloper.android.com%2Freference%2Fandroid%2Fapp%2FActivity.html%23onSaveInstanceState%28android.os.Bundle%29" target="_blank" rel="noopener">onSaveInstanceState()</a> 方法同样可以解决屏幕旋转带来的数据丢失问题,但它只能保存少量的能支持序列化的数据,而ViewModel没有这个限制,它能支持页面中所有的数据。但要注意的是,ViewModel不支持数据的持久化,当界面彻底销毁,ViewModel及其数据也就不存在了。</p><p>我们前面提到过,使用ViewModel的时候,不能将任何含有Context引用的对象传入ViewModel,因为这可能会导致内存泄露。但如果你希望在ViewModel中使用Context怎么办呢?我们可以使用<strong>AndroidViewModel</strong>类,它继承自ViewModel,并且接收<strong>Application</strong>作为Context,既然是Application作为Context,也就意味着,我们能够明确它的生命周期和Application是一样的,这就不算是一个内存泄露了。</p><p>另外,在本示例中,我们通过自定义接口的方式(OnTimeChangeListener)来实现ViewModel到Activity的通信,这不是一种好的方法,实际上Android为我们提供了<strong>LiveData</strong>组件来解决这个问题。通过LiveData,当ViewModel中的数据发生变化时,Activity能自动收到通知,从而更新UI。我们在下一章节中继续讨论这个问题。</p><h2 id="三、其它使用:"><a href="#三、其它使用:" class="headerlink" title="三、其它使用:"></a>三、其它使用:</h2><h4 id="1-Activity与Fragment”通信”"><a href="#1-Activity与Fragment”通信”" class="headerlink" title="1. Activity与Fragment”通信”"></a>1. Activity与Fragment”通信”</h4><p>在同一个Activity中存在多个Fragment时,相互传递数据,一般通过定义接口来实现,Acticity从中协调。使用ViewModel能轻易解决Activity和Fragment之间的通信。</p><ul><li>Activity不需要做任何事情,不需要干涉这两个Fragment之间的通信。</li><li>Fragment不需要互相知道,即使一个消失不可见,另一个也能正常的工作。</li><li>Fragment有自己的生命周期,它们之间互不干扰,即便你用一个FragmentC替代了B,FragmentA也能正常工作,没有任何问题。</li></ul><p>有了ViewModel,Activity与Fragment可以共享一个ViewModel,因为Fragment是依附在Activity上的,在实例化ViewModel时将该Activity传入ViewModelProviders,它会给你一个该Activity已创建好了的ViewModel,这个Fragment可以方便的访问该ViewModel中的数据.在Activity中修改userModel数据后,该Fragment就能拿到更新后的数据.</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyFragment</span> <span class="token keyword">extends</span> <span class="token class-name">Fragment</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">onStart</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//这里拿到的ViewModel实例,其实是和Activity中创建的是一个实例</span> UserModel userModel <span class="token operator">=</span> ViewModelProviders<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token function">getActivity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>UserModel<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span></code></pre><h4 id="2-Fragment与Fragment”通信”"><a href="#2-Fragment与Fragment”通信”" class="headerlink" title="2. Fragment与Fragment”通信”"></a>2. Fragment与Fragment”通信”</h4><p>下面我们来看一个例子</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SharedViewModel</span> <span class="token keyword">extends</span> <span class="token class-name">ViewModel</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token keyword">final</span> MutableLiveData<span class="token operator"><</span>Item<span class="token operator">></span> selected <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MutableLiveData</span><span class="token operator"><</span>Item<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">select</span><span class="token punctuation">(</span>Item item<span class="token punctuation">)</span> <span class="token punctuation">{</span> selected<span class="token punctuation">.</span><span class="token function">setValue</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> LiveData<span class="token operator"><</span>Item<span class="token operator">></span> <span class="token function">getSelected</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> selected<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MasterFragment</span> <span class="token keyword">extends</span> <span class="token class-name">Fragment</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> SharedViewModel model<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">onCreate</span><span class="token punctuation">(</span>Bundle savedInstanceState<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">onCreate</span><span class="token punctuation">(</span>savedInstanceState<span class="token punctuation">)</span><span class="token punctuation">;</span> model <span class="token operator">=</span> ViewModelProviders<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token function">getActivity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>SharedViewModel<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> itemSelector<span class="token punctuation">.</span><span class="token function">setOnClickListener</span><span class="token punctuation">(</span>item <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span> model<span class="token punctuation">.</span><span class="token function">select</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DetailFragment</span> <span class="token keyword">extends</span> <span class="token class-name">Fragment</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">onCreate</span><span class="token punctuation">(</span>Bundle savedInstanceState<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">onCreate</span><span class="token punctuation">(</span>savedInstanceState<span class="token punctuation">)</span><span class="token punctuation">;</span> SharedViewModel model <span class="token operator">=</span> ViewModelProviders<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token function">getActivity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>SharedViewModel<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> model<span class="token punctuation">.</span><span class="token function">getSelected</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">observe</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> item <span class="token operator">-</span><span class="token operator">></span> <span class="token comment" spellcheck="true">// Update the UI.</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><ul><li>首先定义一个ViewModel,在里面放点数据</li><li>然后在MasterFragment和DetailFragment都可以拿到该ViewModel,拿到了该ViewModel就可以拿到里面的数据了,相当于间接通过ViewModel通信了. so easy….</li></ul><hr><p>参考链接:<a href="https://links.jianshu.com/go?to=https%3A%2F%2Fzhuanlan.zhihu.com%2Fp%2F76361500" target="_blank" rel="noopener">https://zhuanlan.zhihu.com/p/76361500</a><br> 链接:<a href="https://www.jianshu.com/p/7e1497871db2" target="_blank" rel="noopener">https://www.jianshu.com/p/7e1497871db2</a></p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>kotlin:安卓开发动态申请多个权限</title>
<link href="http://yoursite.com/2020/09/23/kotlin%EF%BC%9A%E5%AE%89%E5%8D%93%E5%BC%80%E5%8F%91%E5%8A%A8%E6%80%81%E7%94%B3%E8%AF%B7%E5%A4%9A%E4%B8%AA%E6%9D%83%E9%99%90/"/>
<id>http://yoursite.com/2020/09/23/kotlin%EF%BC%9A%E5%AE%89%E5%8D%93%E5%BC%80%E5%8F%91%E5%8A%A8%E6%80%81%E7%94%B3%E8%AF%B7%E5%A4%9A%E4%B8%AA%E6%9D%83%E9%99%90/</id>
<published>2020-09-23T08:31:07.000Z</published>
<updated>2020-10-02T09:17:15.113Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>应用接入支付宝的sdk,需要申请一些权限。</p><p>这里是kotlin的代码:</p><p>对了:6.0以后有3组,27个权限是需要动态申请的,具体,百度吧。</p><p>下面申请了两个权限(要在manifest文件注册哈)</p><pre class=" language-Kotlin"><code class="language-Kotlin"> //1、首先声明一个数组permissions,将需要的权限都放在里面 val permissions = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE) val mPermissionList = ArrayList<String>() val mRequestCode = 0x1//权限请求码 fun initPermission() { mPermissionList.clear() for (permission in permissions) { if (ContextCompat.checkSelfPermission( this@MainActivity, permission ) != PackageManager.PERMISSION_GRANTED ) { mPermissionList.add(permission) } } if (!mPermissionList.isEmpty()) { // 后续操作... ActivityCompat.requestPermissions(this@MainActivity, permissions, mRequestCode) } else { Toast.makeText(this@MainActivity,"全部授予!",Toast.LENGTH_SHORT).show() } }//重写 override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) when (requestCode) { 0x1 -> for (i in 0 until grantResults.size) { if (grantResults[i] != PackageManager.PERMISSION_GRANTED) Toast.makeText(this,"您有未授予的权限,可能影响使用",Toast.LENGTH_SHORT).show() } }// 授权结束后的后续操作</code></pre><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>reply-code=530, reply-text=NOT_ALLOWED - access to vhost '/' refused for user user_admin</title>
<link href="http://yoursite.com/2020/09/22/reply-code=530/"/>
<id>http://yoursite.com/2020/09/22/reply-code=530/</id>
<published>2020-09-22T10:31:07.000Z</published>
<updated>2020-10-02T09:27:13.363Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>Java使用amqp-client连接rabbitmq服务端,报出如下异常</p><pre class=" language-java"><code class="language-java">Exception in thread <span class="token string">"main"</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>IOException at com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>client<span class="token punctuation">.</span>impl<span class="token punctuation">.</span>AMQChannel<span class="token punctuation">.</span><span class="token function">wrap</span><span class="token punctuation">(</span>AMQChannel<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">105</span><span class="token punctuation">)</span> at com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>client<span class="token punctuation">.</span>impl<span class="token punctuation">.</span>AMQChannel<span class="token punctuation">.</span><span class="token function">wrap</span><span class="token punctuation">(</span>AMQChannel<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">101</span><span class="token punctuation">)</span> at com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>client<span class="token punctuation">.</span>impl<span class="token punctuation">.</span>AMQChannel<span class="token punctuation">.</span><span class="token function">exnWrappingRpc</span><span class="token punctuation">(</span>AMQChannel<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">123</span><span class="token punctuation">)</span> at com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>client<span class="token punctuation">.</span>impl<span class="token punctuation">.</span>AMQConnection<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span>AMQConnection<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">377</span><span class="token punctuation">)</span> at com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>client<span class="token punctuation">.</span>ConnectionFactory<span class="token punctuation">.</span><span class="token function">newConnection</span><span class="token punctuation">(</span>ConnectionFactory<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">824</span><span class="token punctuation">)</span> at com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>client<span class="token punctuation">.</span>ConnectionFactory<span class="token punctuation">.</span><span class="token function">newConnection</span><span class="token punctuation">(</span>ConnectionFactory<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">778</span><span class="token punctuation">)</span> at com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>client<span class="token punctuation">.</span>ConnectionFactory<span class="token punctuation">.</span><span class="token function">newConnection</span><span class="token punctuation">(</span>ConnectionFactory<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">868</span><span class="token punctuation">)</span> at rq<span class="token punctuation">.</span>Producer<span class="token punctuation">.</span><span class="token function">main</span><span class="token punctuation">(</span>Producer<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">30</span><span class="token punctuation">)</span> at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>NativeMethodAccessorImpl<span class="token punctuation">.</span><span class="token function">invoke0</span><span class="token punctuation">(</span>Native Method<span class="token punctuation">)</span> at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>NativeMethodAccessorImpl<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>NativeMethodAccessorImpl<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">62</span><span class="token punctuation">)</span> at sun<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>DelegatingMethodAccessorImpl<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>DelegatingMethodAccessorImpl<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">43</span><span class="token punctuation">)</span> at java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>Method<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>Method<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">498</span><span class="token punctuation">)</span> at com<span class="token punctuation">.</span>intellij<span class="token punctuation">.</span>rt<span class="token punctuation">.</span>execution<span class="token punctuation">.</span>application<span class="token punctuation">.</span>AppMain<span class="token punctuation">.</span><span class="token function">main</span><span class="token punctuation">(</span>AppMain<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">147</span><span class="token punctuation">)</span>Caused by<span class="token operator">:</span> com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>client<span class="token punctuation">.</span>ShutdownSignalException<span class="token operator">:</span> connection error<span class="token punctuation">;</span> protocol method<span class="token operator">:</span> #method<span class="token operator"><</span>connection<span class="token punctuation">.</span>close<span class="token operator">></span><span class="token punctuation">(</span>reply<span class="token operator">-</span>code<span class="token operator">=</span><span class="token number">530</span><span class="token punctuation">,</span> reply<span class="token operator">-</span>text<span class="token operator">=</span>NOT_ALLOWED <span class="token operator">-</span> access to vhost <span class="token string">'/'</span> refused <span class="token keyword">for</span> user <span class="token string">'user_admin'</span><span class="token punctuation">,</span> <span class="token keyword">class</span><span class="token operator">-</span>id<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">,</span> method<span class="token operator">-</span>id<span class="token operator">=</span><span class="token number">40</span><span class="token punctuation">)</span> at com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>utility<span class="token punctuation">.</span>ValueOrException<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span>ValueOrException<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">66</span><span class="token punctuation">)</span> at com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>utility<span class="token punctuation">.</span>BlockingValueOrException<span class="token punctuation">.</span><span class="token function">uninterruptibleGetValue</span><span class="token punctuation">(</span>BlockingValueOrException<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">32</span><span class="token punctuation">)</span> at com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>client<span class="token punctuation">.</span>impl<span class="token punctuation">.</span>AMQChannel$BlockingRpcContinuation<span class="token punctuation">.</span><span class="token function">getReply</span><span class="token punctuation">(</span>AMQChannel<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">360</span><span class="token punctuation">)</span> at com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>client<span class="token punctuation">.</span>impl<span class="token punctuation">.</span>AMQChannel<span class="token punctuation">.</span><span class="token function">privateRpc</span><span class="token punctuation">(</span>AMQChannel<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">225</span><span class="token punctuation">)</span> at com<span class="token punctuation">.</span>rabbitmq<span class="token punctuation">.</span>client<span class="token punctuation">.</span>impl<span class="token punctuation">.</span>AMQChannel<span class="token punctuation">.</span><span class="token function">exnWrappingRpc</span><span class="token punctuation">(</span>AMQChannel<span class="token punctuation">.</span>java<span class="token operator">:</span><span class="token number">117</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token number">10</span> more</code></pre><p>定位发现是连接rabbitmq使用的用户没有赋予访问权限,我创建的是admin用户,给admin用户赋予‘/’目录的访问权限就可以,执行如下命令:</p><pre class=" language-java"><code class="language-java">sudo rabbitmqctl set_permissions <span class="token operator">-</span>p <span class="token operator">/</span> admin <span class="token string">'.*'</span> <span class="token string">'.*'</span> <span class="token string">'.*'</span></code></pre><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>Android LayoutInflater.inflate()使用详解</title>
<link href="http://yoursite.com/2020/09/22/Android%20LayoutInflater.inflate()%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3/"/>
<id>http://yoursite.com/2020/09/22/Android%20LayoutInflater.inflate()%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3/</id>
<published>2020-09-22T08:31:07.000Z</published>
<updated>2020-10-02T09:16:37.491Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>相信大家都用过LayoutInflater(布局填充器),今天主要说下我对inflate方法的使用理解。<br> inflate方法有如下两种:</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> View <span class="token function">inflate</span> <span class="token punctuation">(</span><span class="token keyword">int</span> resource<span class="token punctuation">,</span> ViewGroup root<span class="token punctuation">)</span><span class="token keyword">public</span> View <span class="token function">inflate</span> <span class="token punctuation">(</span><span class="token keyword">int</span> resource<span class="token punctuation">,</span> ViewGroup root<span class="token punctuation">,</span> <span class="token keyword">boolean</span> attachToRoot<span class="token punctuation">)</span></code></pre><p>查看源码,我们发现两个参数的方法内部引用了三个参数的方法</p><pre class=" language-kotlin"><code class="language-kotlin"><span class="token keyword">public</span> View <span class="token function">inflate</span><span class="token punctuation">(</span><span class="token annotation builtin">@LayoutRes</span> int resource<span class="token punctuation">,</span> <span class="token annotation builtin">@Nullable</span> ViewGroup root<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token function">inflate</span><span class="token punctuation">(</span>resource<span class="token punctuation">,</span> root<span class="token punctuation">,</span> root <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>我们将两个参数的方法转化为三个参数的方法。如下:</p><pre class=" language-php"><code class="language-php">inflater<span class="token punctuation">.</span><span class="token function">inflate</span><span class="token punctuation">(</span>R<span class="token punctuation">.</span>layout<span class="token punctuation">.</span>item<span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>—————<span class="token operator">></span>inflater<span class="token punctuation">.</span><span class="token function">inflate</span><span class="token punctuation">(</span>R<span class="token punctuation">.</span>layout<span class="token punctuation">.</span>item<span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token keyword">null</span> <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>相当于:inflater<span class="token punctuation">.</span><span class="token function">inflate</span><span class="token punctuation">(</span>R<span class="token punctuation">.</span>layout<span class="token punctuation">.</span>item<span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>inflater<span class="token punctuation">.</span><span class="token function">inflate</span><span class="token punctuation">(</span>R<span class="token punctuation">.</span>layout<span class="token punctuation">.</span>item<span class="token punctuation">,</span> <span class="token keyword">parent</span><span class="token punctuation">)</span><span class="token punctuation">;</span>—————<span class="token operator">></span>inflater<span class="token punctuation">.</span><span class="token function">inflate</span><span class="token punctuation">(</span>R<span class="token punctuation">.</span>layout<span class="token punctuation">.</span>item<span class="token punctuation">,</span> <span class="token keyword">parent</span><span class="token punctuation">,</span> <span class="token keyword">parent</span><span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>相当于:inflater<span class="token punctuation">.</span><span class="token function">inflate</span><span class="token punctuation">(</span>R<span class="token punctuation">.</span>layout<span class="token punctuation">.</span>item<span class="token punctuation">,</span> <span class="token keyword">parent</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>所以我们只需要研究四个方法</p><pre class=" language-css"><code class="language-css">inflater<span class="token number">.</span><span class="token function">inflate</span><span class="token punctuation">(</span>R<span class="token number">.</span>layout<span class="token number">.</span>item, null, true<span class="token punctuation">)</span><span class="token punctuation">;</span>inflater<span class="token number">.</span><span class="token function">inflate</span><span class="token punctuation">(</span>R<span class="token number">.</span>layout<span class="token number">.</span>item, null, false<span class="token punctuation">)</span><span class="token punctuation">;</span>inflater<span class="token number">.</span><span class="token function">inflate</span><span class="token punctuation">(</span>R<span class="token number">.</span>layout<span class="token number">.</span>item, parent, true<span class="token punctuation">)</span><span class="token punctuation">;</span>inflater<span class="token number">.</span><span class="token function">inflate</span><span class="token punctuation">(</span>R<span class="token number">.</span>layout<span class="token number">.</span>item, parent, false<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><h5 id="首先我先说下我对每个参数的理解"><a href="#首先我先说下我对每个参数的理解" class="headerlink" title="首先我先说下我对每个参数的理解"></a>首先我先说下我对每个参数的理解</h5><p>第一个参数:想要添加的布局<br> 第二个参数:想要添加到哪个布局上面<br> (<strong>null和有值的区别 null时第一个参数中最外层的布局大小无效,有值的时候最外层的布局大小有效</strong>)<br> 第三个参数:是否直接添加到第二个参数布局上面<br> (<strong>true代表layout文件填充的View会被直接添加进parent,而传入false则代表创建的View会以其他方式被添加进parent</strong>)</p><h5 id="下面我们来对四个方法一一讲解"><a href="#下面我们来对四个方法一一讲解" class="headerlink" title="下面我们来对四个方法一一讲解"></a>下面我们来对四个方法一一讲解</h5><p>为了更好的理解我准备了一个简单的案例,我的Activity的布局如下:一个超简单的RelativeLayout 布局,我们在这理解为父布局</p><pre class=" language-xml"><code class="language-xml"><span class="token prolog"><?xml version="1.0" encoding="utf-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>RelativeLayout</span> <span class="token attr-name"><span class="token namespace">xmlns:</span>android</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://schemas.android.com/apk/res/android<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>@+id/rl<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>match_parent<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>RelativeLayout</span><span class="token punctuation">></span></span></code></pre><p>我还有一个布局layout.xml如下:有一个宽度高度为200dp的LinearLayout ,LinearLayout 中有一个Button按钮。</p><pre class=" language-xml"><code class="language-xml"><span class="token prolog"><?xml version="1.0" encoding="utf-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>LinearLayout</span> <span class="token attr-name"><span class="token namespace">xmlns:</span>android</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://schemas.android.com/apk/res/android<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>150dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>150dp<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>background</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>#ff0000<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>Button</span> <span class="token attr-name"><span class="token namespace">android:</span>layout_width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>layout_height</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wrap_content<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">android:</span>text</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>开开心心每一天<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>LinearLayout</span><span class="token punctuation">></span></span></code></pre><p>布局准好了,下面进入正题。我要把layout.xml这个布局文件添加到我的activity的布局中,我的Activity的代码如下:</p><p><img src="https:////upload-images.jianshu.io/upload_images/1912584-58ec3e3fc7a313b4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/625/format/webp" alt="img"></p><p>Activity.png</p><p>为了看到四个方法有什么不一样,我依次替换圈起来的代码,查看展示效果:</p><h6 id="1:inflater-inflate-R-layout-item-null-true"><a href="#1:inflater-inflate-R-layout-item-null-true" class="headerlink" title="1:inflater.inflate(R.layout.item, null, true);"></a>1:inflater.inflate(R.layout.item, null, true);</h6><p><img src="https:////upload-images.jianshu.io/upload_images/1912584-1b2425267e872c22.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/441/format/webp" alt="img"></p><p>img.png</p><h6 id="2:inflater-inflate-R-layout-item-null-false"><a href="#2:inflater-inflate-R-layout-item-null-false" class="headerlink" title="2:inflater.inflate(R.layout.item, null, false);"></a>2:inflater.inflate(R.layout.item, null, false);</h6><p><img src="https:////upload-images.jianshu.io/upload_images/1912584-1b2425267e872c22.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/441/format/webp" alt="img"></p><p>img.png</p><h6 id="3:inflater-inflate-R-layout-item-parent-true"><a href="#3:inflater-inflate-R-layout-item-parent-true" class="headerlink" title="3:inflater.inflate(R.layout.item, parent, true);"></a>3:inflater.inflate(R.layout.item, parent, true);</h6><p>使用该方法时rl.addView(view);代码报错,错误异常如下</p><pre class=" language-csharp"><code class="language-csharp">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>IllegalStateException<span class="token punctuation">:</span> The specified child already has a parent<span class="token punctuation">.</span> You must call <span class="token function">removeView</span><span class="token punctuation">(</span><span class="token punctuation">)</span> on the child's parent first<span class="token punctuation">.</span></code></pre><p>意思是非法状态异常,指定的child已经有一个父容器了。你必须先用chile的父容器调用removeView()。</p><h6 id="源码中对第三个参数的解释是"><a href="#源码中对第三个参数的解释是" class="headerlink" title="源码中对第三个参数的解释是"></a>源码中对第三个参数的解释是</h6><pre class=" language-csharp"><code class="language-csharp">Whether the inflated hierarchy should be attached to the root parameter<span class="token operator">?</span> If <span class="token keyword">false</span><span class="token punctuation">,</span> root <span class="token keyword">is</span> only used to create the correct subclass of LayoutParams <span class="token keyword">for</span> the root view <span class="token keyword">in</span> the XML<span class="token punctuation">.</span></code></pre><p>attachToRoot传入true代表layout文件填充的View会被直接添加进parent,而传入false则代表创建的View会以其他方式被添加进parent。</p><h6 id="解决方法"><a href="#解决方法" class="headerlink" title="解决方法"></a>解决方法</h6><p>删除rl.addView(view);这一行代码,然后运行展示效果</p><p><img src="https:////upload-images.jianshu.io/upload_images/1912584-ee882486eab4b075.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/428/format/webp" alt="img"></p><p>img.png</p><h6 id="4:inflater-inflate-R-layout-item-parent-false"><a href="#4:inflater-inflate-R-layout-item-parent-false" class="headerlink" title="4:inflater.inflate(R.layout.item, parent, false);"></a>4:inflater.inflate(R.layout.item, parent, false);</h6><p><img src="https:////upload-images.jianshu.io/upload_images/1912584-ee882486eab4b075.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/428/format/webp" alt="img"></p><p>img.png</p><h5 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h5><p> 从案例中我们可以看到1、2这两种方式运行结果一样,第二个参数传null会使要添加的布局视图中根视图的宽高设置失效,在使用这种方式的时候会造成我们无形中多加一层布局,为了使其子view显示相应高度。<strong>在这里不推荐使用</strong><br> 3这种方式我们在使用的时候一定要注意,它会使代表layout文件填充的View会被直接添加进parent,如果我们使用这种方式后,在执行addView()方法就会造成上面的错误。</p><h4 id="推荐使用第四种方式-inflater-inflate-R-layout-item-parent-false"><a href="#推荐使用第四种方式-inflater-inflate-R-layout-item-parent-false" class="headerlink" title="推荐使用第四种方式 inflater.inflate(R.layout.item, parent, false);"></a>推荐使用第四种方式 inflater.inflate(R.layout.item, parent, false);</h4><p>以上是我对inflate方法的使用理解,有什么不对的地方,大家积极留言,我看到后会第一时间回复大家,最后祝大家每天都有个好心情。</p><p>作者:石头1314<br>链接:<a href="https://www.jianshu.com/p/c92243287793" target="_blank" rel="noopener">https://www.jianshu.com/p/c92243287793</a><br>来源:简书<br>著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。</p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
<entry>
<title>android studio如何导入jar包</title>
<link href="http://yoursite.com/2020/09/21/android%20studio%E5%A6%82%E4%BD%95%E5%AF%BC%E5%85%A5jar%E5%8C%85/"/>
<id>http://yoursite.com/2020/09/21/android%20studio%E5%A6%82%E4%BD%95%E5%AF%BC%E5%85%A5jar%E5%8C%85/</id>
<published>2020-09-21T08:31:07.000Z</published>
<updated>2020-10-02T09:15:44.616Z</updated>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><p><a href="https://jingyan.baidu.com/article/f3e34a121b0a11f5eb65359a.html" target="_blank" rel="noopener">https://jingyan.baidu.com/article/f3e34a121b0a11f5eb65359a.html</a></p><script> document.querySelectorAll('.github-emoji') .forEach(el => { if (!el.dataset.src) { return; } const img = document.createElement('img'); img.style = 'display:none !important;'; img.src = el.dataset.src; img.addEventListener('error', () => { img.remove(); el.style.color = 'inherit'; el.style.backgroundImage = 'none'; el.style.background = 'none'; }); img.addEventListener('load', () => { img.remove(); }); document.body.appendChild(img); }); </script>]]></content>
<summary type="html">
<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" cla
</summary>
</entry>
</feed>