android应用开发:android网络通信

众所周知,Android与服务器通信通常采用HTTP通信方式和Socket通信方式,而HTTP通信方式又分getpost两种方式。

HTTP协议简介:

        HTTP (Hypertext Transfer Protocol ),是Web联网的基础,也是手机联网常用的协议之一,HTTP协议是建立在TCP协议之上的一种协议。

        HTTP连接最显著的特点是客户端发送的每次请求都需要服务器回送响应,在请求结束后,会主动释放连接。从建立连接到关闭连接的过程称为一次连接  HTTP 1.0中,客户端的每次请求都要求建立一次单独的连接,在处理完本次请求后,就自动释放连接。  HTTP 1.1中则可以在一次连接中处理多个请求,并且多个请求可以重叠进行,不需要等待一个请求结束后再发送下一个请求。

        由于HTTP在每次请求结束后都会主动释放连接,因此HTTP连接是一种短连接无状态,要保持客户端程序的在线状态,需要不断地向服务器发起连接请求。通常的做法是即使不需要获得任何数据,客户端也保持每隔一段固定的时间向服务器发送一次保持连接的请求,服务器在收到该请求后对客户端进行回复,表明知道客户端在线。若服务器长时间无法收到客户端的请求,则认为客户端下线,若客户端长时间无法收到服务器的回复,则认为网络已经断开。

        

        基于HTTP1.0协议的客户端在每次向服务器发出请求后,服务器就会向客户端返回响应消息,在确认客户端已经收到响应消息后,服务端就会关闭网络连接。在这个数据传输过程中,并不保存任何历史信息和状态信息,因此,HTTP协议也被认为是无状态的协议。


        HTTP1.1HTTP1.0相比较而言,最大的区别就是增加了持久连接支持。当客户端使用HTTP1.1协议连接到服务器后,服务器就将关闭客户端连接的主动权交还给客户端;也就是说,只要不调用Socket类的close方法关闭网络连接,就可以继续向服务器发送HTTP请求。

        HTTP连接使用的是请求响应的方式(2次握手),不仅在请求时需要先建立连接,而且需要客户端向服务器发出请求后,服务器端才能回复数据。而Socket连接在双方建立起连接后就可以直接进行数据的传输



         HTTP协议的特点:         支持B/SC/S模式;

        简单快速:客户向服务器请求服务时,只需传送请求方法和路径。请求方法常用的有GETHEADPOST

        灵活:HTTP 允许传输任意类型的数据对象。正在传输的类型由Content-Type 加以标记;

        无状态:HTTP 协议是无状态协议。无状态是指协议对于事务处理没有记忆能力。缺少状态意味着如果后续处理需要前面的信息,则它必须重传,这样可能导致每次连接传送的数据量增大。

        HTTP协议请求方法:

        请求行中包括了请求方法,解释如下:

        GET 请求获取Request-URI 所标识的资源;

        POST Request-URI 所标识的资源后附加新的数据;

        HEAD 请求获取由Request-URI 所标识的资源的响应消息报头         

        PUT 请求服务器存储一个资源,并用Request-URI 作为其标识

        DELETE 请求服务器删除Request-URI 所标识的资源;

        TRACE 请求服务器回送收到的请求信息,主要用于测试或诊断

        CONNECT 保留将来使用

        OPTIONS 请求查询服务器的性能,或者查询与资源相关的选项和需求

        

        GetPost请求区别:

        Post请求可以向服务器传送数据,而且数据放在HTML HEADER内一起传送到服务端URL地址,数据对用户不可见。而get是把参数数据队列加到提交的URL中,值和表单内各个字段一一对应, 例如

