android寮€鍙戠瑪璁颁箣缃戠粶缂栫▼鈥斾娇鐢═CP鍗忚鍜孶RL杩涜缃戠粶缂栫▼

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android寮€鍙戠瑪璁颁箣缃戠粶缂栫▼鈥斾娇鐢═CP鍗忚鍜孶RL杩涜缃戠粶缂栫▼相关的知识,希望对你有一定的参考价值。

鏍囩锛?/p>

鎵嬫満鏈韩鏄綔涓烘墜鏈虹粓绔娇鐢ㄧ殑锛屽洜姝ゅ畠鐨勮绠楄兘鍔涳紝瀛樺偍鑳藉姏閮芥槸鏈夐檺鐨勩€傚畠鐨勪富瑕佷紭鍔挎槸鎼哄甫鏂逛究锛屽彲浠ラ殢鏃舵墦寮€锛岃€屼笖鎵嬫満閫氬父鎬绘槸澶勪簬鑱旂綉鐘舵€併€傚洜姝ょ綉缁滄敮鎸佸浜庢墜鏈哄簲鐢ㄩ潪甯搁噸瑕併€?/p>

android瀹屽叏鏀寔JDK鏈韩鐨凾CP锛孶DP缃戠粶閫氫俊API锛屼篃鍙互浣跨敤ServerSocket,Socket鏉ュ缓绔嬪熀浜嶵CP/IP鍗忚鐨勭綉缁滈€氫俊锛屼篃鍙互浣跨敤DatagramSocket,Datagrampacket鏉ュ缓绔嬪熀浜嶶DP鍗忚鐨勭綉缁滈€氫俊銆?
Android涔熸敮鎸丣DK鎻愪緵鐨刄RL锛孶RLConnection绛夌綉缁滈€氫俊API銆?
Android杩樺唴缃簡HttpClient锛屽拰HttpUrlConnection杩欐牱鍙互闈炲父鏂逛究鍦板彂閫丠TTP璇锋眰,骞惰幏鍙朒ttp鍝嶅簲銆?

鈥斺€斿熀浜嶵CP鍗忚鐨勭紪绋?/strong>

    浣跨敤Socket,ServerSocket缂栫▼:

    浣跨敤澶氱嚎绋嬶紝handler鏈哄埗瀹屾垚缃戠粶缂栫▼.

    娉ㄦ剰锛氱敱浜庢墜鏈烘棤绾夸笂缃戠殑IP鍦板潃閫氬父閮芥槸鐢辩Щ鍔ㄨ繍钀ュ叕鍙稿姩鎬佸垎閰嶇殑锛屼竴鑸笉浼氭湁鑷繁鍥哄畾鐨処P鍦板潃锛屽洜姝ゅ緢灏戝湪鎵嬫満涓婅繍琛屾湇鍔″櫒绔紝鏈嶅姟鍣ㄧ閫氬父杩愯鍦ㄦ湁鍥哄畾IP鐨勬湇鍔″櫒涓娿€?
    鈶犺窡缃戠粶鏈夊叧鐨勫氨瑕佸姞Internet鏉冮檺

    鈶$綉缁滄暟鎹殑璇锋眰瑕佸湪瀛愮嚎绋嬩腑杩涜

    鈶㈡洿鏂癠I闇€瑕佸€熷姪Handler鏈哄埗

    鈶g紪鐮侀棶棰橈細Android榛樿缂栫爜涓篣TF-8   Java榛樿缂栫爜涓篏B2312

    鈶ゆ湰鏈篒P鍦板潃闂锛氫娇鐢?27.0.0.1鍦ㄦā鎷熶腑涓嶈兘璁块棶鍒版湰鍦扮殑鏈嶅姟鍣紝鍥犱负妯℃嫙鍣ㄤ篃鍦ㄦ湰鏈?

鏈€鍚庢湁浣跨敤Socket,ServerSocket缃戠粶缂栫▼鐨勭ず渚?/p>

鈥斺€斾娇鐢║RL杩涜缃戠粶缂栫▼

   **鈥斺€斾娇鐢║RL璁块棶缃戠粶璧勬簮**

   URL瀵硅薄浠h〃缁熶竴璧勬簮瀹氫綅绗︺€傚畠鏄寚鍚戜簰鑱旂綉涓殑璧勬簮銆傝璧勬簮鍙互鏄畝鍗曠殑鏂囦欢鎴栫洰褰曘€備篃鍙互鏄鏇村鏉傜殑瀵硅薄鐨勫紩鐢ㄣ€?
   URL鐨勬牸寮忥細protocol://host:port/resourceName

   鍙互浣跨敤鏋勯€犲櫒鍒涘缓URL瀵硅薄锛屼竴鏃﹁幏寰桿RL瀵硅薄锛屽彲浠ヨ皟鐢ㄥ涓嬬殑鏂规硶璁块棶璇RL瀵瑰簲鐨勮祫婧愶細

  String getFile():鑾峰彇URL鐨勮祫婧愬悕

  String getHost():鑾峰彇URL鐨勪富鏈哄悕

  String getPath():鑾峰彇URL鐨勮矾寰勯儴鍒?
  int getPort():鑾峰彇URL鐨勭鍙e彿

  String getProtocol():鑾峰彇URL鐨勫崗璁悕绉?
  String getQuery()锛氳幏鍙朥RL鐨勬煡璇㈠瓧绗︿覆閮ㄥ垎

  InputStream url.openStream():鎵撳紑URL鐨勮繛鎺ワ紝杩斿洖涓€涓鍙栬璧勬簮鐨処nputStream銆?
   **鈥斺€斾娇鐢║RLConnection鎻愪氦璇锋眰**

   URL鐨刼penConnection()鏂规硶灏嗚繑鍥炰竴涓猆RLConnection锛岃瀵硅薄琛ㄧず搴旂敤绋嬪簭鍜孶RL涔嬮棿鐨勯€氫俊杩炴帴锛岀▼搴忓彲浠ラ€氳繃URLConnection瀹炰緥鍚戣URL鍙戦€佽姹傦紝璇诲彇URL寮曠敤鐨勮祫婧愩€傞€氬父鍒涘缓涓€涓拰URL鐨勮繛鎺ワ紝骞跺彂閫佽姹傦紝璇诲彇姝RL寮曠敤鐨勮祫婧愩€?

闇€瑕佸涓嬫楠わ細

a)閫氳繃璋冪敤URL瀵硅薄openConnection()鏂规硶鏉ュ垱寤篣RLConnection瀵硅薄

b)璁剧疆URLConnection鐨勫弬鏁板拰鏅€氳姹傚睘鎬?/p>

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");

//鍙戦€丳OST璇锋眰蹇呴』璁剧疆濡備笅涓よ**

conn.setDoInput(true):璁剧疆璇RLConnection鐨刣oInput璇锋眰澶村瓧娈电殑鍊?/strong>

coon.setDoOutput(true):

c)璋冪敤connect():鎵撳紑鍒版URL寮曠敤鐨勮祫婧愮殑閫氫俊閾炬帴锛堝鏋滃皻鏈缓绔嬭繖鏍风殑杩炴帴锛夈€?

濡傛灉鍦ㄥ凡鎵撳紑杩炴帴(姝ゆ椂 connected 瀛楁鐨勫€间负 true)鐨勬儏鍐典笅璋冪敤 connect 鏂规硶锛屽垯蹇界暐璇ヨ皟鐢?

URLConnection 瀵硅薄缁忓巻涓や釜闃舵锛氶鍏堝垱寤哄璞★紝鐒跺悗寤虹珛杩炴帴銆?/strong>