http://c.tieba.baidu.com/p/3377964103
http://c.tieba.baidu.com/p/3378005804
http://c.tieba.baidu.com/p/3378009365
http://c.tieba.baidu.com/p/3378012438
http://c.tieba.baidu.com/p/3378016459
http://c.tieba.baidu.com/p/3378016610
http://c.tieba.baidu.com/p/3378019503
http://c.tieba.baidu.com/p/3378019830
http://c.tieba.baidu.com/p/3378021984
http://c.tieba.baidu.com/p/3378022788
http://c.tieba.baidu.com/p/3378035226
http://c.tieba.baidu.com/p/3378036943
http://c.tieba.baidu.com/p/3378037887
http://c.tieba.baidu.com/p/3378040680
http://c.tieba.baidu.com/p/3378044481
http://c.tieba.baidu.com/p/3378045262
http://c.tieba.baidu.com/p/3378047815
http://c.tieba.baidu.com/p/3378049138
http://c.tieba.baidu.com/p/3378050716
http://c.tieba.baidu.com/p/3378051436
http://c.tieba.baidu.com/p/3378054281
http://c.tieba.baidu.com/p/3378064684
http://c.tieba.baidu.com/p/3378065265
http://c.tieba.baidu.com/p/3378067482
http://c.tieba.baidu.com/p/3378067923
http://c.tieba.baidu.com/p/3378069730
http://c.tieba.baidu.com/p/3378070378
http://c.tieba.baidu.com/p/3378057285
http://c.tieba.baidu.com/p/3378071134
http://c.tieba.baidu.com/p/3378071751
http://c.tieba.baidu.com/p/3378072446
http://c.tieba.baidu.com/p/3378073739
http://c.tieba.baidu.com/p/3378074211
http://c.tieba.baidu.com/p/3378074998
http://c.tieba.baidu.com/p/3378076189
http://c.tieba.baidu.com/p/3378076255
http://c.tieba.baidu.com/p/3378077285
http://c.tieba.baidu.com/p/3378077446
http://c.tieba.baidu.com/p/3378078582
http://c.tieba.baidu.com/p/3378079467
http://c.tieba.baidu.com/p/3378079774
http://c.tieba.baidu.com/p/3378080709
http://c.tieba.baidu.com/p/3378080912
http://c.tieba.baidu.com/p/3378081949
http://c.tieba.baidu.com/p/3378082221
http://c.tieba.baidu.com/p/3378082974
http://c.tieba.baidu.com/p/3378084014
http://c.tieba.baidu.com/p/3378084930
http://c.tieba.baidu.com/p/3378085804
http://c.tieba.baidu.com/p/3378086708
http://c.tieba.baidu.com/p/3378087595
http://c.tieba.baidu.com/p/3378088404
http://c.tieba.baidu.com/p/3378089243
http://c.tieba.baidu.com/p/3378090829
http://c.tieba.baidu.com/p/3378091595
http://c.tieba.baidu.com/p/3378092332
http://c.tieba.baidu.com/p/3378093041
http://c.tieba.baidu.com/p/3378093707
http://c.tieba.baidu.com/p/3378094380
http://c.tieba.baidu.com/p/3378095068
http://c.tieba.baidu.com/p/3378095692
http://c.tieba.baidu.com/p/3378096379
http://c.tieba.baidu.com/p/3378096999
http://c.tieba.baidu.com/p/3378097660
http://c.tieba.baidu.com/p/3378098251
http://c.tieba.baidu.com/p/3378098860
http://c.tieba.baidu.com/p/3378099478
http://c.tieba.baidu.com/p/3378100110
http://c.tieba.baidu.com/p/3378100746
http://c.tieba.baidu.com/p/3378101356
http://c.tieba.baidu.com/p/3378101940
http://c.tieba.baidu.com/p/3378103574
http://c.tieba.baidu.com/p/3378104124
http://c.tieba.baidu.com/p/3378104670
http://c.tieba.baidu.com/p/3378105199
http://c.tieba.baidu.com/p/3378105692
http://c.tieba.baidu.com/p/3378106169
http://c.tieba.baidu.com/p/3378106713
http://c.tieba.baidu.com/p/3378107195
http://c.tieba.baidu.com/p/3378107692
http://c.tieba.baidu.com/p/3378108127
http://c.tieba.baidu.com/p/3378108595
http://c.tieba.baidu.com/p/3378109024
http://c.tieba.baidu.com/p/3378109421
http://c.tieba.baidu.com/p/3378109842
http://c.tieba.baidu.com/p/3378110259
http://c.tieba.baidu.com/p/3378110693
http://c.tieba.baidu.com/p/3378111135
http://c.tieba.baidu.com/p/3378111530
http://c.tieba.baidu.com/p/3378111917
http://c.tieba.baidu.com/p/3378103028
http://c.tieba.baidu.com/p/3378112307
http://c.tieba.baidu.com/p/3378112702
http://c.tieba.baidu.com/p/3378112702
http://c.tieba.baidu.com/p/3378113492
http://c.tieba.baidu.com/p/3378113894
http://c.tieba.baidu.com/p/3378114317
http://c.tieba.baidu.com/p/3378114674
http://c.tieba.baidu.com/p/3378102481
http://c.tieba.baidu.com/p/3378115036
http://c.tieba.baidu.com/p/3378115397
http://c.tieba.baidu.com/p/3378115758
http://c.tieba.baidu.com/p/3378116127
http://c.tieba.baidu.com/p/3378116540
http://c.tieba.baidu.com/p/3378116929
http://c.tieba.baidu.com/p/3378117277
http://c.tieba.baidu.com/p/3378116929
http://c.tieba.baidu.com/p/3378117631
http://c.tieba.baidu.com/p/3378118023
http://c.tieba.baidu.com/p/3378118423
http://c.tieba.baidu.com/p/3378118788
http://c.tieba.baidu.com/p/3378119164
http://c.tieba.baidu.com/p/3378119578
http://c.tieba.baidu.com/p/3378120029
http://c.tieba.baidu.com/p/3378120487
http://c.tieba.baidu.com/p/3378120897
http://c.tieba.baidu.com/p/3378121295
http://c.tieba.baidu.com/p/3378121737
http://c.tieba.baidu.com/p/3378122175
http://c.tieba.baidu.com/p/3378122609
http://c.tieba.baidu.com/p/3378123095
http://c.tieba.baidu.com/p/3378798630
http://c.tieba.baidu.com/p/3378804120
http://c.tieba.baidu.com/p/3378807890
http://c.tieba.baidu.com/p/3378812662
http://c.tieba.baidu.com/p/3378820014
http://c.tieba.baidu.com/p/3378824084
http://c.tieba.baidu.com/p/3378832928
http://c.tieba.baidu.com/p/3378842937
http://c.tieba.baidu.com/p/3378850215
http://c.tieba.baidu.com/p/3378855876
http://c.tieba.baidu.com/p/3378861160
http://c.tieba.baidu.com/p/3378865053
http://c.tieba.baidu.com/p/3378873772
http://c.tieba.baidu.com/p/3378880510
http://c.tieba.baidu.com/p/3378886573
http://c.tieba.baidu.com/p/3378890024
http://c.tieba.baidu.com/p/3378900813
http://c.tieba.baidu.com/p/3378904442
http://c.tieba.baidu.com/p/3378910860
http://c.tieba.baidu.com/p/3378919463
http://c.tieba.baidu.com/p/3378924191
http://c.tieba.baidu.com/p/3378928733
http://c.tieba.baidu.com/p/3378933474
http://c.tieba.baidu.com/p/3378938277
http://c.tieba.baidu.com/p/3378977259
http://c.tieba.baidu.com/p/3378987464
http://c.tieba.baidu.com/p/3378997405
http://c.tieba.baidu.com/p/3379001265
http://c.tieba.baidu.com/p/3379005827
http://c.tieba.baidu.com/p/3379013947
http://c.tieba.baidu.com/p/3379027672
http://c.tieba.baidu.com/p/3379028124
http://c.tieba.baidu.com/p/3379040717
http://c.tieba.baidu.com/p/3379044326
http://c.tieba.baidu.com/p/3379053998
http://c.tieba.baidu.com/p/3379061330
http://c.tieba.baidu.com/p/3379066850
http://c.tieba.baidu.com/p/3379071395
http://c.tieba.baidu.com/p/3379078552
http://c.tieba.baidu.com/p/3379087386
http://c.tieba.baidu.com/p/3379104849
http://c.tieba.baidu.com/p/3379120475
http://c.tieba.baidu.com/p/3379127041
http://c.tieba.baidu.com/p/3379130751
http://c.tieba.baidu.com/p/3379136990
http://c.tieba.baidu.com/p/3379143814
http://c.tieba.baidu.com/p/3379150653
http://c.tieba.baidu.com/p/3379157738
http://c.tieba.baidu.com/p/3379164716
http://c.tieba.baidu.com/p/3379168415
http://c.tieba.baidu.com/p/3379197784
http://c.tieba.baidu.com/p/3379202187
http://c.tieba.baidu.com/p/3379207322
http://c.tieba.baidu.com/p/3379398670
http://c.tieba.baidu.com/p/3379411106
http://c.tieba.baidu.com/p/3379424106
http://c.tieba.baidu.com/p/3379435926
http://c.tieba.baidu.com/p/3379442149
http://c.tieba.baidu.com/p/3379447860
http://c.tieba.baidu.com/p/3379451787
http://c.tieba.baidu.com/p/3379457907
http://c.tieba.baidu.com/p/3379463892
http://c.tieba.baidu.com/p/3379470556
http://c.tieba.baidu.com/p/3379475874
http://c.tieba.baidu.com/p/3379481202
http://c.tieba.baidu.com/p/3379486512
http://c.tieba.baidu.com/p/3379491857
http://c.tieba.baidu.com/p/3379507300
http://c.tieba.baidu.com/p/3379513254
http://c.tieba.baidu.com/p/3379518782
http://c.tieba.baidu.com/p/3379523805
http://c.tieba.baidu.com/p/3379618567
http://c.tieba.baidu.com/p/3379622898
http://c.tieba.baidu.com/p/3379649500
http://c.tieba.baidu.com/p/3379655168
http://c.tieba.baidu.com/p/3379674479
http://c.tieba.baidu.com/p/3379680086
http://c.tieba.baidu.com/p/3379686005
http://c.tieba.baidu.com/p/3379691944
http://c.tieba.baidu.com/p/3379697946
http://c.tieba.baidu.com/p/3379703907
http://c.tieba.baidu.com/p/3379709893
http://c.tieba.baidu.com/p/3379715841
http://c.tieba.baidu.com/p/3379721783
http://c.tieba.baidu.com/p/3379727566
http://c.tieba.baidu.com/p/3379733382
http://c.tieba.baidu.com/p/3379737574
http://c.tieba.baidu.com/p/3379738993
http://c.tieba.baidu.com/p/3379744780
http://c.tieba.baidu.com/p/3379746437
http://c.tieba.baidu.com/p/3379750440
http://c.tieba.baidu.com/p/3379754764
http://c.tieba.baidu.com/p/3379756122
http://c.tieba.baidu.com/p/3379761769
http://c.tieba.baidu.com/p/3379761886
http://c.tieba.baidu.com/p/3379767622
http://c.tieba.baidu.com/p/3379773214
http://c.tieba.baidu.com/p/3379775595
http://c.tieba.baidu.com/p/3379778839
http://c.tieba.baidu.com/p/3379784070
http://c.tieba.baidu.com/p/3379788547
http://c.tieba.baidu.com/p/3379789495
http://c.tieba.baidu.com/p/3379794777
http://c.tieba.baidu.com/p/3379800020
http://c.tieba.baidu.com/p/3379805383
http://c.tieba.baidu.com/p/3379806346
http://c.tieba.baidu.com/p/3379810607
http://c.tieba.baidu.com/p/3379814428
http://c.tieba.baidu.com/p/3379815631
http://c.tieba.baidu.com/p/3379818302
http://c.tieba.baidu.com/p/3379820549
http://c.tieba.baidu.com/p/3379825459
http://c.tieba.baidu.com/p/3379828936
http://c.tieba.baidu.com/p/3379830324
http://c.tieba.baidu.com/p/3379835035
http://c.tieba.baidu.com/p/3379836800
http://c.tieba.baidu.com/p/3379839668
http://c.tieba.baidu.com/p/3379839668
http://c.tieba.baidu.com/p/3379844219
http://c.tieba.baidu.com/p/3379851995
http://c.tieba.baidu.com/p/3379857209
http://c.tieba.baidu.com/p/3379857691
http://c.tieba.baidu.com/p/3379862291
http://c.tieba.baidu.com/p/3379862665
http://c.tieba.baidu.com/p/3379867248
http://c.tieba.baidu.com/p/3379869439
http://c.tieba.baidu.com/p/3379871737
http://c.tieba.baidu.com/p/3379873954
http://c.tieba.baidu.com/p/3379875930
http://c.tieba.baidu.com/p/3379880033
http://c.tieba.baidu.com/p/3379880441
http://c.tieba.baidu.com/p/3379884050
http://c.tieba.baidu.com/p/3379887193
http://c.tieba.baidu.com/p/3379887876
http://c.tieba.baidu.com/p/3379891568
http://c.tieba.baidu.com/p/3379895149
http://c.tieba.baidu.com/p/3379897354
http://c.tieba.baidu.com/p/3379898661
http://c.tieba.baidu.com/p/3379900071
http://c.tieba.baidu.com/p/3379901971
http://c.tieba.baidu.com/p/3379905277
http://c.tieba.baidu.com/p/3379906939
http://c.tieba.baidu.com/p/3379908508
http://c.tieba.baidu.com/p/3379911512
http://c.tieba.baidu.com/p/3379911521
http://c.tieba.baidu.com/p/3379913696
http://c.tieba.baidu.com/p/3379914416
http://c.tieba.baidu.com/p/3379917089
http://c.tieba.baidu.com/p/3379919751
http://c.tieba.baidu.com/p/3379920356
http://c.tieba.baidu.com/p/3379922427
http://c.tieba.baidu.com/p/3379923497
http://c.tieba.baidu.com/p/3379924940
http://c.tieba.baidu.com/p/3379927196
http://c.tieba.baidu.com/p/3379926347
http://c.tieba.baidu.com/p/3379937545
http://c.tieba.baidu.com/p/3379940750
http://c.tieba.baidu.com/p/3379943364
http://c.tieba.baidu.com/p/3379975539
http://c.tieba.baidu.com/p/3379976909
http://c.tieba.baidu.com/p/3379977884
http://c.tieba.baidu.com/p/3379978817
http://c.tieba.baidu.com/p/3379980461
http://c.tieba.baidu.com/p/3379981524
http://c.tieba.baidu.com/p/3379982529
http://c.tieba.baidu.com/p/3379983460
http://c.tieba.baidu.com/p/3379984504
http://c.tieba.baidu.com/p/3379985441
http://c.tieba.baidu.com/p/3379986132
http://c.tieba.baidu.com/p/3379987257
http://c.tieba.baidu.com/p/3379988702
http://c.tieba.baidu.com/p/3379989470
http://c.tieba.baidu.com/p/3379989937
http://c.tieba.baidu.com/p/3379990662
http://c.tieba.baidu.com/p/3379991227
http://c.tieba.baidu.com/p/3379991770
http://c.tieba.baidu.com/p/3379992308

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。