鍦ㄥ垱寤哄璞′箣鍚庯紝寤虹珛杩炴帴涔嬪墠锛屽彲鎸囧畾鍚勭閫夐」(渚嬪doInput鍜孶seCaches).杩炴帴鍚庡啀杩涜璁剧疆灏变細鍙戠敓閿欒銆傝繛鎺ュ悗鎵嶈兘杩涜鐨勬搷浣?渚嬪getContentLength)锛屽鏈夊繀瑕侊紝灏嗛殣寮忔墽琛岃繛鎺?/strong>.

d)濡傛灉鍙槸鍙戦€丟ET鏂瑰紡璇锋眰锛屼娇鐢╟onnect鏂规硶寤虹珛鍜岃繙绋嬭祫婧愪箣闂寸殑瀹為檯杩炴帴鍗冲彲锛屽湪璇锋眰鐨勫湴鍧€涓紶鍏ユ暟鎹€?/p>

濡傛灉闇€瑕佸彂閫丳ost鏂规硶璇锋眰銆傞渶瑕佽幏鍙?
URLConnection瀹炰緥瀵瑰簲鐨勮緭鍑烘祦鏉ュ彂閫佽姹傚弬鏁帮紝

 PrintWriter out=new PrintWriter(conn.getOutputStream());
 //瑙e喅涔辩爜闂

 String n=EncodingUtils.getString("寮犱笁".getBytes(),"UTF-8");

 out.write("name="+n+"&pwd="+pwd);

 out.flush();//鍒锋柊杈撳嚭娴佺殑缂撳啿

e)杩滅▼璧勬簮鍙樹负鍙敤锛岀▼搴忓彲浠ヨ闂繙绋嬭祫婧愮殑澶村瓧娈垫垨閫氳繃杈撳叆娴佽鍙栬繙绋嬭祫婧愮殑鏁版嵁銆?/p>

getInputStream()鑾峰彇杈撳叆娴併€?
浠庤緭鍏ユ祦璇诲彇response鐨勬暟鎹€?/p>

娉ㄦ剰:

1)濡傛灉鏃㈣浣跨敤杈撳叆娴佽鍙朥RLConnection鍝嶅簲鐨勫唴瀹癸紝涔熻浣跨敤杈撳嚭娴佸彂閫佽姹傚弬鏁帮紝涓€瀹氳鍏堜娇鐢ㄨ緭鍑烘祦锛屽啀浣跨敤杈撳叆娴併€?/p>

2)鍊熷姪浜嶶RLConnection绫荤殑甯姪锛屽簲鐢ㄧ▼搴忓彲浠ラ潪甯告柟渚垮湴涓庢寚瀹氱珯鐐逛氦鎹俊鎭紝鍖呮嫭鍙戦€丟ET璇锋眰锛孭OST璇锋眰锛屽苟鑾峰彇缃戠珯鐨勫搷搴旂瓑銆?/p>

浣跨敤Socket,ServerSocket缂栫▼绀轰緥锛?/p>

鏁堟灉锛?/p>

鎶€鏈垎浜? src=

ServerHuadong涓烘湇鍔″櫒
鍩轰簬TCPIP鍗忚缃戠粶缂栫▼涓哄鎴风

涓嬭浇鍦板潃:http://download.csdn.net




以上是关于android寮€鍙戠瑪璁颁箣缃戠粶缂栫▼鈥斾娇鐢═CP鍗忚鍜孶RL杩涜缃戠粶缂栫▼的主要内容,如果未能解决你的问题,请参考以下文章

python缃戠粶缂栫▼鎬荤粨

缃戠粶缂栫▼ 濂楁帴瀛梥ocket 鍙? 绮樺寘

鍋囪缃戠粶宸ョ▼甯?2鈥斺€斾娇鐢╒RF绌胯秺MPLS鍩熼€氫俊

Linux 缃戠粶妯″瀷

鎵嬫妸鎵嬫暀浣犱娇鐢≒ython缃戠粶鐖櫕鑾峰彇鑿滆氨淇℃伅